2

OK, here's the scoop. I have an almost completely locked-down (view only) PDF from the post office. I must create a web application where a user can type in a bookmark name that will bring up the correct page in the PDF. I know that there is no way to programmatically jump to a particular bookmark. There are no named destinations in the document and no way to create them. Is there a way to programmatically extract page numbers from the bookmarks in a PDF document via JavaScript? Can I loop through the bookmarks to find the right one and read the page number? Why should this be so blasted difficult?

4

1 回答 1

1

我的公司Atalasoft制作了一个Ajax/asp.net 控件,它可以做你想做的事。它可以让您显示 PDF,在服务器端,拉出书签和目的地并将它们发送到客户端控件并不难。

本质上,您正在用基于图像的查看器替换 Acrobat(或其他)查​​看器,该查看器在服务器端已经剖析了 PDF 并可以告诉查看器去哪里。

这是一些用于提取书签的示例代码 - 此代码看起来比您想象的要复杂,这是因为 PDF 中的书签是一棵完整的树。

delegate void BookmarkReceiver(PdfBookmark bookmark);

public void VisitBookmarks(PdfBookmarkList bookmarks, BookmarkReceiver receiever)
{
    foreach (PdfBookmark mark in bookmarks)
    {
        receiver(mark);
        VisitBookmarks(mark.Children);
    }
}

// here's a matcher:

IList<PdfBookmark> FindBookmarksWithName(string s, PdfBookmarkTree tree)
{
    List<PdfBookmark> marks = new List<PdfBookmark>();
    VisitBookmarks(tree.Bookmarks, mark => if (mark.Text == s) marks.Add(mark));
    return marks;
}

当然,如果您只想找到一个谓词,可以修改访问者以采用谓词和短路。

现在要弄清楚如果你有一个特定的书签你应该去哪个页面并不像拉出页面索引那么简单。这是因为书签不必转到特定页面。书签有一个与之关联的动作,并且该动作可能是单击书签时要做的事情的列表。例如,我可以创建一个单击操作,它是两个操作的列表,首先交换第一页和最后一页,然后转到第一页。

因此,您要做的是获取单击操作并遍历列表并尝试找到“转到查看”操作,然后将目标标识为页面索引并抓取它。

是的,这比您想象的要多得多,因为 PDF 的指定比它需要的更丰富。目标页面可以是索引页面,但也可以是本地文件系统上不同文档中的页面,因此我们确实需要检查每一步都有什么。

于 2013-03-20T13:56:01.037 回答