国产PDF格式处理控件Spire.PDF功能演示:在Java中获取PDF 注释

Spire.PDF for Java 是一款专门对 PDF 文档进行操作的 Java 类库。该类库的主要功能在于帮助开发人员在 Java 应用程序(J2SE和J2EE)中生成 PDF 文档和操作现有 PDF 文档,并且运行环境无需安装 Adobe Acrobat。

点击此处下载最新版测试

从 PDF 文件中获取注释

Spire.PDF for Java 提供了PdfPageBase.getAnnotationsWidget()方法来获取文档指定页面的注释集合。

以下是从PDF文件的第一页获取所有注释的步骤:

  • 创建一个PdfDocument类的对象。
  • 使用PdfDocument.loadFromFile()方法加载示例 PDF 文档。
  • 创建一个 StringBuilder 对象。
  • 使用PdfPageBase.getAnnotationsWidget()方法获取文档第一页的注释集合。
  • 循环弹出注释,使用PdfAnnotation.getText() 方法从每个注释中提取数据后,然后使用StringBuilder.append()方法将数据附加到StringBuilder实例。
  • 使用Writer.write()方法将提取的数据写入 txt 文档。
import com.spire.pdf.*;import com.spire.pdf.annotations.*;import java.io.FileWriter;public class Test {    public static void main(String[] args) throws Exception {        //Create an object of PdfDocument class.        PdfDocument pdf = new PdfDocument();        //Load the sample PDF document        pdf.loadFromFile("Annotations.pdf");        //Get the annotation collection of the first page of the document.        PdfAnnotationCollection annotations = pdf.getPages().get(0).getAnnotationsWidget();        //Create a StringBuilder object        StringBuilder content = new StringBuilder();        //Traverse all the annotations        for (int i = 0; i < annotations.getCount(); i++) {            //If it is the pop-up annotations, continue              if (annotations.get(i) instanceof PdfPopupAnnotationWidget)              continue;                      //Get the annotations’ author                content.append("Annotation Author: " + annotations.get(i).getAuthor()+"n");                //Get the annotations’ text                content.append("Annotation Text: " + annotations.get(i).getText()+"n");                //Get the annotations’ modified date                String modifiedDate = annotations.get(i).getModifiedDate().toString();                content.append("Annotation ModifiedDate: " + modifiedDate+"n");                //Get the annotations’ name                content.append("Annotation Name: " + annotations.get(i).getName()+"n");                //Get the annotations’ location                content.append ("Annotation Location: " + annotations.get(i).getLocation()+"n");                }        //Write to a .txt file        FileWriter fw = new FileWriter("GetAnnotations.txt");        fw.write(content.toString());        fw.flush();        fw.close();        }    }

国产PDF格式处理控件Spire.PDF功能演示:在Java中获取PDF 注释

整合所有格式API处理套包Spire.office for Java正在 火热销售中!联系客服立马1分钟了解全部咨询!

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年9月19日
下一篇 2021年9月19日

相关推荐

发表回复

登录后才能评论