从PDF文件中提取链接

从PDF文件中提取链接

链接在PDF文件中表示为注释,因此要提取链接,需要提取所有LinkAnnotation对象。

  1. 创建一个Document对象。

  2. 获取您想要从中提取链接的Page。

  3. 使用AnnotationSelector类从指定页面中提取所有LinkAnnotation对象。

  4. 将 AnnotationSelector 对象传递给 Page 对象的 Accept 方法。

  5. 使用 AnnotationSelector 对象的 getSelected 方法,将所有选定的链接注释获取到一个 IList 对象中。

以下代码片段向您展示如何从 PDF 文件中提取链接。

    public static void ExtractLinksFromThePDFFile() {        
        // 加载 PDF 文件
        Document document = new Document(_dataDir + "UpdateLinks.pdf");
        Page page = document.getPages().get_Item(1);
           
        AnnotationSelector selector = new AnnotationSelector(new LinkAnnotation(page, Rectangle.getTrivial()));
        page.accept(selector);
        java.util.List<Annotation> list = selector.getSelected();
        for(Annotation annot : list)
        {
            System.out.println("注释位置: " + annot.getRect());
        }
                
        // 保存具有更新链接的文档
        //document.save(_dataDir + "ExtractLinks_out.pdf");
    }