删除从PDF文档中的超链接(iTextSharp的) [英] Remove hyperlinks from a PDF document (iTextSharp)

查看:1040
本文介绍了删除从PDF文档中的超链接(iTextSharp的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用iTextSharp的(很新的产品),从PDF文档中删除超链接。有谁知道这是否可能?我一直在挖掘通过API,并没有发现明显的方式做到这一点。

I'm trying to leverage iTextSharp (very new to the product) to remove hyperlinks from a PDF document. Does anyone know if this is possible? I've been digging through the API and haven't found an obvious way to do this.

我的问题是,我有一个PDF文件系统上进行维护empbedded的iframe和PDF中的链接是造成用户最终浏览iframe中的网站,而不是在一个新窗口或选项卡中,所以我在寻找一种方式来杀死在请求时在PDF中的链接。

My problem is that I'm doing maintenance on a system that has PDFs empbedded in an iframe and the links within the PDF are causing users to end up browsing the site within the iframe rather than in a new window or tab so I'm looking for a way to kill the links in the PDF at request time.

在提前,
斯科特

Thanks in advance, Scott

推荐答案

的链接谢谢。人们点击是在一个给定页面的/ Annots阵列注释

The links people click on are annotations in a given page's /Annots array.

您有两种选择:


  1. 摧毁整个/ Annots阵列

  2. 搜索通过/ Annots阵列并删除所有链接注释

只要爆破注释阵列很简单:

Simply blasting the annotation array is easy:

 PdfDictionary pageDict = reader.getPageN(1); // 1st page is 1
 pageDict.remove(PdfName.ANNOTS);

 stamper.close();



现在的问题是,你可能会破坏你想与那些你不保持沿注解

The problem is that you might be destroying annotations that you want to keep along with those you don't.

解决方案是搜索ANNOT数组,寻找链接的URL。

The solution is to search the annot array looking for links to URLs.

PdfDictionary pageDict = reader.getPageN(1);
PdfArray annots = pageDict.getAsArray(PdfName.ANNOTS);
PdfArray newAnnots = new PdfArray();
if (annots != null) {
  for (int i = 0; i < annots.size(); ++i) {
    PdfDictionary annotDict = annots.getAsDict(i);
    if (!PdfName.LINK.equals(annotDict.getAsName(PdfName.SUBTYPE))) {
      // annots are actually listed as PdfIndirectReference's.  
      // Adding the dict directly would be A Bad Thing.
      newAnnots.add(annots.get(i));// get the original reference, not the dict
    }
  }
  pageDict.put(PdfName.ANNOTS, newAnnots);
}

这将删除全部链接注释,不只是那些链接到内部站点。如果你需要深入挖掘,你需要检查出的 PDF规格,部分12.5.6.5(链接注释)和部分12.6.4.7(URI操作)。

This will remove all link annotations, not just those that link to internal sites. If you need to dig deeper, you'll need to check out the PDF Spec, section 12.5.6.5 (link annotations) and section 12.6.4.7 (URI actions).

这篇关于删除从PDF文档中的超链接(iTextSharp的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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