How to add an "In Reply To" annotation?

Tags: annotationstext annotationIn reply to annotationiText 5

I am trying to add a sticky note reply to in pdf using iTextSharp. I am able to create a new annotation in the pdf. But I cannot link it as child of an already existing annotation. I copied most of the properties in parent to its child. I copied it by analyzing the properties of a reply, by manually adding a reply from Adobe Reader. What I am missing is the property /IRT. It needs a reference to the parent popup. Like /IRT 16 0 R. How do I find this reference?

Posted on StackOverflow on Feb 11, 2015 by Jose Tuttu

Please take a look at the AddInReplyTo example.

We have a file named hello_sticky_note.pdf that looks like this:

PDF with a sticky note
PDF with a sticky note

In my example, I know that this annotation is the first entry in the /Annots array (the annotation with index 0), so this is how I'm going to add an "in reply to" annotation:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfDictionary page = reader.getPageN(1);
    PdfArray annots = page.getAsArray(PdfName.ANNOTS);
    PdfDictionary sticky = annots.getAsDict(0);
    PdfArray stickyRect = sticky.getAsArray(PdfName.RECT);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfWriter writer = stamper.getWriter();
    Rectangle stickyRectangle = new Rectangle(
        stickyRect.getAsNumber(0).floatValue(),
        stickyRect.getAsNumber(1).floatValue(),
        stickyRect.getAsNumber(2).floatValue(),
        stickyRect.getAsNumber(3).floatValue()
    );
    PdfAnnotation replySticky = PdfAnnotation.createText(
            writer, stickyRectangle, "Reply", "Hello PDF", true, "Comment");
    replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
    stamper.addAnnotation(replySticky, 1);
    stamper.close();
}

I get the original annotation (in my code, it's named sticky) and I get the position of that annotation (stickyRect). I create a stickyRectangle object and I use that stickyRectangle to create a new PdfAnnotation named replySticky. That's what you already have. Now I add the missing part, that is the reference that you were asking for:

replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));

The resulting PDF looks like hello_in_reply_to.pdf:

PDF with a sticky note and a reply to this note
PDF with a sticky note and a reply to this note

The /NM property is missing for the reply. I thought, it would be generated automatically. Is it reliable to save NM property in an external DB?

The /NM entry is defined as follows:

(Optional) The annotation name, a text string uniquely identifying it among all the annotations on its page.

You can choose which ever String you want for the Reply as long as there is no other annotation with the same name on the page:

replySticky.put(PdfName.NM, new PdfString("MYID0123456789"));

You can save this name in an external DB along with the ID of the PDF and the page number of the page to which the annotation is added.