Changing the author of an annotation

25th October 2015
admin-marketing

Switch code for this example

ChangeAuthor.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * https://www.linkedin.com/groups/Script-Change-Author-Name-Comments-159987.S.5984062085800144899
  4.  */
  5. package sandbox.interactive;
  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.PdfStamper;
  13. import com.itextpdf.text.pdf.PdfString;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ChangeAuthor {
  22.     public static final String SRC = "resources/pdfs/page229_annotations.pdf";
  23.     public static final String DEST = "results/interactive/page229_changed_author.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new ChangeAuthor().manipulatePdf(SRC, DEST);
  29.     }
  30.    
  31.    
  32.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  33.         PdfReader reader = new PdfReader(src);
  34.         PdfDictionary pageDict = reader.getPageN(1);
  35.         PdfArray annots = pageDict.getAsArray(PdfName.ANNOTS);
  36.         if (annots != null) {
  37.             PdfDictionary annot;
  38.             for (int i = 0; i < annots.size(); i++) {
  39.                 changeAuthor(annots.getAsDict(i));
  40.             }
  41.         }
  42.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  43.         stamper.close();
  44.     }
  45.    
  46.     public void changeAuthor(PdfDictionary annot) {
  47.         if (annot == null) return;
  48.         PdfString t = annot.getAsString(PdfName.T);
  49.         if (t == null) return;
  50.         if ("iText".equals(t.toString()))
  51.             annot.put(PdfName.T, new PdfString("Bruno Lowagie"));
  52.     }
  53. }
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