Adding Annotations to existing PDF file
Add Free Text Annotation in an Existing PDF File (facades)
A free text annotation displays text directly on the page. PdfContentEditor allows you to add annotations of different types in an existing PDF file. You can use the respective method to add a particular annotation. For example, in the following code snippet, we have used createFreeText method to add FreeText type annotation.
Any type of annotations can be added to the PDF file in the same way. First of all, you need to create an object of type PdfContentEditor and bind input PDF file using bindPdf method. Secondly, you have to create a Rectangle object to specify the area of the annotation. After that, you can call createFreeText method to add FreeText annotation, the page number on which the annotation is located, and then use save method to save the updated PDF file.
The following code snippet shows you how to add free text annotation in a PDF file.
public static void AddFreeTextAnnotation()
{
var document = new Document(_dataDir + "sample.pdf");
PdfContentEditor editor = new PdfContentEditor(document);
TextFragmentAbsorber tfa = new TextFragmentAbsorber("PDF");
tfa.visit(document.getPages().get_Item(1));
java.awt.Rectangle rect = new java.awt.Rectangle();
rect.x = (int)tfa.getTextFragments().get_Item(1).getRectangle().getLLX();
rect.y = (int)tfa.getTextFragments().get_Item(1).getRectangle().getURY() + 5;
rect.height = 18;
rect.width = 100;
editor.createFreeText(rect, "Free Text Demo", 1); // last param is a page number
editor.save(_dataDir + "PdfContentEditorDemo_FreeTextAnnotation.pdf");
}
Add Text Annotation in an Existing PDF File (facades)
In this example also, you need to create an object of type PdfContentEditor and bind input PDF file using bindPdf method. Secondly, you have to create a Rectangle object to specify the area of the annotation. After that, you can call createFreeText method to add FreeText annotation, create title of your annotations, and the page number on which the annotation is located.
public static void AddTextAnnotation()
{
var document = new Document(_dataDir + "sample.pdf");
PdfContentEditor editor = new PdfContentEditor(document);
TextFragmentAbsorber tfa = new TextFragmentAbsorber("PDF");
tfa.visit(document.getPages().get_Item(1));
java.awt.Rectangle rect = new java.awt.Rectangle();
rect.x = (int)tfa.getTextFragments().get_Item(1).getRectangle().getLLX();
rect.y = (int)tfa.getTextFragments().get_Item(1).getRectangle().getURY() + 5;
rect.height = 18;
rect.width = 100;
editor.createText(rect, "Aspose User", "PDF is a better format for modern documents", false, "Key", 1);
editor.save(_dataDir + "PdfContentEditorDemo_TextAnnotation.pdf");
}
Add Line Annotation in an Existing PDF File (facades)
We also specify the Rectangle, coordinates of the beginning and end of the line, page number, thickness, style and color of the annotation frame, type of line dash, type of start and end of the line.
public static void AddLineAnnotation()
{
var document = new Document(_dataDir + "Appartments.pdf");
PdfContentEditor editor = new PdfContentEditor(document);
// Create Line Annotation
editor.createLine(
new java.awt.Rectangle(550, 93, 562, 439),
"Test",
556, 99, 556, 443, 1, 1,
java.awt.Color.RED,
"dash",
new int[] { 1, 0, 3 },
new String[] { "Open", "Open" });
editor.save(_dataDir + "PdfContentEditorDemo_LineAnnotation.pdf");
}