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

问题描述 投票:11回答:5

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

c# pdf command-line bookmarks
5个回答
12
投票

请尝试以下代码

PdfReader pdfReader = new PdfReader(filename);

IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(pdfReader);

for(int i=0;i<bookmarks.Count;i++)
{
    MessageBox.Show(bookmarks[i].Values.ToArray().GetValue(0).ToString());

    if (bookmarks[i].Count > 3)
    {
        MessageBox.Show(bookmarks[i].ToList().Count.ToString());
    }
}

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


2
投票

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

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

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类还提供与大纲项目样式相关的属性等。

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


2
投票

由于书签是树形结构(https://en.wikipedia.org/wiki/Tree_(data_structure)),我在这里使用了一些递归来收集所有书签和它的孩子。

iTextSharp为我解决了这个问题。

dotnet add package iTextSharp

使用以下代码收集所有书签:

using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using iTextSharp.text.pdf;

namespace PdfManipulation
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder bookmarks = ExtractAllBookmarks("myPdfFile.pdf");
        }

        private static StringBuilder ExtractAllBookmarks(string pdf)
        {
            StringBuilder sb = new StringBuilder();
            PdfReader reader = new PdfReader(pdf);
            IList<Dictionary<string, object>> bookmarksTree = SimpleBookmark.GetBookmark(reader);
            foreach (var node in bookmarksTree)
            {
                sb.AppendLine(PercorreBookmarks(node).ToString());
            }
            return RemoveAllBlankLines(sb);
        }

        private static StringBuilder RemoveAllBlankLines(StringBuilder sb)
        {
            return new StringBuilder().Append(Regex.Replace(sb.ToString(), @"^\s+$[\r\n]*", string.Empty, RegexOptions.Multiline));
        }

        private static StringBuilder PercorreBookmarks(Dictionary<string, object> bookmark)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(bookmark["Title"].ToString());
            if (bookmark != null && bookmark.ContainsKey("Kids"))
            {
                IList<Dictionary<string, object>> children = (IList<Dictionary<string, object>>) bookmark["Kids"];
                foreach (var bm in children)
                {
                    sb.AppendLine(PercorreBookmarks(bm).ToString());
                }
            }
            return sb;
        }
    }
}

1
投票

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

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

通常的免责声明适用


1
投票

You can use the PDFsharp library.它是根据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)
© www.soinside.com 2019 - 2024. All rights reserved.