Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
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#.
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);
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();
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; } } ... ... }
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 | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .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 | |
---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-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 | |
---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved. |