Search code examples
c#-4.0pdfannotationsitextacrofields

Read Check Box, Radio Button Name and values from PDF using iText Sharp


I have a fillable PDF contains CheckBoxes and RadioButtons and TextBox.

How do i get the CheckBox Name and its value also how do we know that it is a checkbox / Radio Button?

i'm using iTextSharp and have look at my below code

 PdfReader pdfReader = new PdfReader(FileName);
 pdfReader.SelectPages("37");
        MemoryStream oStream = new MemoryStream();
        PdfStamper stamper = new PdfStamper(pdfReader, oStream);
        AcroFields form = stamper.AcroFields;
        if (form.Fields.Count() > 0)
        {
            IDictionary<string,AcroFields.Item> oDic= form.Fields;

            foreach (string FieldName in oDic.Keys)
            {
                //FieldName - CheckBox name; i need to confirm that is a Checkbox...
            }

            foreach (AcroFields.Item oItem in oDic.Values)
            {
                // how do we get check box values
            }
        }

Solution

  • The following code may help you out, if you still need it. It only works for AcroForms

    int BUTTON = 1;
    int CHECK_BOX = 2;
    int RADIO_BUTTON = 3;
    int TEXT_FIELD = 4;
    int LIST_BOX = 5;
    int COMBO_BOX = 6;
    
    PdfReader pdfReader = new PdfReader(path);
    AcroFields af = pdfReader.AcroFields;
    
    foreach (var field in af.Fields)
    {
        bool isRadio = RADIO_BUTTON == af.GetFieldType(field.Key));
    }
    

    Edit:

    Also, field.Key is the name of the field and field.Value is the value at it.

    For checkboxes, if(field.Value == "Yes") then it is selected... if it is anything else, it is not selected.

    Edit:

    And I just found how tro get Radio Button options, if you are needing them.

    myKey k = new myKey(field.Key, af.GetField(field.Key), af.GetFieldType(field.Key));
    if (k.isRadio())
    {
        try { k.options.AddRange(af.GetAppearanceStates(k.key)); }
        catch { }
    }
    Keys.Add(k);