1

I'm working with iTextSharp on Visual Studio. I created a Pdfannotation like that:

PdfAnnotation annotation = PdfAnnotation.CreateText(stamper.Writer, rect, "Author", "I write my text", true, "New Paragraph");
stamper.AddAnnotation(annotation, 1);

So my annotation contents a text "I write my text". When I go on Adobe Acrobat Reader to open my pdf, and when I click on the button "Comment" to see all the comments I wrote on my pdf, I see my comment and near to my comment "I write my text", I see a little square that I can check or uncheck. This is a little Checkbox which was automatically created when I created my Pdf annotation. I didn't create it by myself.

I would like to check or uncheck this little checkbox using iTextSharp.

I thought about doing that, but it doesn't work :

  RadioCheckField checkbox = new RadioCheckField(stamper.Writer, rect, "bonjour", "on");
        checkbox.CheckType = RadioCheckField.TYPE_CHECK;
        checkbox.Checked = true;
        PdfFormField field = checkbox.CheckField;

        annotation.Put(PdfName.A, field);

Does anyone know how to do it ?

Thank you a lot!

Have a good day! :)

Caryluna
  • 87
  • 1
  • 1
  • 7
  • You are creating a text annotation. You can't check or uncheck a text annotation. Please show us how you're creating a check box. – Bruno Lowagie Jun 06 '16 at 08:55
  • If you look on Adobe Acrobat Reader, you'll see that after adding you're annotation you can choose to check it or uncheak it. I can't add a picture, the website does'nt allow me to (because I'm new on it). – Caryluna Jun 06 '16 at 09:13
  • Your question is inaccurate. You are creating a text annotation (aka sticky note). You can change the representation to a check mark as shown [here](http://itextpdf.org/documents/stickynote.png), but that's not what you're asking. You are asking to check/uncheck a check box. A check box is a form field represented by a widget annotation, *not* a text annotation. Be more accurate! – Bruno Lowagie Jun 06 '16 at 09:29
  • On the picture you are showing to me, in the column "Comments Lists" I see your name "Bruno" and on the right of that i see a little square, that's the checkbox I'm speaking about, and that I would like to check with itextSharp – Caryluna Jun 06 '16 at 09:52
  • Do you know the meaning of that check box? Please explain! – Bruno Lowagie Jun 06 '16 at 10:21
  • In my office they use it as "this annotation is approved", so I would like to know how to check or uncheck this "checkbox" with iTextSharp. Do you know how to do it ? – Caryluna Jun 06 '16 at 10:36
  • Set the check box using Acrobat. Compare the PDF with the unchecked annotation with the PDF with the checked annotation using RUPS. Update the annotation accordingly. – Bruno Lowagie Jun 06 '16 at 10:37

1 Answers1

0

The question was somewhat confusion because of the terminology "check/uncheck" in the context of a text (or Sticky Note) annotation. The correct terminology would have been: How to I mark/unmark a text annotation?

Checking/unchecking immediately makes us think about check boxes, but the following screen shot shows what is meant when we talk about marking a text annotation:

enter image description here

Marking a text annotation isn't a matter of checking a check box. Marking a text annotation is done by adding a hidden "In Reply To" (IRT) annotation. See How to add an "In Reply To" annotation? on the official site for more information about "In Reply To" annotations.

I've adapted the AddInReplyTo example with the AddMarked as result:

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);
    PdfDictionary popup = annots.getAsDict(1);
    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, "Bruno", "Marked set by Bruno", false, "Comment");
    replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
    replySticky.put(PdfName.STATE, new PdfString("Marked"));
    PdfNumber n = sticky.getAsNumber(PdfName.F);
    replySticky.put(PdfName.F, new PdfNumber(n.intValue() | PdfAnnotation.FLAGS_HIDDEN));
    replySticky.put(new PdfName("StateModel"), new PdfString("Marked"));
    stamper.addAnnotation(replySticky, 1);
    stamper.close();
}

The example is in Java, but it should be fairly easy to adapt it to C#. It's important to know that marking the original annotation sticky is done by adding an extra annotation replySticky. The difference with an ordinary IRT annotation, is that we are going to hide the annotation by adding FLAGS_HIDDEN to the flags of the annotation. We also set the /State to Marked and the /StateModel to Marked.

This code turns hello_sticky_note.pdf into hello_marked.pdf as was requested, but there's a catch! The check box will only be visible if you are logged in as user "Bruno". This check box is for personal use only.

If you want others to see a review status, you shouldn't use the "Marked" feature. Instead you should use "Review". This is poorly documented in ISO-32000. See the table with title "Additional entries specific to a text annotation":

enter image description here

This table refers to the table with title "Annotation States":

enter image description here

We have used the combination StateModel = Marked; State = Marked, which means that the annotation has been marked by the user. I didn't find any reference in the spec that this mark is only visible on the machine of the user who marked the document.

After discovering this, I've created the AddAccepted example:

PdfAnnotation replySticky = PdfAnnotation.createText(
        writer, stickyRectangle, "Bruno", "Accepted by Bruno", false, "Comment");
replySticky.put(PdfName.IRT, annots.getAsIndirectObject(0));
replySticky.put(PdfName.STATE, new PdfString("Accepted"));
PdfNumber n = sticky.getAsNumber(PdfName.F);
replySticky.put(PdfName.F, new PdfNumber(n.intValue() | PdfAnnotation.FLAGS_HIDDEN));
replySticky.put(new PdfName("StateModel"), new PdfString("Review"));
stamper.addAnnotation(replySticky, 1);
stamper.close();

This example is identical to what we had before, except that we now use the combination: StateModel = Review; State = Accepted. As you can tell from the "Annotation states" table, other possible options for the State are "Rejected", "Cancelled", "Completed" and "None" (which is the default value).

The result looks like this:

enter image description here

As you can see, a green check mark appears in the comments panel. It shows "Bruno Accepted" on a computer where the logged in user isn't Bruno. You can check this for yourself here: hello_accepted.pdf

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Than you for your answer! Why do you use this line : replySticky.Put(PdfName.IRT, annots.GetAsIndirectObject(0)); Because when I write it, I don't see my comment answer "Marked set by Bruno" anymore. And when I don't write it, my answer is here but the problem persists : my answer is not marked. – Caryluna Jun 06 '16 at 13:53
  • Don't copy my example literally. If you remove the `IRT` key, you don't understand my answer. `IRT` stands for **In Reply To.** The value of this entry is a reference to the annotation that needs to be marked. In my simple example, it's easy: there's only one annotation, so I can take the first one `annots.getAsIndirectObject(0)`. In your case, you have to change `0` into the index of the annotation you want to mark. Start by trying the example on hello_sticky_note.pdf. Examine what happens and adapt the simple example to match your needs. – Bruno Lowagie Jun 06 '16 at 14:01
  • I copied your code with your pdf example hello_sticky_note.pdf and it doesn't work. The answer "Marked set by Bruno" doesn't appear and the mark doesn't too – Caryluna Jun 06 '16 at 14:28
  • The answer "Marked set by Bruno" should not appear: it is `HIDDEN`! The mark should appear and appears (when done correctly). When I run the code in Java, it works as expected. I don't know what you're doing wrong, but it works for me. This is my proof: http://gitlab.itextsupport.com/itext/sandbox/raw/master/cmpfiles/annotations/cmp_hello_marked.pdf – Bruno Lowagie Jun 06 '16 at 15:10
  • I save the proof and I open it with Adobe Reader but I don't see any mark – Caryluna Jun 06 '16 at 15:39
  • That's because you are logged in as a different user. That check mark only works on the machine of the user that checked it. Maybe your question is still wrong. Maybe you don't want to *mark* the annotation. Maybe you want to *review* the annotation. I've updated my answer. The next time you ask a question, you should be more helpful by being more clear. I've spent several hundreds of dollars worth of my time on your question. – Bruno Lowagie Jun 06 '16 at 17:17
  • Thank you a lot! I tried your new code with the “Review” features and it’s perfect. I didn’t know that the mark was only visible by the user who wrote it, so the “Review” features are perfectly adapted for what I want to do. Thank you again ! – Caryluna Jun 08 '16 at 13:12