如何以编程方式阅读PDF书签

我正在使用PDF转换器访问PDF中的图形数据。 一切正常,但我没有得到书签列表。 是否有可以读取PDF书签的命令行应用程序或C#组件? 我找到了iText和SharpPDF库,我正在浏览它们。 你做过这样的事吗?

请尝试以下代码

PdfReader pdfReader = new PdfReader(filename); IList> bookmarks = SimpleBookmark.GetBookmark(pdfReader); for(int i=0;i 3) { MessageBox.Show(bookmarks[i].ToList().Count.ToString()); } } 

注意:不要忘记将iTextSharp DLL添加到项目中。

如果您使用商业解决方案,则可以尝试使用Docotic.Pdf库完成任务。

下面是一个示例代码,列出书签中包含其部分属性的所有顶级项目。

 using (PdfDocument doc = new PdfDocument("file.pdf")) { PdfOutlineItem root = doc.OutlineRoot; foreach (PdfOutlineItem item in root.Children) { Console.WriteLine("{0} ({1} child nodes, points to page {2})", item.Title, item.ChildCount, item.PageIndex); } } 

PdfOutlineItem类还提供与大纲项样式等相关的属性。

免责声明:我为图书馆的供应商工作。

如果商业图书馆是您的选择,您可以试试Amyuni PDF Creator .Net 。

使用类Amyuni.PDFCreator.IacDocument.RootBookmark来检索书签树的根,然后使用IacBookmark中的属性来访问每个树元素,在树中导航,以及根据需要添加,编辑或删除元素。

通常的免责声明适用

您可以使用PDFsharp库。 它在MIT许可下发布,因此甚至可以在企业开发中使用。 这是一个未经测试的例子。

 using PdfSharp.Pdf; using (PdfDocument document = PdfReader.IO.Open("bookmarked.pdf", IO.PdfDocumentOpenMode.Import)) { PdfDictionary outline = document.Internals.Catalog.Elements.GetDictionary("/Outlines"); PrintBookmark(outline); } void PrintBookmark(PdfDictionary bookmark) { Console.WriteLine(bookmark.Elements.GetString("/Title")); for (PdfDictionary child = bookmark.Elements.GetDictionary("/First"); child != null; child = child.Elements.GetDictionary("/Next")) { PrintBookmark(child); } } 

陷阱:

  • PdfSharp不支持打开pdf超过1.6版本。 (抛出: cannot handle iref streams. the current implementation of pdfsharp cannot handle this pdf feature introduced with acrobat 6
  • PDF格式中有许多类型的字符串,其中PDFsharp返回包括UTF-16BE字符串。 (7.9.2.1 ISO32000 2008)