Changing properties of an annotation

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

31st May 2016
iText PDF

Switch code for this example

MovePopup.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/33609564/how-to-modify-size-for-pdfannotations-in-itext-and-disable-some-options
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.layout.Document;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23. @Category(SampleTest.class)
  24. public class MovePopup extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/annotations/move_popup.pdf";
  26.     public static final String SRC = "./src/test/resources/pdfs/hello_sticky_note.pdf";
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new MovePopup().manipulatePdf(DEST);
  32.     }
  33.  
  34.     @Override
  35.     protected void manipulatePdf(String dest) throws Exception {
  36.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  37.         Document doc = new Document(pdfDoc);
  38.  
  39.         PdfDictionary page = pdfDoc.getFirstPage().getPdfObject();
  40.         PdfArray annots = page.getAsArray(PdfName.Annots);
  41.  
  42.         PdfDictionary sticky = annots.getAsDictionary(0);
  43.         PdfArray stickyRect = sticky.getAsArray(PdfName.Rect);
  44.         PdfArray stickyRectangle = new PdfArray(new float[]{
  45.                 stickyRect.getAsNumber(0).floatValue() - 120, stickyRect.getAsNumber(1).floatValue() - 70,
  46.                 stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue() - 30
  47.         });
  48.         sticky.put(PdfName.Rect, stickyRectangle);
  49.  
  50.         PdfDictionary popup = annots.getAsDictionary(1);
  51.         PdfArray popupRect = popup.getAsArray(PdfName.Rect);
  52.         PdfArray popupRectangle = new PdfArray(new float[]{
  53.                 popupRect.getAsNumber(0).floatValue() - 250, popupRect.getAsNumber(1).floatValue(),
  54.                 popupRect.getAsNumber(2).floatValue(), popupRect.getAsNumber(3).floatValue() - 250
  55.         });
  56.         popup.put(PdfName.Rect, popupRectangle);
  57.  
  58.         doc.close();
  59.     }
  60. }
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