- 使用Java将注释添加到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 documentDocument pdfDocument = new Document("input.pdf");// Create annotationTextAnnotation textAnnotation = new TextAnnotation(pdfDocument.getPages().get_Item(1), new com.aspose.pdf.Rectangle(200, 400, 400, 600));// Set annotation titletextAnnotation.setTitle("Sample Annotation Title");// Set annotation subjecttextAnnotation.setSubject("Sample Subject");textAnnotation.setState(AnnotationState.Accepted);// Specify the annotation contentstextAnnotation.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 pagepdfDocument.getPages().get_Item(1).getAnnotations().add(textAnnotation);// Save the output filepdfDocument.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 documentDocument pdfDocument = new Document("input.pdf");// Delete particular annotationpdfDocument.getPages().get_Item(1).getAnnotations().delete(1);// Save the update documentpdfDocument.save("output.pdf");
删除所有PDF注释
以下是使用Java删除PDF页面上所有注释的步骤。
- 首先,使用Document类加载PDF文档。
- 使用Document.getPages().get_Item(int).getAnnotations().delete()方法删除所有注释。
- 最后,使用Document.save(string)方法保存更新的PDF 。
以下代码示例显示了如何使用Java删除PDF页面上的所有注释。
// Open source PDF documentDocument pdfDocument = new Document("input.pdf");// Delete all annotationpdfDocument.getPages().get_Item(1).getAnnotations().delete();// Save the update documentpdfDocument.save("output.pdf");
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!