PdfTarget extends PdfObjectWrapper<PdfDictionary> { public PdfTarget setAnnotation(PdfFileAttachmentAnnotation pdfAnnotation, PdfDocument pdfDocument) { PdfPage page = pdfAnnotation.getPage(); if (null == page) { throw new PdfException(PdfException.AnnotationShallHaveReferenceToPage); } else { put(PdfName.P, new PdfNumber(pdfDocument.getPageNumber(page) - 1)); int indexOfAnnotation = -1; final List<PdfAnnotation> annots = page.getAnnotations(); for (int i = 0; i < annots.size(); i++) { if (annots.get(i) != null && pdfAnnotation.getPdfObject().equals(annots.get(i).getPdfObject())) { indexOfAnnotation = i; break; } } put(PdfName.A, new PdfNumber(indexOfAnnotation)); } return this; } private  PdfTarget(PdfDictionary pdfObject); static PdfTarget create(PdfDictionary pdfObject); static PdfTarget createParentTarget(); static PdfTarget createChildTarget(); static PdfTarget createChildTarget(String embeddedFileName); static PdfTarget createChildTarget(String namedDestination, String annotationIdentifier); static PdfTarget createChildTarget(int pageNumber, int annotationIndex); PdfTarget setName(String name); String getName(); PdfTarget setAnnotation(PdfFileAttachmentAnnotation pdfAnnotation, PdfDocument pdfDocument); PdfFileAttachmentAnnotation getAnnotation(PdfDocument pdfDocument); PdfTarget setTarget(PdfTarget target); PdfTarget getTarget(); PdfTarget put(PdfName key, PdfObject value);  }
@Test public void setAnnotationTest() { try (PdfDocument document = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { PdfFileAttachmentAnnotation annotation0 = new PdfFileAttachmentAnnotation( new Rectangle(0,0, 20, 20)); PdfFileAttachmentAnnotation annotation1 = new PdfFileAttachmentAnnotation( new Rectangle(1,1, 21, 21)); PdfFileAttachmentAnnotation annotation2 = new PdfFileAttachmentAnnotation( new Rectangle(2,2, 22, 22)); document.addNewPage(); document.getPage(1).addAnnotation(annotation0); document.getPage(1).addAnnotation(annotation1); document.getPage(1).addAnnotation(annotation2); PdfTarget target = PdfTarget.create(new PdfDictionary()); target.setAnnotation(annotation2, document); PdfDictionary dictionary = target.getPdfObject(); Assert.assertEquals(0, dictionary.getAsNumber(PdfName.P).intValue()); Assert.assertEquals(2, dictionary.getAsNumber(PdfName.A).intValue()); } }