如何向 PDF 添加注释

如何向 PDF 添加注释

您在 PDF 文件中添加文本注释以总结文档中写入的内容并注意关键点和其他细节。但是,您可以在文档的任何位置添加文本注释,一旦创建,它就会显示为弹出窗口,并在关闭时显示为图标。在这篇博文中,您将学习如何使用 Java API 为 PDF 文件添加注释。此外,您将了解帮助您以编程方式在文件中添加文本注释的方法。

本指南将涵盖以下几点:

PDF 的 Java API - 安装

这个库的安装过程只是一步之遥。您可以 下载 API 或使用以下 Maven 配置安装它。

 <repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <classifier>jdk17</classifier>
</dependency>

如何在 PDF 中添加注释

现在,您可以利用此 PDF 库的方法以编程方式将文本注释添加到 PDF 文件。

请按照代码片段和下面提到的步骤进行操作:

  1. 实例化 Document 类的实例并加载源 PDF 文件。
  2. 通过调用 getItem 方法按索引获取页面。
  3. 初始化表示矩形的 Rectangle 类的对象。
  4. 通过创建 TextAnnotation 类的对象,在指定页面上创建新的文本注释。
  5. setTitle 方法设置应显示在注释标题栏中的文本。
  6. setSubject 方法设置表示对象描述的文本。
  7. setContents 方法设置注释文本。
  8. 用 textAnnotation 对象实例化 Border 对象的构造函数。
  9. 通过调用 setWidthsetDash 方法设置边框和虚线图案的宽度。
  10. 调用 add 方法,将注释添加到集合中。
  11. save 方法将保存注释文件。

将以下代码复制并粘贴到您的主文件中:

// 实例化 Document 类的实例并加载源 PDF 文件。
Document document = new Document(  "table.pdf");
// 通过调用 get_Item 方法按索引获取页面 
Page page = document.getPages().get_Item(1);
// 初始化一个表示矩形的 Rectangle 类的对象。
Rectangle rect = new Rectangle(200, 750, 400, 790);
// 通过创建 TextAnnotation 类的对象,在指定页面上创建新的 Text 注释。 
TextAnnotation textAnnotation = new TextAnnotation(page, rect);
// setTitle 方法设置应显示在注释标题栏中的文本。 
textAnnotation.setTitle("Aspose User");
// setSubject 方法设置表示对象描述的文本。 
textAnnotation.setSubject("Sample Subject");
// setContents 方法设置注释文本。 
textAnnotation.setContents("Sample contents for the annotation");
textAnnotation.setOpen(true);
textAnnotation.setIcon(TextIcon.Circle);
// 使用 textAnnotation 对象实例化边框对象的构造函数。 
Border border = new Border(textAnnotation);
// 通过调用 setWidth 和 setDash 方法设置边框和虚线图案的宽度。  
border.setWidth(5);
border.setDash(new Dash(1, 1));
textAnnotation.setBorder(border);
textAnnotation.setRect(rect);
// 调用将注释添加到集合的 add 方法。 
page.getAnnotations().add(textAnnotation);
// save 方法将保存注释文件。 
document.save(  "sample_textannot.pdf");

您可以在下图中看到输出:

文本注释

阅读文本注释

同样,我们可以按照以下步骤从现有 PDF 文件中读取文本注释:

  1. 创建 Document 类的实例并加载源 PDF 文件。
  2. 调用 getItem 方法按索引获取页面。
  3. 使用 AnnotationSelector 过滤注释。
  4. 调用 accept 方法接受访问者处理注解。
  5. 调用 getSelected 方法获取选中的 Annotation 对象列表。
  6. 打印结果。

将以下代码复制并粘贴到您的主文件中:

// 创建 Document 类的实例并加载源 PDF 文件。 
Document document = new Document(  "sample_textannot.pdf");
// 通过调用 get_Item 方法按索引获取页面   
Page page = document.getPages().get_Item(1);
// 使用 AnnotationSelector 过滤注释 
AnnotationSelector annotationSelector = new AnnotationSelector(
        new TextAnnotation(page, Rectangle.getTrivial()));
// 调用accept方法接受访问者处理注解。 
page.accept(annotationSelector);
//  调用 getSelected 方法获取选定 Annotation 对象的列表。 
List<Annotation> TextAnnotations = annotationSelector.getSelected();
// 打印结果
for (Annotation fa : TextAnnotations) {
    System.out.println(fa.getRect());
}

删除文本注释

此外,您可以按照以下步骤和代码片段以编程方式删除文本注释:

  1. 创建 Document 类的对象并加载源 PDF 文件。
  2. 使用 AnnotationSelector 过滤注释。
  3. 通过调用accept方法接受访问者处理注解。
  4. 调用 getSelected 方法获取选中的 Annotation 对象列表。
  5. 通过调用 delete 方法循环遍历所有文本注释并删除注释。
  6. 调用 save 方法保存文件。

将以下代码复制并粘贴到您的主文件中:

// 创建 Document 类的对象并加载源 PDF 文件。 
Document document = new Document(  "sample_textannot.pdf");
// 使用 AnnotationSelector 过滤注释 
Page page = document.getPages().get_Item(1);
AnnotationSelector annotationSelector = new AnnotationSelector(
        new TextAnnotation(page, Rectangle.getTrivial()));
// 通过调用accept方法接受访问者处理注解 
page.accept(annotationSelector);
// 调用 getSelected 方法获取选定 Annotation 对象的列表。 
List<Annotation> TextAnnotations = annotationSelector.getSelected();
// 遍历所有文本注释
for (Annotation fa : TextAnnotations) {
    // 调用delete方法删除注解 
}
// 调用 save 方法保存文件 
document.save ( "sample_textannot_del.pdf");

获得免费许可证

您可以使用 免费临时许可证 来试用 API,而不受评估限制。

加起来

这将我们带到了这篇博文的结尾。您已经了解了如何以编程方式在 PDF 文件中添加注释。此外,您还学习了如何使用此 Java API for PDF 从现有 PDF 文档中读取和删除文本注释。此外,您可以访问 文档 以进一步探索 API 方法。

此外,我们建议您遵循我们的 入门指南

最后,conholdate.com 正在撰写新的博客文章。因此,请保持联系以获取定期更新。

问一个问题

您可以在我们的 论坛 上告诉我们您的问题或疑问。

常见问题

我可以免费注释 PDF 吗?

请访问此 链接 以了解 Java API 为 PDF 公开的代码片段和 API 方法。

也可以看看