[英]How to Import Bookmark from xml to existing 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<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 – 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()方法(另请参阅exportToXml() ;我们目前在两个不同的地方有 API 文档)。
出于某种原因,您没有找到importFromXML()方法(另请参阅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 – 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 文件(作为输入流或使用阅读器), importFromXML()
方法将返回一个List<HashMap<String,Object>>
对象。 您可以使用该对象通过setOutlines()
方法将书签添加到 PDF 文档中。 参见例如BookmarkedTimeTable示例。 或者看看这个问题的答案: Merge pdfs and add bookmark with iText in java
这些示例(当然)使用 Java 编写,但如果您需要 Java 版本,请向下滚动“iText in Action - Second Edition”第 7 章示例捆绑页面,您将找到这些示例的 C# 版本. 例如BookmarkedTimeTable.cs
您会注意到 iTextSharp 中不存在setOutlines()
方法,但您需要使用属性符号:
stamper.Outlines = outlines;
在这种情况下, outlines
是一个List<Dictionary<string,object>>
(C#) 类型的对象,而不是ArrayList<HashMap<String, Object>>
。 对于 C# 开发人员来说,将 Java 示例移植到 C# 应该相当简单,但是如果有疑问,请查看官方网站上提供的cs
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.