1

I am using ITextSharp to fill in my pdf, but I don't know how to check a checkbox.

I have different cases :

  1. I have just 1 box. If the value I get is "true", I check it, otherwise not.
  2. I have 2 boxes, "Yes" and "No". If the value is true, I check Yes, otherwise, I check No.
  3. I have multiple choice. For example, I have "Title", with possible values "Mr", "Mrs" and "Miss". I want to check the box corresponding to the value I get (I will get "Mr", or "Mrs", or "Miss".

Actually, this is what I do :

if (Output.AcroFields.Fields.ContainsKey(m.Item1))
{
    // Boolean or not (have to treat differently)
    if (Input.Data[m.Item2] is Boolean)
    {
        Output.AcroFields.SetField(m.Item1, (bool)Input.Data[m.Item2] ? "On" : "Off"); // I've tried "True", "On", "true"
    }
    else
    {
        Output.AcroFields.SetField(m.Item1, Input.Data[m.Item2].ToString());
    }
}

But checkboxes are never ticked. Text fields work, but not these 3 cases. Any idea ?

carndacier
  • 960
  • 15
  • 38

1 Answers1

2

The question is unclear. A check box can have two possible values. One of those values is always Off (that's defined in ISO-32000-1). The other value can be anything. You can find out the possible values using my answer to this question: Checking off pdf checkbox with itextsharp

I could interpret your question as: I want to create a check box with more than 2 states. That's impossible: you have an 'on' state and an 'off' state.

Maybe you are looking to create a Choice field. A Choice field is similar to a drop down box where you can select a value from a list.

Maybe you are looking for a series of check boxes:

[ ] Mr.
[ ] Mrs.
[ ] Ms.

In that case, you'd have three different check boxes. You could have /Yes as the value of each of those check boxes, but usually, you'll have something like /Mr, /Mrs and /Ms for the 'on' values.

If you have an existing PDF, you have to check which value applies using my answer to Checking off pdf checkbox with itextsharp

Update: You write I have 2 boxes, "Yes" and "No". If the value is true, I check Yes, otherwise, I check No. That doesn't really make sense. One check box would be sufficient. If you want to say Yes, it should be checked. If you want to say No, the value should be /Off (No is not an allowed value for the 'off' state).

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    I have an existing PDF already. I just want to populate it. I managed to populate textbox (firstname, lastname, etc.) but not the tickboxes (like title, are you minor ?, etc.). – carndacier Nov 06 '15 at 10:45
  • My answer to the question [Checking off pdf checkbox with itextsharp](http://stackoverflow.com/questions/19698771/checking-off-pdf-checkbox-with-itextsharp) is about inspecting **an existing PDF** to find out which values can be used. You should read my answer *and try it* before commenting on it. – Bruno Lowagie Nov 06 '15 at 10:47