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

查看:44
本文介绍了从 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.

我的问题是我正在维护一个系统,该系统在 iframe 中嵌入了 PDF,而 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.

提前致谢,斯科特

推荐答案

人们点击的链接是给定页面的/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天全站免登陆