Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Jun 17, 2021 8:05 pm

Hello all,

We have a pdf that will be completed by the user and we need to save the values of each field in a database. How can I get the values for each field from C#? Is there a way to get all the fields and values?

Thanks!
Anahi

anahig15
 
Posts: 1
Joined: Thu Jun 17, 2021 7:46 pm

Fri Jun 18, 2021 2:46 am

Hello Anahi,

Thanks for your inquiry.
You can refer to the following code.
If you have any other question, please feel free to contact us.
Code: Select all
                             PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("test.pdf");
            PdfFormWidget formWidget = pdf.Form as PdfFormWidget;

            foreach (PdfStyledFieldWidget fieldWidget in formWidget.FieldsWidget.List)
            {
                //Get the value of textbox field
                if (fieldWidget is PdfTextBoxFieldWidget)
                {
                    PdfTextBoxFieldWidget textboxField = (PdfTextBoxFieldWidget)fieldWidget;
                    string value = textboxField.Text;
                }
                //Get the value of checkbox field
                if (fieldWidget is PdfCheckBoxWidgetFieldWidget)
                {
                    PdfCheckBoxWidgetFieldWidget checkboxField = (PdfCheckBoxWidgetFieldWidget)fieldWidget;
                    bool isChecked = checkboxField.Checked;
                }
                //Get value of radio button
                if (fieldWidget is PdfRadioButtonListFieldWidget)
                {
                    PdfRadioButtonListFieldWidget radioBtnField = (PdfRadioButtonListFieldWidget)fieldWidget;
                    string value = radioBtnField.Value;
                }
                //Get values of comboBox field
                if (fieldWidget is PdfComboBoxWidgetFieldWidget)
                {
                    PdfComboBoxWidgetFieldWidget comBoxField = (PdfComboBoxWidgetFieldWidget)fieldWidget;
                   
                    PdfListWidgetItemCollection items = comBoxField.Values;
                    //Get the value of items
                    foreach (PdfListWidgetItem item in items)
                    {
                       
                        string itemValue = item.Value;
                    }
                    //Get selected value
                    string selectedValue = comBoxField.SelectedValue;
                }
                //Get values of listBox field
                if (fieldWidget is PdfListBoxWidgetFieldWidget)
                {
                    PdfListBoxWidgetFieldWidget listBoxField = (PdfListBoxWidgetFieldWidget)fieldWidget;
                    PdfListWidgetItemCollection items = listBoxField.Values;
                    //Get the value of items
                    foreach (PdfListWidgetItem item in items)
                    {
                        string itemValue = item.Value;
                    }
                    //Get selected value
                    string selectedValue = listBoxField.SelectedValue;
                }
            }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Mon Jun 28, 2021 10:27 am

Hi Anahi,

Hope you are doing well.
Has the issue been solved now? If you could give us some feedback at your convenience, it would be greatly appreciated.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.PDF