Skip to main content
Skip table of contents

Changing the author of an annotation

Example written in answer to the question Click How to change the author name for comments?


changeauthor

JAVA
JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.interactive;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class ChangeAuthor {
    public static final String DEST = "./target/sandbox/interactive/change_author.pdf";

    public static final String SRC = "./src/main/resources/pdfs/page229_annotations.pdf";

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

        new ChangeAuthor().manipulatePdf(DEST);
    }

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

        List<PdfAnnotation> pageAnnots = pdfDoc.getFirstPage().getAnnotations();
        for (PdfAnnotation annot : pageAnnots) {
            if (annot.getTitle() != null) {
                annot.setTitle(new PdfString("Bruno Lowagie"));
            }
        }

        pdfDoc.close();
    }
}


C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Annot;

namespace iText.Samples.Sandbox.Interactive
{
    public class ChangeAuthor
    {
        public static readonly String DEST = "results/sandbox/interactive/change_author.pdf";

        public static readonly String SRC = "../../../resources/pdfs/page229_annotations.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new ChangeAuthor().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

            IList<PdfAnnotation> pageAnnots = pdfDoc.GetFirstPage().GetAnnotations();
            foreach (PdfAnnotation annot in pageAnnots)
            {
                if (annot.GetTitle() != null)
                {
                    annot.SetTitle(new PdfString("Bruno Lowagie"));
                }
            }

            pdfDoc.Close();
        }
    }
}

Resources

page229_annotations.pdf

Results

cmp_change_author.pdf

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.