Annotation in reply to another annotation

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

6th November 2015
admin-marketing

Switch code for this example

AddInReplyTo.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/28450668/how-to-add-in-reply-to-annotation-using-itextsharp
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfAnnotation;
  10. import com.itextpdf.text.pdf.PdfArray;
  11. import com.itextpdf.text.pdf.PdfDictionary;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class AddInReplyTo {
  24.  
  25.     public static final String SRC = "resources/pdfs/hello_sticky_note.pdf";
  26.     public static final String DEST = "results/annotations/hello_in_reply_to.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddInReplyTo().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.         PdfDictionary popup = annots.getAsDict(1);
  40.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  41.         PdfWriter writer = stamper.getWriter();
  42.         Rectangle stickyRectangle = new Rectangle(
  43.             stickyRect.getAsNumber(0).floatValue(), stickyRect.getAsNumber(1).floatValue(),
  44.             stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue()
  45.         );
  46.         PdfAnnotation replySticky = PdfAnnotation.createText(
  47.                 writer, stickyRectangle, "Reply", "Hello PDF", true, "Comment");
  48.         replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
  49.         stamper.addAnnotation(replySticky, 1);
  50.         stamper.close();
  51.     }
  52. }
AddMarked.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/37652181
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfAnnotation;
  10. import com.itextpdf.text.pdf.PdfArray;
  11. import com.itextpdf.text.pdf.PdfDictionary;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfNumber;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16. import com.itextpdf.text.pdf.PdfString;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class AddMarked {
  26.  
  27.     public static final String SRC = "resources/pdfs/hello_sticky_note.pdf";
  28.     public static final String DEST = "results/annotations/hello_marked.pdf";
  29.  
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new AddMarked().manipulatePdf(SRC, DEST);
  34.     }
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfDictionary page = reader.getPageN(1);
  38.         PdfArray annots = page.getAsArray(PdfName.ANNOTS);
  39.         PdfDictionary sticky = annots.getAsDict(0);
  40.         PdfArray stickyRect = sticky.getAsArray(PdfName.RECT);
  41.         PdfDictionary popup = annots.getAsDict(1);
  42.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  43.         PdfWriter writer = stamper.getWriter();
  44.         Rectangle stickyRectangle = new Rectangle(
  45.             stickyRect.getAsNumber(0).floatValue(), stickyRect.getAsNumber(1).floatValue(),
  46.             stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue()
  47.         );
  48.         PdfAnnotation replySticky = PdfAnnotation.createText(
  49.                 writer, stickyRectangle, "Bruno", "Marked set by Bruno", false, "Comment");
  50.         replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
  51.         replySticky.put(PdfName.STATE, new PdfString("Marked"));
  52.         PdfNumber n = sticky.getAsNumber(PdfName.F);
  53.         replySticky.put(PdfName.F, new PdfNumber(n.intValue() | PdfAnnotation.FLAGS_HIDDEN));
  54.         replySticky.put(new PdfName("StateModel"), new PdfString("Marked"));
  55.         stamper.addAnnotation(replySticky, 1);
  56.         stamper.close();
  57.     }
  58. }
AddAccepted.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/37652181
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfAnnotation;
  10. import com.itextpdf.text.pdf.PdfArray;
  11. import com.itextpdf.text.pdf.PdfDictionary;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfNumber;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16. import com.itextpdf.text.pdf.PdfString;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class AddAccepted {
  26.  
  27.     public static final String SRC = "resources/pdfs/hello_sticky_note.pdf";
  28.     public static final String DEST = "results/annotations/hello_accepted.pdf";
  29.  
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new AddAccepted().manipulatePdf(SRC, DEST);
  34.     }
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfDictionary page = reader.getPageN(1);
  38.         PdfArray annots = page.getAsArray(PdfName.ANNOTS);
  39.         PdfDictionary sticky = annots.getAsDict(0);
  40.         PdfArray stickyRect = sticky.getAsArray(PdfName.RECT);
  41.         PdfDictionary popup = annots.getAsDict(1);
  42.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  43.         PdfWriter writer = stamper.getWriter();
  44.         Rectangle stickyRectangle = new Rectangle(
  45.             stickyRect.getAsNumber(0).floatValue(), stickyRect.getAsNumber(1).floatValue(),
  46.             stickyRect.getAsNumber(2).floatValue(), stickyRect.getAsNumber(3).floatValue()
  47.         );
  48.         PdfAnnotation replySticky = PdfAnnotation.createText(
  49.                 writer, stickyRectangle, "Bruno", "Accepted by Bruno", false, "Comment");
  50.         replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
  51.         replySticky.put(PdfName.STATE, new PdfString("Accepted"));
  52.         PdfNumber n = sticky.getAsNumber(PdfName.F);
  53.         replySticky.put(PdfName.F, new PdfNumber(n.intValue() | PdfAnnotation.FLAGS_HIDDEN));
  54.         replySticky.put(new PdfName("StateModel"), new PdfString("Review"));
  55.         stamper.addAnnotation(replySticky, 1);
  56.         stamper.close();
  57.     }
  58. }
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