0

我使用这个库的最新版本(itextsharp 5.5.11)。我正在使用XMLWorkerHelper将 HTML 转换为 PDF,除了一个之外都很好......当 XMLWorker 看到 tags <h1><h2>等时,这个标签将被加入书签。我可以关闭此功能,因为我创建了书签吗?

在这里我设置了通用标签:

chunk = new Chunk(textOfChapter, fontBold_14);
chunk.SetGenericTag(textOfChapter);
paragraph = new Paragraph(chunk);
doc.Add(paragraph);

addHtmlPage(doc, writer, nodePath, i + 1, str);

对于OnGenericTag

public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text)
{
    string name = "dest" + (counter++);
    int page = writer.PageNumber;

    if (!toc.Keys.Contains<string>(text))
        toc.Add(text, new KeyValuePair<string, int>(name, page));

    writer.AddNamedDestination(name, page, new PdfDestination(PdfDestination.FITH, rect.GetTop(topOffset)));
}

在这里我创建书签:

private PdfOutline createBookmark(PdfOutline pdfBookmark, XmlNode xmlNode, PdfWriter writer,
                              string str, Dictionary<string, KeyValuePair<string, int>> toc)
{
    var text = xmlNode.Attributes["text"].Value;
    KeyValuePair<string, int> value;

    var key = "";
    key = str + text;

    if (toc.ContainsKey(key))
    {
        value = toc[key];
        var page = value.Value;
        var bookmark = new PdfOutline(pdfBookmark,
                                      PdfAction.GotoLocalPage(page,
                                      new PdfDestination(PdfDestination.FITH),
                                      writer), str + " " + text, true);

        return bookmark;
    }
    else
    {
        var bookmark = new PdfOutline(pdfBookmark,
                                      PdfAction.GotoLocalPage(1,
                                      new PdfDestination(PdfDestination.FITH),
                                      writer), "no bookmark*** " + str + " " + text, true);
        return bookmark;
    }
}

我在这里使用XMLWorker

var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css));
var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cleanHtml));
FontFactory.Register(inputFont);
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.Register(inputFont);
FontFactory.FontImp = fontProvider;

XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss, Encoding.UTF8, fontProvider);

我对此进行了测试。我h1在html中添加了标签

HtmlNode NodeH = doc.CreateElement("h1");
NodeH.InnerHtml = HtmlDocument.HtmlEncode(")_________________ LiiIIII ____________________(");
NodeH.SetAttributeValue("align", "center");

nodeBody.PrependChild(NodeH);

我收到了以下信息:

在此处输入图像描述

谢谢你。

4

0 回答 0