pteranodonjohn
pteranodonjohn

Reputation: 203

How to determine field type from a field in a PDF document using iTextSharp?

I'm experimenting with the iTextSharp library with C# and VisualStudio. I'm trying to obtain field names and field types (TextBox, RadioButton, ComboBox, CheckBox) from the AcroFields object.

Field names were easy to find but I'm struggling with field type. I've checked the iText javadoc because someone on here said the methods and functions should be similar in iTextSharp but have not found that to be the case.

Here's my code that get's the field names:

FormObject fo = new FormObject();
List<FormField> form_fields = new List<FormField>();

PdfReader reader = new PdfReader(file_name);
AcroFields reader_fields = reader.AcroFields;

foreach (KeyValuePair<String, iTextSharp.text.pdf.AcroFields.Item> entry in reader_fields.Fields)
{
    FormField ff = new FormField();
    ff.Field_name = entry.Key.ToString();
    form_fields.Add(ff);
}

Any ideas on how I can extract the field type from the AcroFields object? I know it has to be in there somewhere...

Upvotes: 5

Views: 5284

Answers (1)

pteranodonjohn
pteranodonjohn

Reputation: 203

Was able to find field types this morning.

FormObject fo = new FormObject();
List<FormField> form_fields = new List<FormField>();

PdfReader reader = new PdfReader(file_name);
AcroFields reader_fields = reader.AcroFields;



foreach (KeyValuePair<String, iTextSharp.text.pdf.AcroFields.Item> entry in reader_fields.Fields)
{
    FormField ff = new FormField();
    ff.Field_name = entry.Key.ToString();
    int field_type = reader_fields.GetFieldType(entry.Key.ToString());
    form_fields.Add(ff);
}

Upvotes: 4

Related Questions