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.

Mon Feb 27, 2023 2:43 pm

Hi, We have been using Spire.PDF for .NET to allow us to populate already existing .pdfs. We get the .pdfs, open them and set the values for the different fields. We have always only had to populate Text fields so we used the PdTextBoxFieldWidget object. But there has been a change to one of the .pdfs and now they have 2 dropdown fields. (USA State abbreviations) The dropdowns are already populated and we just need to set the display to be the state we want. Just trying to figure out which component to use and how to set the selected value. Guessing we can use the PdfComboBoxWidgetFieldWidget and use one of it's calls like this: comboboxField.SetSelectedValue(stateabbrev); Let me know if that should work.

Thanks for any help you can provide.

Brian B

breslinb
 
Posts: 2
Joined: Fri May 06, 2016 8:09 pm

Tue Feb 28, 2023 9:05 am

Hi

Thanks for your inquiry.
Your idea is right, please see the following code for reference.
Code: Select all
 
            //Load a pdf document
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("test.pdf");

            //Get pdf forms
            PdfFormWidget formWidget = doc.Form as PdfFormWidget;
            //Find pdf form fields
            for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
            {
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
               
                if (field is PdfComboBoxWidgetFieldWidget)
                {
                    PdfComboBoxWidgetFieldWidget comBoxField = field as PdfComboBoxWidgetFieldWidget;
                    // This value must be one of the candidates in the drop-down box
                    comBoxField.SelectedValue= "dropdown2";
                } 
            }

            doc.SaveToFile("result.pdf");

If the code does not meet your requirement or you have further questions, just feel free to contact us.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Mar 13, 2023 12:22 pm

Thank you for the reply. I did try out coding using the example given. One problem is that we already have a .pdf and it is already loaded with the state values (NY, NJ, MA, RI, etc) If we are given a value not found in the list, which is possible outside of this code, what would be the best option to check and disallow that value? Currently an error is thrown due to the value being processed not being in the item list from the dropdown, so we need to test for that and not allow it to process and just leave the dropdown empty. Let me know what you think. Thanks!

It also looks like the .pdf we are working with doesn't have a blank space as one of the dropdown entries. So we should probably test for that before we try and set the value. I just wanted to see if there was a way to test if a value we are trying to set is in the list of values before making the call to set it and having it fail due to that value not being in the dropdown. Here is the error we got:

Host app generates error, from DD
System.AggregateException: One or more errors occurred. ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
at Spire.Pdf.Widget.PdfChoiceWidgetFieldWidget.SetSelectedValue(String[] values)
at Tyler.TMS.Business.Reports.NYS19AReportHelper.ProcessDS870(GetEmployeesForNYS19A_Result emp) in D:\a\transportation-traversa\transportation-traversa\Source\Tyler.TMS\Tyler.TMS.Business\Reports\NYS19AReportHelper.cs:line 462
at Tyler.TMS.Business.Reports.NYS19AReportHelper.Process(IEnumerable1 employees, IEnumerable1 reportIds, Dictionary2 commonFields) in D:\a\transportation-traversa\transportation-traversa\Source\Tyler.TMS\Tyler.TMS.Business\Reports\NYS19AReportHelper.cs:line 175 at Tyler.TMS.Business.Reports.ReportManager.ExecuteStateReports(StateReportModel model) in D:\a\transportation-traversa\transportation-traversa\Source\Tyler.TMS\Tyler.TMS.Business\Reports\ReportManager.cs:line 395 at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---

breslinb
 
Posts: 2
Joined: Fri May 06, 2016 8:09 pm

Wed Mar 15, 2023 9:09 am

Hi,

Thanks for your response.
For your requirement, I suggest trying out this approach, comBoxField.SelectedIndex = new int[] { 0 }; .
Here's a way to check if the given value is in the list of values:
Code: Select all
if (field is PdfComboBoxWidgetFieldWidget)
                {
                    PdfComboBoxWidgetFieldWidget comBoxField = field as PdfComboBoxWidgetFieldWidget;

                    foreach(PdfListWidgetItem item in comBoxField.Values)
                    {
                        if (item.Value.Equals(inputValue))
                        {
                           //...
                        }
                    }
                }


Please let us know if you have any questions.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF