通过 Java 管理 PDF 中的注释

管理 PDF 文档中的注释。使用 Aspose.PDF for Java 以编程方式修改 PDF 文件

如何使用 Java 库管理注释

为了添加文本注释,我们将使用 Aspose.PDF for Java API,这是一款功能丰富、功能强大且易于使用的适用于 Java 平台的转换 API。你可以直接从 Maven 下载它的最新版本,然后通过在 pom.xml 中添加以下配置将其安装在基于 Maven 的项目中。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

通过 Java 在 PDF 文档中创建注释


你需要 Aspose.PDF for Java 才能在你的环境中试用代码。

  1. 在 “文档” 类的实例中加载 PDF。
  2. 创建要添加到 PDF 的注释。
  3. 将注释添加到 Page 对象的 “注释” 集合中。
  4. 保存 PDF 文件。

PDF 文本注释-Java

Example: Java

Document document = new Document(_dataDir + "sample.pdf");
Page page = document.getPages().get_Item(1);
Rectangle rect = new Rectangle(200, 750, 400, 790);
TextAnnotation textAnnotation = new TextAnnotation(page, rect);

textAnnotation.setTitle("Aspose User");
textAnnotation.setSubject("Sample Subject");
textAnnotation.setState (AnnotationState.Accepted);
textAnnotation.setContents("Sample contents for the annotation");
textAnnotation.setOpen(true);
textAnnotation.setIcon(TextIcon.Circle);

Border border = new Border(textAnnotation);
border.setWidth(5);
border.setDash(new Dash(1, 1));
textAnnotation.setBorder(border);
textAnnotation.setRect(rect);

page.getAnnotations().add(textAnnotation);
document.save(_dataDir + "sample_textannot.pdf");