使用 ITextSharp 编辑 PDF 中的超链接和锚点
我正在使用 iTextSharp 库和 C#.Net 来分割我的 PDF 文件。
考虑一个名为 example.pdf 的 PDF 文件,包含 72 页。此sample.pdf 包含具有导航到其他页面的超链接的页面。例如:第 4 页有 3 个超链接,点击分别导航到相应的第 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 文件。
谢谢你, 阿肖克
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想要的东西是完全有可能的。您想要的将需要您使用低级 PDF 对象(PdfDictionary、PdfArray 等)。
每当有人需要使用这些对象时,我总是将他们参考 PDF 参考。对于您的情况,您需要检查第 7 章(特别是第 3 节)和第 12 章的第 3 节(文档级导航)和第 5 节(注释)。
假设您已经阅读了本文,那么您需要执行以下操作:
步骤 1.1 并不简单。有几种不同类型的“本地转到”注释格式。您需要确定给定链接指向哪个页面。有些链接可能会说 PDF 相当于“下一页”或“上一页”,而其他链接可能会包含对特定页面的引用。这将是“间接对象引用”,而不是页码。
要从页面引用确定页码,您需要......哎呀。好的。最有效的方法是为原始文档中的每个页面调用 PdfReader.GetPageRef(int pageNum) 并将其缓存在地图中(reference->pageNum)。
然后,您可以通过创建远程转到 PdfAction 并将其写入链接注释的“A”(操作)条目来构建“远程转到”链接,删除之前存在的任何内容(可能是“Dest”)。
我不太会说 C#,所以我将把实际的实现留给你。
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:
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.
好吧,基于 @Mark Storer 这里有一些起始代码。第一种方法创建一个包含 10 页的示例 PDF,并且第一页上有一些链接可以跳转到 PDF 的不同部分,以便我们可以使用一些内容。第二种方法打开第一种方法中创建的 PDF,并遍历每个注释,尝试找出注释链接到的页面并将其输出到 TRACE 窗口。代码采用 VB 编写,但如果需要,应该可以轻松转换为 C#。它的目标是 iTextSharp 5.1.1.0。
如果有机会,我可能会尝试更进一步,实际上拆分并重新链接事物,但我现在没有时间。
Alright, based on what @Mark Storer here's some starter code. The first method creates a sample PDF with 10 pages and some links on the first page that jump around to different parts of the PDF so we have something to work with. The second methods opens the PDF created in the first method and walks through each annotation trying to figure out which page the annotation links to and outputs it to the TRACE window. The code is in VB but should be easily converted to C# if needed. Its targetting iTextSharp 5.1.1.0.
If I get a chance I might try to take this further and actually split and re-link things but I don't have time right now.
下面的这个函数使用 iTextSharp 来:
第 4 步是在此处插入您想要的任何逻辑...更新链接、记录它们等。
好运气。
This function below uses iTextSharp to:
Step #4 is to insert whatever logic you want in here... update the links, log them, etc.
Good luck.