PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

Save Captured Form Field Data Back To AcroForms PDF Files... Online

Capturing PDF form field data over the Web and storing it in a database is a tame affair. How about creating new PDF files containing the original form fields but with the captured information as the new default values?
By Shijin C. A. and M. V. Niranjan

A great deal of creativity and effort goes into designing PDF Forms (or AcroForms). When users submit a form, the form field data is captured on a website and then typically saved in a faceless database.

However, if the captured data could be set as the default values in a new PDF file containing the same form fields, then what we have is a virtual copy of the PDF file that the user saw when the form was submitted - a curious case of WYSIWYG in reverse!

And, this can be done on-the-fly by a webserver. Using Gnostice PDFOne .NET, form fields in a PDF file can be edited and captured data items can be set as their default values.

Here is an example using a webserver running on Active Server Pages .NET (ASP.NET) and Internet Information Services (IIS).

A typical PDF AcroForms file with form fields, such as text boxes, check boxes, push buttons, and radio buttons is used. Each form field is identified in the PDF document by a unique name. The form is submitted using a submit button. The submit action is associated with a specific website URL. When the submit action occurs, the PDF viewer application (such as Adobe® Reader™) submits all the form field values to the specified URL.

On the website, the submitted values are available to the script on the above URL via the Server.Request object. Now, here are the code steps in C#.

Load the input file

A copy of the PDF file containing the original form fields is used as the input. The captured data needs to be saved in this copy. A PDFDocument object is created and then a call to the Load() method loads the file.

   PDFDocument doc = new PDFFDocument();
   doc.Load(input.pdf);
Retrieve all the form fields from the input file

The method GetAllFormFields() retrieves all the form fields from the input file. The method GetAllFormFields is overloaded to retrieve specific set of form fields, say, on a particular page. All the retrieved form fields are collected in a list.

   ArrayList list = doc.GetAllFormFields();
Change the values of each form field using values given by the user

The Request object contains all the keys. For each key, retrieve the form fields and iterate through the list. Type cast to the respective form fields and change their existing values to new values. The new values can be obtained by the Request object specifying each key. The following code shows how to change the values of text fields.

   for (int i = 0; i < list.Count; i++)
   {
      PDFFormTextField txtFld = list[i] as PDFFormTextField;  
      if (txtFld != null)
      {
         strFieldValue = Request.Form[key];
  if (strFieldValue != null)
         {
            txtFld.FieldValue = strFieldValue;  
         }
      }
      ...
      ...
    }
Save the output file and display

The output file is saved using the Response object. The name of the file includes the current date and time plus a random number so that each set of captured input goes to a separate file.

The code also displays the new PDF file on the user's browser so that whoever submitted the form can also have a copy of the PDF with the values that were originally submitted.

   // Saves the document
   doc.Version = PDFVersion.V1_4;
   Random random =new Random();
   string output = DateTime.Now.ToString("yymmdd")
                   + random.Next(1000).ToString("_000")
                   + ".pdf";
   doc.Save(Server.MapPath(output));
   doc.Close();
   // Displays the PDF document on user's browser with new values
   FileStream fs = new FileStream(Server.MapPath(output), 
                                  FileMode.Open);
   byte[] ba = new byte[fs.Length];
   fs.Read(ba, 0, (int)fs.Length);
   fs.Close();
   Response.Clear();
   Response.ContentType = "application/pdf";
   Response.BinaryWrite(ba);
   Response.Flush();
   Response.ContentType = null;
   Response.Close();
   ...

Click here to download the project file for this example. After the installation, copy http://localhost/FillFormFields/WebForm1.aspx and paste it in your browser. A PDF file will be displayed. You can modify the form fields and then submit the form. A copy of the original PDF file but with the form fields displaying the data you had entered will be displayed and also saved to the project installation folder. If you get any errors, please ensure that the folder containing the PDFFormFields.pdf file in the installation folder is given adequate permissions.

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.