Quantcast
Channel: Developer Express Inc.
Viewing all articles
Browse latest Browse all 2389

Filling the forms – programmatically

$
0
0

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…

privatevoid FormFillManual(string pathForPdf)
    {PdfDocumentProcessor doc = newPdfDocumentProcessor();
      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).

privatevoid SampleFillPdf(string pathForPdf, string pathForData)
    {PdfDocumentProcessor doc = newPdfDocumentProcessor();
      doc.LoadDocument(pathForPdf);PdfFormData data = newPdfFormData(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;

privatevoid SampleExtractPdf(string pathForPdf, string pathForExportedData)
    {PdfDocumentProcessor doc = newPdfDocumentProcessor();
      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.


Viewing all articles
Browse latest Browse all 2389

Trending Articles