我需要在从HTML生成的pdf格式中设计有序列表元素。我知道这不是我尝试过的方式支持的http://demo.itextsupport.com/xmlworker/itextdoc/CSS-conformance-list.htm:
ol li:before {
content: counter(cnt-1) ". ";
counter-increment: cnt-1
}
ol > li > ol > li > ol li:before {
content: counter(cnt-1) "." counter(cnt-2) "." counter(cnt-3);
counter-increment: cnt-3
}有没有什么解决方案可以将处理ol属性(内容、计数器、计数器增量)添加到itextsharp中?
发布于 2014-08-19 21:59:21
我使用HtmlAgility + CSS处理嵌套的ul元素,然后再转换为PDF。
ol { list-style-type:none; list-style:none;}
public string CustomOrderedListNumbering(StringBuilder sb, string xpathRoot)
{
HtmlDocument doc = new HtmlDocument() { OptionAutoCloseOnEnd = true };
HtmlNode.ElementsFlags.Remove("meta");
HtmlNode.ElementsFlags.Remove("link");
doc.LoadHtml(sb.ToString());
HtmlNodeCollection mainOls = doc.DocumentNode.SelectNodes(xpathRoot);
if (mainOls != null)
{
foreach (HtmlAgilityPack.HtmlNode node in mainOls)
{
Recursive(node, "", 1, 1);
}
}
string htmlMainPart = doc.DocumentNode.InnerHtml;
htmlMainPart = htmlMainPart.Replace("<br>", "<br />");
return htmlMainPart;
}
private void Recursive(HtmlNode nodeOl, string baseNum, int depth, int counter)
{
HtmlNodeCollection lis = nodeOl.ChildNodes;
foreach (var li in lis)
{
string num = baseNum + counter + ".";
var textNode = li.SelectSingleNode("./text()") as HtmlTextNode;
if (textNode == null)
{
textNode = li.SelectSingleNode("./*/text()") as HtmlTextNode;
}
textNode.Text = num + " " + textNode.Text;
var childOls = li.SelectNodes("ol[@class='number']");
if (childOls != null)
{
foreach (var cOl in childOls)
{
Recursive(cOl, num, depth + 1, 1);
}
}
counter++;
}
}https://stackoverflow.com/questions/24737991
复制相似问题