Changing properties of an annotation

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

9th November 2015

Switch code for this example
 * This example was written by Bruno Lowagie in answer to the following question:
package sandbox.annotations;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfRectangle;
import com.itextpdf.text.pdf.PdfStamper;
import sandbox.WrapToTest;

 * @author iText
public class MovePopup {
    public static final String SRC = "resources/pdfs/hello_sticky_note.pdf";
    public static final String DEST = "results/annotations/hello_sticky_moved.pdf";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        new MovePopup().manipulatePdf(SRC, DEST);
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfDictionary page = reader.getPageN(1);
        PdfArray annots = page.getAsArray(PdfName.ANNOTS);
        PdfDictionary sticky = annots.getAsDict(0);
        PdfArray stickyRect = sticky.getAsArray(PdfName.RECT);
        PdfRectangle stickyRectangle = new PdfRectangle(
            stickyRect.getAsNumber(0).floatValue() - 120, stickyRect.getAsNumber(1).floatValue() -70,
            stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue() - 30
        sticky.put(PdfName.RECT, stickyRectangle);
        PdfDictionary popup = annots.getAsDict(1);
        PdfArray popupRect = popup.getAsArray(PdfName.RECT);
        PdfRectangle popupRectangle = new PdfRectangle(
            popupRect.getAsNumber(0).floatValue() - 250, popupRect.getAsNumber(1).floatValue(),
            popupRect.getAsNumber(2).floatValue(), popupRect.getAsNumber(3).floatValue() - 250
        popup.put(PdfName.RECT, popupRectangle);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

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