Paul Usher's Blog

Filling the forms – programmatically

As soon as we announced the form filling capabilities in v14.2 of the PDF Viewer control you made it clear… you wanted the ability to do it yourself – in code. While it seems like a trivial task (as most PDF features do), it was far from it.  Anyone who has looked at the PDF specifications will know there is a lot to consider and we had to make it straightforward for you to implement, after all, it’s what you would expect.

Well, the team have been working tirelessly on it and I am pleased to share that in the v14.2.5 maintenance update, programmatic form filling IS available. There are a number of different ways to interact with form data; directly, via import and via export. Lets take a look at what is involved…

    private void FormFillManual(string pathForPdf)
    {
      PdfDocumentProcessor doc = new PdfDocumentProcessor();
      doc.LoadDocument(pathForPdf);
      PdfFormData data = doc.GetFormData();
      data["Your_Field_Name_1"].Value = "Some Value 1";
      data["Your_Field_Name_2"].Value = "Some Value 2";
      data["Your_Field_Name_3"].Value = "Some Value 3";
      doc.ApplyFormData(data);
      doc.SaveDocument(pathForPdf);
      doc.CloseDocument();
    }

If you know the names of the fields you want to populate then it is really easy…

Here a sample method receives a string parameter to a path of a PDF Form that you want to fill.  After creating a new instance of the PdfDocumentProcessor object the LoadDocument() method is used to load the PDF. Next step is to set up an instance of the new PdfFormData object and populate it via the PdfDocumentProcessor.GetFormData() method.  Setting or updating the field values is done by providing the field name as a key, for example: data[“Your_Field_Name_1”].Value = “Some Value 1”.  When all values have been set, the ApplyFormData() method is used to update the PdfFormData object, then a call to the SaveDocument() method to commit the changes to the file.

If required, you can easily retrieve a list of field names by iterating through the string collection returned by the method GetFieldNames()

      foreach (string name in formData.GetFieldNames())
      {
        object value = formData[name].Value;
      }

It is possible to fill the PdfFormData object directly from a file, supported formats are:

  • Fdf,
  • Xml
  • Xfdf,
  • Txt

When instantiating the object, pass the name of the data file and format if you know it (the engine will attempt to discover the format if you omit the parameter).

    private void SampleFillPdf(string pathForPdf, string pathForData)
    {
      PdfDocumentProcessor doc = new PdfDocumentProcessor();
      doc.LoadDocument(pathForPdf);
      PdfFormData data = new PdfFormData(pathForData); //automatic detection of type 
      doc.ApplyFormData(data);
      doc.SaveDocument(pathForPdf);
      doc.CloseDocument();
    }

In this example the PDF document is loaded, then the PdfFormData object is created with the contents of the supplied file. As before a call to the ApplyFormData() method binds the results to the PDF which is then written back to the file.

Conversely, if you want to extract data from a pre-populated form document you can do something like this;

    private void SampleExtractPdf(string pathForPdf, string pathForExportedData)
    {
      PdfDocumentProcessor doc = new PdfDocumentProcessor();
      doc.LoadDocument(pathForPdf);
      PdfFormData data = doc.GetFormData();
      data.Save(pathForExportedData, PdfFormDataFormat.Fdf);
      doc.CloseDocument();
    }

To add the functionality to the PDF Viewer component via extension methods, all you need to do is reference the DevExpress.Docs assembly.

The v14.2.5 has just been released, so you can start implementing this cool feature immediately, as always I welcome your feedback.

Published Feb 19 2015, 06:53 PM by
Bookmark and Share

Comments

Del W

Nice! Thanks!

February 19, 2015 3:14 PM

mayer schmukler

THANK YOU!

February 20, 2015 12:51 PM

Orhan ÖCAL

Süper 👍Thanks.

February 23, 2015 8:10 PM

Steven Rasmussen

That's great news!  Thanks!

February 24, 2015 8:41 AM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners