How to add an In Reply To annotation?
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
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 {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfPage page = pdfDoc.getFirstPage();
List annots = page.getAnnotations();
PdfDictionary sticky = annots.get(0).getPdfObject();
Rectangle stickyRectangle = sticky.getAsArray(PdfName.Rect).toRectangle();
PdfAnnotation replySticky = new PdfTextAnnotation(stickyRectangle)
.setIconName(new PdfName("Comment"))
.setInReplyTo(annots.get(0))
.setText(new PdfString("Reply"))
.setContents("Hello PDF")
.setOpen(true);
pdfDoc.getFirstPage().addAnnotation(replySticky);
pdfDoc.close();
}
I get the original annotation (in my code, it's named sticky
) and I get the position of that annotation. I create a stickyRectangle
object and I use that stickyRectangle
to create a new PdfTextAnnotation
named replySticky
. That's what you already have. Now I add the missing part, that is the reference that you were asking for:
replySticky.setInReplyTo(annots.get(0));
The resulting PDF looks like hello_in_reply_to.pdf:
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.
Click this link if you want to see how to answer this question in iText 5.