Question PDF with formfield

asmodi

New member
Joined
Sep 18, 2019
Messages
2
Programming Experience
1-3
Hello everybody,

I am fairly new in programming with C#.
I did some Java, Javascript and php before though.

I have a searchable PDF with a whole lot of formfields. Every formfield has an ID.
Let's say we don't know the names of the ID we just know they have an ID.

What I want to do is to loop through that PDF file and get the value from every formfield with an ID.

Is that possible?
Idealy the values get saved as a variable with the same name like the ID of the formfield.

At the moment I am very clueless tbh...

I hope someone can give me some advices.

With best regards
Asmodi
 
Unless your intent is generate code dynamically, then creating variables and setting them is going to be very hard since you are finding the ID at run time, but variables are declared at compile time. If this is truly your intent, look at the T4 templating engine that comes with the Visual Studio SDK.

Now if all you need is to set values in a dictionary where the IDs are the keys into the dictionary, then this will be relatively easy. The hardest part will be finding the IDs and values from the PDF.
 
It is possible to loop through the form fields in a PDF document and obtain each field name one by one. The following code snippet is based on a .NET PDF API called Spire.PDF. Since different form returns different "value", for instance, a text box form may have a "Text" value, a combo box may have a "SelectedIndex" value, you'll need to determine the form type before you can get the "value" of it. This part isn't included in following code, but you can learn something useful from this article.

Good luck.

C#:
//load PDF document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

//get form widget from it
PdfFormWidget formWidget = doc.Form as PdfFormWidget;

//loop through the widget collection
for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
{
    //get each field
    PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
   
    //get field name
    string fieldName = field.Name;                
}
 
Something like this with itext7 that you can install with Nuget:
C#:
PdfDocument doc = new PdfDocument(new PdfReader(@"C:\FormExample.pdf"));
var form = PdfAcroForm.GetAcroForm(doc, false);
foreach (var field in form.GetFormFields)
    Console.WriteLine($"Key: {field.Key}, Value: {field.Value.GetValueAsString}");
doc.Close();
 
Back
Top Bottom