且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除从PDF文档中的超链接(iTextSharp的)

更新时间:2023-02-21 20:22:48

的链接谢谢。人们点击是在一个给定页面的/ 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).