Add Annotation to PDF

Add Text Annotation

To add an annotation:

  1. Open the input PDF.
  2. Create the annotation you want to add.
  3. Add the annotation to Annotations collection of the page you want to add it to.

The following code snippet shows how to add an annotation to a PDF page.

Cross, circle and star values are also added to the com.aspose.pdf.TextIcon enumeration.

Invisible Annotation

Sometimes, it is necessary to create a watermark that isn’t visible in the document when viewing it but should be visible when the document is printed. Use annotation flags for this purpose. The following code snippet shows how to accomplish this requirement.

Add Watermark Annotation

You can add Watermark Text using WatermarkAnnotation at specific position of PDF page, opacity of Watermark can also controlled by using opacity property. Please check following code snippet to add WatermarkAnnotation.

Java

 //Load a Document

com.aspose.pdf.Document doc = new com.aspose.pdf.Document(myDir+"HelloWorld.pdf");

//Load Page object to add Annotation

Page page = doc.getPages().get_Item(1);

//Create Annotation

WatermarkAnnotation wa = new WatermarkAnnotation(page, new com.aspose.pdf.Rectangle(100, 500, 400, 600));

//Add annotation into Annotation collection of Page

page.getAnnotations().add(wa);

//Create TextState for Font settings

com.aspose.pdf.TextState ts = new com.aspose.pdf.TextState();

ts.setForegroundColor(com.aspose.pdf.Color.getBlue());

ts.setFont(FontRepository.findFont("Times New Roman"));

ts.setFontSize(32);

//Set opacity level of Annotation Text

wa.setOpacity(0.1);

//Add Text in Annotation

wa.setTextAndState(new String[] { "HELLO", "Line 1", "Line 2" }, ts);

//Save the Document

doc.save(myDir+"Output.pdf");