XMLWorkerHelper有序列表样式

时间:2014-07-14 13:46:15

标签: itextsharp

我需要在从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中?

1 个答案:

答案 0 :(得分:0)

我在转换为PDF

之前使用HtmlAgility + CSS处理了嵌套的ul元素
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++;
            }
        }