String inputFile = DATA_DIR.resolve("sample.pdf").toString();
String outputFile = DATA_DIR.resolve("java-add-annotation.pdf").toString();
Document pdfDocument = new Document(inputFile);
DefaultAppearance appearance = new DefaultAppearance();
appearance.setFontSize(12);
appearance.setFontName("Arial");
Page page = pdfDocument.getPages().get_Item(1);
FreeTextAnnotation freeTextAnnotation =
new FreeTextAnnotation(
page,
new Rectangle(299.988, 703.664, 508.708, 720.769),
appearance);
freeTextAnnotation.setContents("This is a free text annotation.");
freeTextAnnotation.setName("FreeText1");
freeTextAnnotation.setSubject("Revision 01");
freeTextAnnotation.setTitle("Free Text Annotation");
freeTextAnnotation.setPopup(new PopupAnnotation(page,
new Rectangle(299.988, 713.664, 308.708, 720.769)));
freeTextAnnotation.getPopup().setOpen(true);
page.getAnnotations().add(freeTextAnnotation);
pdfDocument.save(outputFile);
pdfDocument.close();