Open and Fill Pre-existing PDF

Posted by: mchandler2 on 16 March 2020, 7:15 am EST

  • Posted 16 March 2020, 7:15 am EST

    I am evaluating Documents for PDF.

    I have successfully created a PDF "template" with Documents for PDF. I then opened this template, filled the form fields and saved it with a different name.

    However, our project requires that I be able to open a PDF template that was created with Adobe Acrobat, fill the form fields and save it. I have tried to do this with Documents for PDF, but it doesn't seem to recognize the form fields. There was not a fields collection to iterate through.

    Is Documents for PDF able to open a PDF that was created by Adobe Acrobat and iterate through the form fields and fill them out? If so, please direct me to a code example of this.

    Thank you for your help.
  • Replied 16 March 2020, 6:05 pm EST

    Hello Mary,

    To further investigate, we need your PDF file with which you are facing the issue. It will help us to assist you in a better way.

    Regards,
    Prabhat Sharma.
  • Replied 18 March 2020, 2:22 am EST

    Hi,

    Your form probably contains nested fields, to iterate through all fields in such a form you can use code similar to this:

    using (FileStream fs = new FileStream("Form.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    GcPdfDocument doc = new GcPdfDocument();
    doc.Load(fs);
    printFields(doc.AcroForm.Fields);
    return;

    void printFields(FieldCollection fields)
    {
    foreach (var f in fields)
    {
    Console.WriteLine($"Found field {f.Name}");
    printFields(f.Children);
    }
    }
    }

    Please let me know if this helps.

    Dmitry.
  • Replied 19 March 2020, 12:19 am EST

    Unfortunately, this did not help. However, one of your competitors has an easy to use product that works great.
  • Replied 19 March 2020, 1:35 am EST

    Could you explain in what way it did not help? I ran the code I posted on the actual PDF form that you sent to our support, and it did iterate through all the fields in it. So I'd very much like to understand what part of it did not help.

    Cheers.
  • Replied 19 March 2020, 2:07 am EST

    Please use the attached file and post the names of the fields in this PDF. When I run your code below I get the following results. These results are incorrect. I am curious to see the names of the fields that you say you can get.

    Dim doc = New GcPdfDocument()
    Using fs = New FileStream(msSampleFileName, FileMode.Open, FileAccess.Read)
    doc.Load(fs)
    For Each fld In doc.AcroForm.Fields
    ' Find the field name
    Debug.Print(fld.Name)
    For Each f In fld.Children
    Debug.Print(f.Name)
    Next
    Next
    doc.Clear()
    End Using

    This is result:
    topmostSubform[0]
    Page1[0]
    Page2[0]
  • Marked as Answer

    Replied 19 March 2020, 2:35 am EST

    Hi,

    Unlike the code I posted, your code only goes down one level. You need to recurse into those pages. Here is the complete code that enumerates all fields:


    Imports System
    Imports System.IO
    Imports GrapeCity.Documents.Pdf
    Imports GrapeCity.Documents.Pdf.AcroForms

    Module Program
    Sub Main(args As String())
    Using fs = New FileStream("SampleForm.pdf", FileMode.Open, FileAccess.Read, FileShare.Read)
    Dim doc = New GcPdfDocument()
    doc.Load(fs)
    EnumFields(doc.AcroForm.Fields)
    End Using
    End Sub

    Sub EnumFields(fields As FieldCollection)
    For Each f In fields
    Console.WriteLine(f.Name)
    EnumFields(f.Children)
    Next
    End Sub
    End Module


    and here's what it prints on your file:

    topmostSubform[0]
    Page1[0]
    f1_01[0]
    f1_02[0]
    f1_03[0]
    f1_04[0]
    f1_05[0]
    f1_06[0]
    f1_07[0]
    f1_08[0]
    c1_1[0]
    c1_1[1]
    f1_09[0]
    f1_10[0]
    f1_11[0]
    f1_12[0]
    f1_13[0]
    f1_14[0]
    f1_15[0]
    f1_16[0]
    f1_17[0]
    f1_18[0]
    f1_19[0]
    f1_20[0]
    f1_21[0]
    f1_22[0]
    Page2[0]
    c2_1[0]
    c2_1[1]
    f2_01[0]
    f2_02[0]
    f2_03[0]
    c2_2[0]
    c2_2[1]
    f2_04[0]
    f2_05[0]
    f2_06[0]
    f2_07[0]
    f2_08[0]
    f2_09[0]
    f2_10[0]
    f2_11[0]
    f2_12[0]
    f2_13[0]
    f2_14[0]
    f2_15[0]
    f2_16[0]
    f2_17[0]
    f2_18[0]
    f2_19[0]
    f2_20[0]
    f2_21[0]
    f2_22[0]
    f2_23[0]
    f2_24[0]
    f2_25[0]


    Hope this helps.
  • Replied 19 March 2020, 2:39 am EST

    Thank you.
  • Replied 19 March 2020, 2:42 am EST

    You're very welcome. I'll mark this as answered then if you don't mind. Please let me know if you have any other questions.

    Cheers.
  • Replied 19 March 2020, 2:57 am EST

    I cannot field the FieldCollection in GrapeCity.Documents. I checked the following namespaces:
    GrapeCity.Documents.Common
    GrapeCity.Documents.Drawing
    GrapeCity.Documents.Imaging
    GrapeCity.Documents.Pdf
    GrapeCity.Documents.Text

    I don't see any other namespaces available in what I downloaded from NuGet.

    The instructions said to search for and install GrapeCity.Documents.Pdf. I did this and NuGet also installed GrapeCity.Documents.Common and GrapeCity.Documents.Imaging. Please tell me which of these contains FieldCollection. It seems I don't have everything I need.

  • Replied 19 March 2020, 2:59 am EST

    Nevermind. I found it.
  • Replied 19 March 2020, 3:01 am EST

    Sorry, clicked 'post' just as your message appeared :)

    You have all the necessary assemblies. The PDF OM is very large, so some types are in namespaces nested inside GrapeCity.Documents.Pdf, in particular the form related types are in GrapeCity.Documents.Pdf.AcroForms - note the following import in my code snippet:

    Imports GrapeCity.Documents.Pdf.AcroForms

    Thanks.
  • Replied 23 April 2020, 6:30 am EST

    I have ASP.NET Web Application.
    Our clients need the ability to upload fillable pre- existing PDF documents for their users.
    These users need to be able to open (or view) this PDF fill and saved them back.
    Does this product allow me to do this on ASP.NET Web Applications? and more important without the need of they to install anything except for my application.

    We don't have the control of the files they could upload
  • Replied 23 April 2020, 4:16 pm EST

    Hello,
    In the latest release of PDFViewer, we have form edit and save support. Please refer to the following demo sample for reference:
    https://www.grapecity.com/documents-api-pdf/demos/gc-pdf-viewer/viewer-edit-all/pdf-cs

    Regards,
    Manish Gupta
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels