使用 ITextSharp 编辑 PDF 中的超链接和锚点 [英] Editing Hyperlink and Anchors in PDF using ITextSharp

查看:38
本文介绍了使用 ITextSharp 编辑 PDF 中的超链接和锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 iTextSharp 库和 C#.Net 来拆分我的 PDF 文件.

考虑一个名为 sample.pdf 的 PDF 文件,其中包含 72 页.此 sample.pdf 包含具有导航到其他页面的超链接的页面.例如:在第 4 页中有三个超链接,点击后会导航到相应的第 24、27、28 页.与第 4 页相同,有近 12 个页面与它们有此超链接.

现在使用 iTextSharp 库,我已将此 PDF 页面拆分为 72 个单独的文件,并以 1.pdf,2.pdf....72.pdf 的名称保存.因此,在 4.pdf 中单击该超链接时,我需要使 PDF 导航到 24.pdf、27.pdf、28.pdf.

请帮帮我.如何编辑和设置 4.pdf 中的超链接,以便导航到相应的 pdf 文件.

谢谢,阿肖克

解决方案

你想要的完全有可能.您需要使用低级 PDF 对象(PdfDictionary、PdfArray 等).

每当有人需要处理这些对象时,我总是让他们参考 PDF 参考.就您而言,您需要查看第 7 章(特别是第 3 节)和第 12 章第 3 节(文档级导航)和第 5 节(注释).

假设您已经阅读了该内容,那么您需要执行以下操作:

  1. 遍历每个页面的注释数组(在原始文档中,在分解之前).

    1. 查找所有链接注释和他们的目的地.
    2. 为与新文件对应的链接构建一个新目标.
    3. 将该新目的地写入链接注释中.

  2. 使用 PdfCopy 将此页面写入新的 PDF(它将复制注释和页面内容).

步骤 1.1 并不简单.有几种不同类型的本地转到"注释格式.您需要确定给定链接指向哪个页面.一些链接可能会说 PDF 等效于下一页"或上一页",而其他链接会包含对特定页面的引用.这将是间接对象引用",而不是页码.

要从页面参考中确定页码,您需要...哎哟.好的.最有效的方法是为原始文档中的每个页面调用 PdfReader.GetPageRef(int pageNum) 并将其缓存在地图中(reference->pageNum).

然后,您可以通过创建远程转到 PdfAction 并将其写入链接注释的A"(操作)条目,删除之前存在的任何内容(可能是Dest")来构建远程转到"链接.

我不会说 C#,所以我会把实际的实现留给你.

I am using iTextSharp library and C#.Net for splitting my PDF file.

Consider a PDF file named sample.pdf containing 72 pages. This sample.pdf contains pages that have hyperlink that navigate to other page. Eg: In the page 4 there are three hyperlinks which when clicked navigates to corresponding 24th,27th,28th page. As same as the 4th page there are nearly 12 pages that is having this hyperlinks with them.

Now using iTextSharp library I had split this PDF pages into 72 separate file and saved with the name as 1.pdf,2.pdf....72.pdf. So in the 4.pdf when clicking that hyperlinks I need to make the PDF navigate to 24.pdf,27.pdf,28.pdf.

Please help me out here. How can I edit and set the hyperlinks in the 4.pdf so that it navigates to corresponding pdf files.

Thank you, Ashok

解决方案

What you want is quite possible. What you want will require you to work with the low-level PDF objects (PdfDictionary, PdfArray, etc).

And whenever someone needs to work with those objects, I always refer them to the PDF Reference. In your case, you'll want to examine chapter 7 (particularly section 3) and chapter 12, sections 3 (doc-level navigation) and 5 (annotations).

Assuming you've read that, here's what you need to do:

  1. Step through the annotation array of each page (in the original doc, before breaking it up).

    1. Find all the link annotations & their destinations.
    2. Build a new destination for that link corresponding to the new file.
    3. write that new destination into the link annotation.

  2. Write this page into a new PDF using PdfCopy (it'll copy the annotations as well as the page content).

Step 1.1 isn't simple. There are several different kinds of "local goto" annotation formats. You need to determine which page a given link points to. Some links might say the PDF equivalent of "next page" or "previous page", while others will include a reference to a particular page. This will be an "indirect object reference", not a page number.

To determine the page number from a page reference, you need to... ouch. Okay. The most efficient way would be to call PdfReader.GetPageRef(int pageNum) for each page in the original document and cache it in a map (reference->pageNum).

You can then build "remote goto" links by creating a remote goto PdfAction, and writing that into the link annotation's "A" (action) entry, removing anything that was there before (probably a "Dest").

I don't speak C# very well, so I'll leave the actual implementation to you.

这篇关于使用 ITextSharp 编辑 PDF 中的超链接和锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆