Annotation in reply to another annotation

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

31st May 2016
iText PDF

Switch code for this example

AddInReplyTo.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/28450668/how-to-add-in-reply-to-annotation-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  17. import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24. import java.util.List;
  25.  
  26. @Category(SampleTest.class)
  27. public class AddInReplyTo extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_in_reply_to.pdf";
  29.     public static final String SRC = "./src/test/resources/pdfs/hello_sticky_note.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new AddInReplyTo().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  40.         PdfPage page = pdfDoc.getFirstPage();
  41.         List annots = page.getAnnotations();
  42.         Rectangle stickyRectangle = annots.get(0).getRectangle().toRectangle();
  43.         PdfAnnotation replySticky = new PdfTextAnnotation(stickyRectangle)
  44.                 .setIconName(new PdfName("Comment"))
  45.                 .setInReplyTo(annots.get(0))
  46.                 .setText(new PdfString("Reply"))
  47.                 .setContents("Hello PDF")
  48.                 .setOpen(true);
  49.         pdfDoc.getFirstPage().addAnnotation(replySticky);
  50.         pdfDoc.close();
  51.     }
  52. }
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