如何将书签从 xml 导入现有的 pdf?

How to Import Bookmark from xml to existing pdf?

我目前正在处理 pdf 项目 (dotnet/c#[Itextsharp pdf]),我想将书签从一个 pdf 导出和导入到另一个 pdf(两者都是pdf 具有相同的内容,唯一的区别是带有 bookmark/without 书签,一个是普通 pdf,另一个是链接 pdf)。将书签导出到 xml 工作正常,但我没有将导出的书签 (xml) 导入另一个 pdf 的想法。任何人都可以提出解决方案。

这里附上我的代码。

        string inputpdf = "D:\chapter1.pdf"; string outputbookmark="D:\chapter1Bookmark.xml";
        PdfReader reader = new PdfReader(inputpdf);
        IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);
        using (StreamWriter Sw = new StreamWriter(outputbookmark))
        {
            SimpleBookmark.ExportToXML(bookmarks, Sw,"ISO8859-1", true);

        }
        reader.Close();

我的 xml 输出文件是

<?xml version="1.0" encoding="ISO8859-1"?>
<Bookmark>
  <Title Color="0 0 0" Page="1 XYZ 36 806" Action="GoTo" >Introduction</Title>
  <Title Color="0 0 0" Page="1 XYZ 36 410" Action="GoTo" >Getting Started
    <Title Color="0 0 0" Page="1 XYZ 36 364" Action="GoTo" >Printing a test page</Title>
    <Title Color="0 0 0" Page="4 XYZ 36 740" Action="GoTo" >Accessing the novaPDF Printing Preferences &#8211; test the multiline bookmark detection option</Title>
    <Title Color="0 0 0" Page="5 XYZ 36 806" Action="GoTo" >Creating PDF Files</Title>
  </Title>
</Bookmark>

我的 Pdf 文件在

http://www.novapdf.com/uploads/novapdf_en/media_items/pdf-example-bookmarks.original.pdf

您目前正在使用exportToXml() method (see also exportToXml();我们目前在两个不同的地方有 API 文档。

出于某种原因,您没有找到 importFromXML() method (see also importFromXML())。如果您有一个包含书签的 XML 文件,例如:

<?xml version="1.0" encoding="ISO8859-1"?>
<Bookmark>
  <Title Color="0 0 0" Page="1 XYZ 36 806" Action="GoTo" >Introduction</Title>
  <Title Color="0 0 0" Page="1 XYZ 36 410" Action="GoTo" >Getting Started
    <Title Color="0 0 0" Page="1 XYZ 36 364" Action="GoTo" >Printing a test page</Title>
    <Title Color="0 0 0" Page="4 XYZ 36 740" Action="GoTo" >Accessing the novaPDF Printing Preferences &#8211; test the multiline bookmark detection option</Title>
    <Title Color="0 0 0" Page="5 XYZ 36 806" Action="GoTo" >Creating PDF Files</Title>
  </Title>
</Bookmark>

您可以读取此 XML 文件(作为输入流或使用 reader),并且 importFromXML() 方法将 return 一个 List<HashMap<String,Object>>目的。您可以使用此对象通过 setOutlines() 方法将书签添加到 PDF 文档。例如参见 [​​=22=]

这些示例(当然)在 Java 中,但如果您需要 Java 版本,请在捆绑 chapter 7 of "iText in Action - Second Edition" and you'll find the C# version of those examples. For instance BookmarkedTimeTable.cs[=24= 示例的页面上向下滚动]

你会注意到方法 setOutlines() 在 iTextSharp 中不存在,但你需要使用 属性 表示法:

stamper.Outlines = outlines;

在这种情况下,outlinesList<Dictionary<string,object>> (C#) 类型的对象,而不是 ArrayList<HashMap<String, Object>>。对于 C# 开发人员来说,将 Java 示例移植到 C# 应该相当简单,但如有疑问,请查看官方网站上提供的 cs 文件。