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 Oct 31, 2019 7:50 pm

While the PDF Form Filling with Spire.PDF seems quite simple, I am unsure why I cannot fill a simple PDF Form as follows.

I'm sure that I'm missing something very simple, but I am bewildered as to why I can save the PDF with or without form flattening completely as expected, yet the Forms.Fields.List.Count returns 0

//save example to C drive (taken from adobe website)

Code: Select all
using System;
using System.Collections.Generic;
using Spire.Pdf;
using Spire.Pdf.Widget;

namespace spireTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string formPath = @"E:\create-forms-sample.pdf";
            Dictionary<string, string> dict = new Dictionary<string, string>();
            FillPDFForm(formPath, dict);
            Console.ReadLine();
        }

        public static PdfDocument FillPDFForm(string sourcePDF,
            Dictionary<string, string> formResults)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(sourcePDF);
           
            PdfFormWidget formWidget = doc.Form as PdfFormWidget;

            Console.WriteLine("Form widget has length {0}",formWidget.Fields.Count);
            Console.WriteLine("Form has length {0}", doc.Form.Fields.List.Count);
            for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
            {
                //can't fill forms if widget field list is empty!
            }
           
            //doc.Form.IsFlatten = true;
            //doc.SaveToFile(@"C:\canFlattenAndSave.pdf"); //can flatten ok.
            return doc;
        }
    }
}

matthewgibb
 
Posts: 2
Joined: Wed Oct 30, 2019 1:01 pm

Fri Nov 01, 2019 7:51 am

Hi,

Thanks for your inquiry.
While loading an existing PDF file, you need to use formWidget.FieldsWidget.List.Count rather than doc.Form.Fields.List.Count to get the count of the form field.

And I could fill data into the form field in your PDF file successfully. Below is my full code for your reference.
Code: Select all
        static void Main(string[] args)
        {
            string formPath = @"……\create-forms-sample.pdf";
            Dictionary<string, string> dict = new Dictionary<string, string>();
            FillPDFForm(formPath, dict);
        }

            public static PdfDocument FillPDFForm(string sourcePDF, Dictionary<string, string> formResults)
            {
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(sourcePDF);

                PdfFormWidget formWidget = doc.Form as PdfFormWidget;

                for (int j = 0; j < formWidget.FieldsWidget.List.Count; j++)
                {
                    PdfField field = formWidget.FieldsWidget.List[j] as PdfField;
                   
                    if (field is PdfTextBoxFieldWidget)
                    {
                        PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;
                        switch (textBoxField.Name)
                        {
                            case "location":
                                textBoxField.Text = "test1 [email protected]";
                                break;
                            case "function":
                                textBoxField.Text = "test2 [email protected]";
                                break;
                            case "reason":
                                textBoxField.Text = "test3 [email protected]";
                                break;
                        }
                    }

                    if (field is PdfRadioButtonListFieldWidget)
                    {
                        PdfRadioButtonListFieldWidget radioBtnField = field as PdfRadioButtonListFieldWidget;
                        switch (radioBtnField.Name)
                        {
                            case "headcountAddition":
                                radioBtnField.SelectedIndex = 0;
                                break;
                            case "closeOffice":
                                radioBtnField.SelectedIndex = 1;
                                break;
                            case "headcountReduction":
                                radioBtnField.SelectedIndex = 0;
                                break;
                        }
                    }
                }

                //doc.Form.IsFlatten = true;
                doc.SaveToFile("result.pdf");
            return doc;
        }


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Fri Nov 01, 2019 1:43 pm

Yes -thank you, Amber! I was using Fields as opposed to FieldsWidget -thanks again for your prompt reply. Everything is working perfectly as expected.

matthewgibb
 
Posts: 2
Joined: Wed Oct 30, 2019 1:01 pm

Mon Nov 04, 2019 1:20 am

Hi,

Thanks for your feedback. Sorry to reply late for weekend.
Any question, welcome to contact us. Have a nice day.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF

cron