How to change the author name for comments?

Tags: annotationstext annotationauthoriText 5

Does anyone have a script which will change the name of the author whenever any markup such as underlining, highlighting, etc, is applied? You can do this manually through the properties box, but this is time consuming and difficult.

Posted on LinkedIn on Mar 19, 2015 by Paul Tredoux

I have created a small ChangeAuthor example that changes the author from "iText" to "Bruno Lowagie" using iText in Java.

It takes a PDF with annotations created by "iText" and turns it into a PDF with annotations created by "Bruno Lowagie".

The code is really simple: we loop over the annotations of a page and we change the T entry (if present and if equal to "iText"). We use PdfStamper to persist the changed PDF.

public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException { 
    PdfReader reader = new PdfReader(src); 
    PdfDictionary pageDict = reader.getPageN(1); 
    PdfArray annots = pageDict.getAsArray(PdfName.ANNOTS); 
    if (annots != null) { 
        PdfDictionary annot; 
        for (int i = 0; i < annots.size(); i++) { 
            changeAuthor(annots.getAsDict(i)); 
        } 
    } 
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); 
    stamper.close(); 
}
 
public void changeAuthor(PdfDictionary annot) { 
    if (annot == null) return; 
    PdfString t = annot.getAsString(PdfName.T); 
    if (t == null) return; 
    if ("iText".equals(t.toString())) 
        annot.put(PdfName.T, new PdfString("Bruno Lowagie")); 
}