public void addAnnotation(PdfAnnotation annot) { try { ArrayList allAnnots = new ArrayList(); if (annot.isForm()) { PdfFormField field = (PdfFormField) annot; if (field.getParent() != null) return; expandFields(field, allAnnots); if (cstp.fieldTemplates == null) cstp.fieldTemplates = new HashMap(); } else allAnnots.add(annot); for (int k = 0; k < allAnnots.size(); ++k) { annot = (PdfAnnotation) allAnnots.get(k); if (annot.isForm()) { if (!annot.isUsed()) { HashMap templates = annot.getTemplates(); if (templates != null) cstp.fieldTemplates.putAll(templates); } PdfFormField field = (PdfFormField) annot; if (field.getParent() == null) addDocumentField(field.getIndirectReference()); } if (annot.isAnnotation()) { PdfObject pdfobj = PdfReader.getPdfObject(pageN.get(PdfName.ANNOTS), pageN); PdfArray annots = null; if (pdfobj == null || !pdfobj.isArray()) { annots = new PdfArray(); pageN.put(PdfName.ANNOTS, annots); } else annots = (PdfArray) pdfobj; annots.add(annot.getIndirectReference()); if (!annot.isUsed()) { PdfRectangle rect = (PdfRectangle) annot.get(PdfName.RECT); if (rect != null && (rect.left() != 0 || rect.right() != 0 || rect.top() != 0 || rect.bottom() != 0)) { int rotation = reader.getPageRotation(pageN); Rectangle pageSize = reader.getPageSizeWithRotation(pageN); switch (rotation) { case 90: annot.put( PdfName.RECT, new PdfRectangle( pageSize.getTop() - rect.bottom(), rect.left(), pageSize.getTop() - rect.top(), rect.right())); break; case 180: annot.put( PdfName.RECT, new PdfRectangle( pageSize.getRight() - rect.left(), pageSize.getTop() - rect.bottom(), pageSize.getRight() - rect.right(), pageSize.getTop() - rect.top())); break; case 270: annot.put( PdfName.RECT, new PdfRectangle( rect.bottom(), pageSize.getRight() - rect.left(), rect.top(), pageSize.getRight() - rect.right())); break; } } } } if (!annot.isUsed()) { annot.setUsed(); cstp.addToBody(annot, annot.getIndirectReference()); } } } catch (IOException e) { throw new ExceptionConverter(e); } }
/** * A Hide action hides or shows an annotation. * * @param annot * @param hide * @return A Hide Action */ public static PdfAction createHide(PdfAnnotation annot, boolean hide) { return createHide(annot.getIndirectReference(), hide); }