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.

Fri Dec 05, 2014 9:31 pm

Code: Select all
public MemoryStream UpdatePdf(string filePath, IList<Field> fields)
        {
            var doc = new PdfDocument();
            doc.LoadFromFile(filePath);
            var mem = new MemoryStream();

            //var formWidget = (PdfFormWidget) doc.Form;
            var formWidget = doc.Form as PdfFormWidget;
            foreach (var pdfField in formWidget.FieldsWidget.List)
            {
                if (pdfField is PdfTextBoxFieldWidget)
                {
                    var textBoxField = pdfField as  PdfTextBoxFieldWidget;
                    foreach (var field in fields)
                    {
                        if (textBoxField.Name == field.FieldName) textBoxField.Text = field.FieldValue;
                    }
                }


                if (pdfField is PdfComboBoxWidgetFieldWidget)
                {
                    foreach (var field in fields.Where(w => w.DropDown))
                    {
                    var comboBoxField = pdfField as PdfComboBoxWidgetFieldWidget;
                        if (comboBoxField.Name == field.FieldName)
                        {
                           foreach (var dropItem in field.DropDownList)
                           {
                               comboBoxField.Values.Add(new PdfListWidgetItem(dropItem,dropItem));
                               int[] items = {0};
                               comboBoxField.SelectedIndex = items;
                           }
                        }
                    }
                }
            }
           doc.SaveToStream(mem);
            return mem;
        }
    }
}


The line of code below produces an Object refernce error. Can you please tell me what I've done wrong. Thanks
comboBoxField.Values.Add(new PdfListWidgetItem(dropItem,dropItem));

cn982mj
 
Posts: 3
Joined: Fri Dec 05, 2014 7:31 pm

Mon Dec 08, 2014 6:05 am

Dear cn982mj,

Sorry for the delay reply.

To add new item to combox, please refer to the code below:
Code: Select all
PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId);
comboBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
comboBoxField.BorderWidth = 0.75f;
comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
comboBoxField.Required = required;
//......
comboBoxField.Items.Add(new PdfListFieldItem(text, text));
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Mon Dec 08, 2014 1:07 pm

The code example you provided does not give an error but when I open the file the dropdown still contains nothing. Is there something else I need to do. Thanks

Code: Select all
public MemoryStream UpdatePdf(string filePath, IList<Field> fields)
        {
            var doc = new PdfDocument();
            doc.LoadFromFile(filePath);
            var mem = new MemoryStream();

            //var formWidget = (PdfFormWidget) doc.Form;
            var formWidget = doc.Form as PdfFormWidget;
            var pages = doc.Pages;
            foreach (PdfPageBase page in pages)
            {
                    foreach (var pdfField in formWidget.FieldsWidget.List)
                    {
                        if (pdfField is PdfTextBoxFieldWidget)
                        {
                            var textBoxField = pdfField as  PdfTextBoxFieldWidget;
                            foreach (var field in fields)
                            {
                                if (textBoxField.Name == field.FieldName) textBoxField.Text = field.FieldValue;
                            }
                        }


                        if (pdfField is PdfComboBoxWidgetFieldWidget)
                        {
                            foreach (var field in fields.Where(w => w.DropDown))
                            {
                                    var comboBoxField = new PdfComboBoxField(page, field.FieldName);
                                        if (comboBoxField.Name == field.FieldName)
                                        {
                                           foreach (var dropItem in field.DropDownList)
                                           {
                                               comboBoxField.Bounds = new RectangleF(0.0f,842f,225f,25f);
                                               comboBoxField.BorderWidth = 0.75f;
                                               comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica,9f);
                                               comboBoxField.Required = true;
                                               comboBoxField.Items.Add(new PdfListFieldItem(dropItem,dropItem));
                                           }
                                        }
                            }
                        }
                    }
            }
           doc.SaveToStream(mem);
            return mem;
        }
    }

cn982mj
 
Posts: 3
Joined: Fri Dec 05, 2014 7:31 pm

Tue Dec 09, 2014 2:34 am

Dear cn982mj,

Sorry that I made a mistake.

The solution I provided is to add a new combox to pdf document. But from your code snippet I found that you wanted to traverse all pdf form widgets and add items to combox, so the code snippet you sent at first time is correct.

BTW: I tried it with Spire.PDF Pack v3.2.0, and here is my code snippet:
Code: Select all
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("FormField.pdf");
PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
foreach (PdfPageBase page in pdf.Pages)
{
    for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
    {
        PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
        if (field is PdfComboBoxWidgetFieldWidget)
        {
            PdfComboBoxWidgetFieldWidget combobox = field as PdfComboBoxWidgetFieldWidget;
            combobox.Values.Add(new PdfListWidgetItem("Ish", "Ish"));
            combobox.SelectedIndex = new int[] { 0 };
        }
    }
}
pdf.SaveToFile("Result.pdf", FileFormat.PDF);
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Tue Dec 09, 2014 2:10 pm

OK,
I'm using the free version 3.0.26.56040

Whiich producing an error Object reference not set to an instance of an object.

Is there a fix for this? Thanks

cn982mj
 
Posts: 3
Joined: Fri Dec 05, 2014 7:31 pm

Wed Dec 10, 2014 1:42 am

Dear cn982mj,

After testing with Free Spire.PDF, I find there is no error about "Object reference not set to an instance of an object".
But the operation about filling form field has no effect, and I have posted it to our dev team, we will inform you if there is any update.
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Mon Apr 04, 2016 7:06 am

Free Spire.PDF for .NET Version:3.2 and Spire.PDF Pack Version:3.6

....
combobox.Values.Add(new PdfListWidgetItem("Ish", "Ish"));
....


it does not work. Error: Object reference not set to an instance of an object.

Is there a fix for this? Thanks!

dummy007
 
Posts: 1
Joined: Sun Apr 03, 2016 8:51 pm

Mon Apr 04, 2016 8:00 am

Hi,

Thanks for your posting.
To help us reproduce your issue, please share your code and pdf file. You can also send it to amy.zhao@e-iceblue.com.
Thank you.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Return to Spire.PDF