Home  >  Article  >  Java  >  How to add annotations to PDF using Java code

How to add annotations to PDF using Java code

王林
王林forward
2023-05-21 22:07:041758browse

Process

1. Use the Document class to load the PDF document.

2. Create a TextAnnotation object and add text annotations.

3. Set the title, theme and other comment attributes.

4. Use the Border class to set the annotated Border.

Use the Document.getPages().get_Item(int).getAnnotations().add(Annotation) method to add annotations to the document.

5. Use Document.save to save the updated PDF.

Example

// 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");

The above is the detailed content of How to add annotations to PDF using Java code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete