Changing the author of an annotation

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

31st May 2016

Switch code for this example

    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV


 * This example was written by Bruno Lowagie in answer to the following question:
package com.itextpdf.samples.sandbox.interactive;

import com.itextpdf.kernel.pdf.*;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;

import org.junit.experimental.categories.Category;


public class ChangeAuthor extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/interactive/change_author.pdf";
    public static final String SRC = "./src/test/resources/pdfs/page229_annotations.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        new ChangeAuthor().manipulatePdf(DEST);

    public void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        PdfDictionary pageDict = pdfDoc.getFirstPage().getPdfObject();
        PdfArray annots = pageDict.getAsArray(PdfName.Annots);
        if (annots != null) {
            for (int i = 0; i < annots.size(); i++) {


    public void changeAuthor(PdfDictionary annotation) {
        if (annotation == null) {
        PdfString t = annotation.getAsString(PdfName.T);
        if (t == null) {
        if ("iText".equals(t.toString())) {
            annotation.put(PdfName.T, new PdfString("Bruno Lowagie"));

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