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

我目前正在研究pdf项目(dotnet / c#[ Itextsharp pdf ]),我想从一个pdf导出书签到另一个pdf(两个pdf都有相同的内容,唯一的区别是书签/没有书签,一个是正常的pdf和另一个链接的pdf)。 将书签导出到xml工作正常,但我没有想法将导出的书签(xml)导入到另一个pdf。 任何机构都可以建议解决

在这里,我附上我的代码。

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

我的xml输出文件是

   Introduction Getting Started <Title>Printing a test page Accessing the novaPDF Printing Preferences – test the multiline bookmark detection option Creating PDF Files   

我的Pdf文件可用

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

您当前正在使用exportToXml()方法(另请参见exportToXml() ;我们目前在两个不同的位置具有API文档)。

出于某种原因,您没有找到importFromXML()方法(另请参见importFromXML() )。 如果您有包含书签的XML文件,例如:

   Introduction Getting Started Printing a test page Accessing the novaPDF Printing Preferences – test the multiline bookmark detection option Creating PDF Files   

您可以读取此XML文件(作为输入流或使用阅读器), importFromXML()方法将返回List>对象。 您可以使用此对象使用setOutlines()方法将书签添加到PDF文档。 例如,参见BookmarkedTimeTable示例。 或者看一下这个问题的答案: 合并pdf并在java中添加带有iText的书签

这些示例(当然)是用Java编写的,但如果您需要Java版本,请向下滚动包含“iText in Action – Second Edition”第7章示例的页面,您将找到这些示例的C#版本。 例如BookmarkedTimeTable.cs

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

 stamper.Outlines = outlines; 

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