likes
comments
collection
share

如何使用Java在PDF文件中添加或删除注释?

作者站长头像
站长
· 阅读数 6

PDF文件中的注释用于详细说明内容。批注可以是注释,弹出窗口或图形对象,例如箭头,线条等。由于PDF文件不可编辑,因此批注可让您提供其他信息。在本文中,您将学习如何以编程方式使用PDF文件中的注释。特别是,您将学习如何使用Java在PDF文件中添加或删除注释。

  • 使用Java将注释添加到PDF
  • 使用Java从PDF中删除注释
    • 删除特殊注释
    • 删除所有注释

Aspose.PDF for Java是功能丰富的API,可让您使用Java生成,编辑和转换PDF文件。此外,该API允许您无缝处理各种PDF注释。感兴趣的朋友可点击下方按钮下载最新版。获取最新版

如何使用Java在PDF文件中添加或删除注释?

使用Java将注释添加到PDF

Java的Aspose.PDF支持许多注释,包括但不限于文本,线条,圆形,正方形,突出显示等。要使用每种注释类型,Java的Aspose.PDF提供了一个单独的类。例如,LineAnnotation 类用于添加行,而 HighlightAnnotation 类用于添加突出显示注释。

为了演示,我们将文本注释添加到PDF文件中。以下是与API引用一起使用Java向PDF添加文本注释的步骤。

  • 首先,使用Document类加载PDF文档。
  • 创建TextAnnotation类的对象以添加文本注释。
  • 设置注释的属性,例如标题,主题等。
  • 使用Border类设置注释的 Border。
  • 使用Document.getPages().get_Item(int).getAnnotations().add(Annotation) 方法将注释添加到文档中。
  • 最后,使用Document.save(string)方法保存更新的PDF 。

以下代码示例显示了如何使用Java将文本注释添加到PDF。

// Open the source PDF document
Document pdfDocument = new Document("input.pdf");

// Create annotation
TextAnnotation textAnnotation = new TextAnnotation(pdfDocument.getPages().get_Item(1), new com.aspose.pdf.Rectangle(200, 400, 400, 600));

// Set annotation title
textAnnotation.setTitle("Sample Annotation Title");

// Set annotation subject
textAnnotation.setSubject("Sample Subject");
textAnnotation.setState(AnnotationState.Accepted);

// Specify the annotation contents
textAnnotation.setContents("Sample contents for the annotation");
textAnnotation.setOpen(true);
textAnnotation.setIcon(TextIcon.Key);
Border border = new Border(textAnnotation);
border.setWidth(5);
border.setDash(new Dash(1, 1));
textAnnotation.setBorder(border);
textAnnotation.setRect(new com.aspose.pdf.Rectangle(200, 400, 400, 600));

// Add annotation in the annotations collection of the page
pdfDocument.getPages().get_Item(1).getAnnotations().add(textAnnotation);

// Save the output file
pdfDocument.save("output.pdf");

使用Java从PDF中删除注释

Java的Aspose.PDF支持许多注释,包括但不限于文本,线条,圆形,正方形,突出显示等。要使用每种注释类型,Java的Aspose.PDF提供了一个单独的类。例如,LineAnnotation 类用于添加行,而 HighlightAnnotation 类用于添加突出显示注释。

为了从PDF页面中删除注释,Aspose.PDF for Java提供了以下选项:

  • 删除页面上的特定注释
  • 删除页面上的所有注释

删除特定的PDF批注

以下是使用Java从PDF文件中删除注释的步骤。

  • 首先,使用Document类加载PDF文档。
  • 使用Document.getPages().get_Item(int).getAnnotations().delete(int)方法通过索引删除所需的注释。
  • 最后,使用Document.save(string)方法保存更新的PDF 。

以下代码示例显示了如何使用Java从PDF页面中删除特定注释。

// Open the source PDF document
Document pdfDocument = new Document("input.pdf");

// Delete particular annotation
pdfDocument.getPages().get_Item(1).getAnnotations().delete(1);

// Save the update document
pdfDocument.save("output.pdf");

删除所有PDF注释

以下是使用Java删除PDF页面上所有注释的步骤。

  • 首先,使用Document类加载PDF文档。
  • 使用Document.getPages().get_Item(int).getAnnotations().delete()方法删除所有注释。
  • 最后,使用Document.save(string)方法保存更新的PDF 。

以下代码示例显示了如何使用Java删除PDF页面上的所有注释。

// Open source PDF document
Document pdfDocument = new Document("input.pdf");

// Delete all annotation
pdfDocument.getPages().get_Item(1).getAnnotations().delete();

// Save the update document
pdfDocument.save("output.pdf");

如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。