Changing properties of an annotation

This is a code example of iText PDF, discover more.

9th November 2015
admin-marketing

Switch code for this example

MovePopup.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/33609564/how-to-modify-size-for-pdfannotations-in-itext-and-disable-some-options
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfArray;
  9. import com.itextpdf.text.pdf.PdfDictionary;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfRectangle;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  * @author iText
  21.  */
  22. @WrapToTest
  23. public class MovePopup {
  24.    
  25.     public static final String SRC = "resources/pdfs/hello_sticky_note.pdf";
  26.     public static final String DEST = "results/annotations/hello_sticky_moved.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new MovePopup().manipulatePdf(SRC, DEST);
  32.     }
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         PdfDictionary page = reader.getPageN(1);
  36.         PdfArray annots = page.getAsArray(PdfName.ANNOTS);
  37.         PdfDictionary sticky = annots.getAsDict(0);
  38.         PdfArray stickyRect = sticky.getAsArray(PdfName.RECT);
  39.         PdfRectangle stickyRectangle = new PdfRectangle(
  40.             stickyRect.getAsNumber(0).floatValue() - 120, stickyRect.getAsNumber(1).floatValue() -70,
  41.             stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue() - 30
  42.         );
  43.         sticky.put(PdfName.RECT, stickyRectangle);
  44.         PdfDictionary popup = annots.getAsDict(1);
  45.         PdfArray popupRect = popup.getAsArray(PdfName.RECT);
  46.         PdfRectangle popupRectangle = new PdfRectangle(
  47.             popupRect.getAsNumber(0).floatValue() - 250, popupRect.getAsNumber(1).floatValue(),
  48.             popupRect.getAsNumber(2).floatValue(), popupRect.getAsNumber(3).floatValue() - 250
  49.         );
  50.         popup.put(PdfName.RECT, popupRectangle);
  51.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  52.         stamper.close();
  53.     }
  54. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now