简体   繁体   English

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

[英]How to Import Bookmark from xml to existing pdf?

I am currently working on pdf projects (dotnet/c#[ Itextsharp pdf ]), I want to export and import bookmarks from one pdf to another pdf (both pdf are having same content, only difference are with bookmark/without bookmark and one is normal pdf and another linked pdf).我目前正在处理 pdf 项目(dotnet/c#[ Itextsharp pdf ]),我想将书签从一个 pdf 导出和导入到另一个 pdf(两个 pdf 的内容相同,唯一的区别是带书签/不带书签,一个是正常的pdf 和另一个链接的 pdf)。 Exporting bookmarks to xml is working fine but I don`t have the idea of importing exported bookmark(xml) to another pdf.将书签导出到 xml 工作正常,但我不知道将导出的书签(xml)导入另一个 pdf。 Can any body suggest solution.任何机构都可以提出解决方案。

Here I am attaching my code.在这里我附上我的代码。

        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();

My xml output file is我的 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>

My Pdf file available in我的 PDF 文件可在

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

You are currently using the exportToXml() method (see also exportToXml() ; we currently have the API docs in two different places).您当前正在使用exportToXml()方法(另请参阅exportToXml() ;我们目前在两个不同的地方有 API 文档)。

For some reason, you didn't find the importFromXML() method (see also importFromXML() ).出于某种原因,您没有找到importFromXML()方法(另请参阅importFromXML() )。 If you have an XML file containing bookmarks, for instance:如果您有一个包含书签的 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>

You can read this XML file (as an input stream or using a reader), and the importFromXML() method will return a List<HashMap<String,Object>> object.您可以读取这个 XML 文件(作为输入流或使用阅读器), importFromXML()方法将返回一个List<HashMap<String,Object>>对象。 You can use this object to add the bookmarks to a PDF document using the setOutlines() method.您可以使用该对象通过setOutlines()方法将书签添加到 PDF 文档中。 See for instance the BookmarkedTimeTable example.参见例如BookmarkedTimeTable示例。 Or take a look at the answer to this question: Merge pdfs and add bookmark with iText in java或者看看这个问题的答案: Merge pdfs and add bookmark with iText in java

These examples are (of course) in Java, but if you need the Java version, scroll down on the page that bundles the examples of chapter 7 of "iText in Action - Second Edition" and you'll find the C# version of those examples.这些示例(当然)使用 Java 编写,但如果您需要 Java 版本,请向下滚动“iText in Action - Second Edition”第 7 章示例捆绑页面,您将找到这些示例的 C# 版本. For instance BookmarkedTimeTable.cs例如BookmarkedTimeTable.cs

You'll notice that the method setOutlines() doesn't exist in iTextSharp, but that you need to use the property notation:您会注意到 iTextSharp 中不存在setOutlines()方法,但您需要使用属性符号:

stamper.Outlines = outlines;

In this case, outlines is an object of type List<Dictionary<string,object>> (C#) instead of ArrayList<HashMap<String, Object>> .在这种情况下, outlines是一个List<Dictionary<string,object>> (C#) 类型的对象,而不是ArrayList<HashMap<String, Object>> It should be fairly simple for a C# developer to port the Java examples to C#, but when in doubt, check the cs files that are made available on the official web site.对于 C# 开发人员来说,将 Java 示例移植到 C# 应该相当简单,但是如果有疑问,请查看官方网站上提供的cs文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM