Persits Software, Inc. Web Site
 Navigator:  Home |  Manual |  Chapter 12: Form Fill-in
Chapter 13: Barcodes Chapter 11: Form Creation
  Chapter 12: Form Fill-in
12.1 Form Fill-in Overview
12.2 Adobe Acrobat 7/Designer 7 Issues
12.3 Code Sample

12.1 Form Fill-in Overview

In Chapter 9 of this manual, we already described the process of filling in a form-like document (see Section 9.2).

The approach described in that chapter had one serious drawback: individual blanks in a "form" had to be referenced by their physical coordinates. This is because the Section 9.2 code sample did not involve a real form, but a regular (static) PDF document depicting a form.

Using tools such as Adobe Acrobat, it is possible to place interactive fields on an existing document using a graphical user interface. The form creator chooses a name, size, location and other options for each field. Once a form has been created, it can be filled in either interactively by a user, or automatically using tools such as AspPDF. A form field can be referenced by name, as opposed to (x, y)-coordinates, which simplifies the coding significantly.

The PdfForm object provides the method FindField which returns a PdfAnnot object representing a top-level form field with the specified name. If no field under this name is found, the method returns Nothing.

PdfAnnot objField = objDoc.Form.FindField("txtLastName");

To specify a text value for a field, the method SetFieldValue of the PdfAnnot object should be used. This method accepts two arguments: a text string, and a font to be used to render the string. The SetFieldValue method uses the field's pre-defined alignment and font size parameters. It also preserves an existing field border if there is one.

The SetFieldValue method can be used to set a value not only for text fields, but checkboxes, drop-down list boxes and radio buttons as well. To select or unselect a checkbox, you must specify a state name for the text string argument. The state name for the "on" state should be obtained via the FieldOnValue property. The state name for the "off" state is usually "Off".

When working with radio button groups, you need to use the PdfAnnot.Children collection to reference each individual radio button within a group. The code snippet below deselects radio button #1 (American Express) and selects radio button #2 via the SetFieldValue method.

The following code fragment fills out a credit card form generated in Section 11.2:

// Credit card number
PdfAnnot objField = objDoc.Form.FindField("ccNumber");
objField.SetFieldValue( "324234324234", objDoc.fonts["Helvetica"] );

// Expiration month
PdfAnnot objField = objDoc.Form.FindField("ccMonth");
objField.SetFieldValue( "Mar", Doc.fonts["Helvetica"] );

// Expiration year
PdfAnnot objField = objDoc.Form.FindField("ccYear");
objField.SetFieldValue( "2010", Doc.fonts["Helvetica"] );

// "Need Receipt?" checkbox
PdfAnnot objField = objDoc.Form.FindField("Receipt");
objField.SetFieldValue( objField.FieldOnValue, null )
// "on" state

// Credit card type (radio buttons)
PdfAnnot objField = objDoc.Form.FindField("ccType");
objField.Children[1].SetFieldValue( "Off", null );
// Unselect Amex
objField.Children[2].SetFieldValue( objField.Children[2].FieldOnValue, null ); '
Select Visa

12.2 Adobe Acrobat 7/Designer 7 Issues
The emergence of Adobe Acrobat 7.0 and Designed 7.0 brought about a number of significant changes to the PDF form specifications. Two changes that are most relevant to AspPDF's form fill-in functionality are a new way of naming form fields, and the Adobe XML Forms Architecture (XFA).

When you switch to PDF forms created by Version 7 and later of the Adobe Acrobat suite, you may have to make some changes to your AspPDF-based applications to accommodate the new Adobe specs.

12.2.1 Field Naming

Adobe Designer 7 assigns hierarchically-built names to form fields, such as form1[0].#subform[0].Address[0]. It is these long names that have to be passed to the Form.FindField method.

If you are not sure as to the full name of a field in your PDF, we recommend opening this document in a text editor (such as WordPad) and searching for your short field name (such as "Address"). You are likely to find a fragment which looks similar to this:

33 0 obj<</Rect[118.701 659.835 501.165 681.165]/TM(form1[0].#subform[0].Address[0]) /Subtype/Widget/TU(Address:)/Parent 29 0 R/F 4/P 8 0 R/Q 0/T<FEFF0041006400640072006500730073005B0030005D> /StructParent 2/DA(/TimesNewRomanPSMT 10.00 Tf 0 g)/FT/Tx/Type/Annot/MK<<>>>>
endobj

The string in parentheses followed by the word /TM is likely this field's long name which you can copy and paste into your script.

To help determine the names of the fields in your form, we have provided the Form Field Finder online application which enables you to upload your PDF document to our server, and the list of full field names in the document will be instantly displayed.

12.2.2 Adobe XML Forms Architecture (XFA)

In a nutshell, XFA is Adobe's new way to describe form structure and content using XML. XFA is only briefly mentioned in the official PDF 1.6 specifications, but the full description of this new standard is available from the Adobe.com web site as a 744-page document. You can download it here.

An attempt to programmatically fill in an XFA-based form (such as a form created by Adobe Designer 7) using AspPDF may fail as AspPDF currently lacks XFA support. As a temporary work-around, AspPDF 1.5.0.1+ offers a new method, PdfForm.RemoveXFA, which completely removes the XFA information from a form, thus making it compatible with older PDF form specifications, which AspPDF does support. In most cases, the user will not even notice any difference between an XFA-based and XFA-free form.

The following code snippet demonstrates the usage of the new method:

PdfDocument objDoc = objPDF.OpenDocument(@"c:\path\TheForm.pdf");
objDoc.Form.RemoveXFA();

PdfAnnot objField = objDoc.Form.FindField("form1[0].#subform[0].Name[0]");
objField.SetFieldValue( "John Smith", Doc.fonts["Times-Roman"] );
...
// Fill in other fields

12.3 Code Sample
The following code sample fills in a simple form created in Adobe Designer 7.0.

C#
// create instance of the PDF manager
PdfManager objPDF = new PdfManager();

// Open existing form
PdfDocument objDoc = objPDF.OpenDocument( Server.MapPath( "SimpleForm.pdf" ) );

PdfFont objFont = objDoc.Fonts["Helvetica-Bold"]; // a standard font

// Remove XFA support from it
objDoc.Form.RemoveXFA();

// Fill in Name
PdfAnnot objField = objDoc.Form.FindField("form1[0].#subform[0].Name[0]");
objField.SetFieldValue( txtName.Text, objFont );

// Fill in Address
objField = objDoc.Form.FindField("form1[0].#subform[0].Address[0]");
objField.SetFieldValue( txtAddress.Text, objFont );

// Fill in marital status
int nIndex = 1; 
if( rdMarried.Checked )
	nIndex = 2;

if( rdDivorced.Checked )
	nIndex = 3;

objField = objDoc.Form.FindField("form1[0].#subform[0].RadioButtonList[0]");
PdfAnnot objChildField = objField.Children[nIndex];
objChildField.SetFieldValue( objChildField.FieldOnValue, null );

// Fill in "How did you hear about us" checkboxes
if( chkInternet.Checked )
{
	objField = objDoc.Form.FindField("form1[0].#subform[0].Internet[0]");
	objField.SetFieldValue( objField.FieldOnValue, null );
}

if( chkWordOfMouth.Checked )
{
	objField = objDoc.Form.FindField("form1[0].#subform[0].WordOfMouth[0]");
	objField.SetFieldValue( objField.FieldOnValue, null );
}

if( chkNewspaper.Checked )
{
	objField = objDoc.Form.FindField("form1[0].#subform[0].Newspaper[0]");
	objField.SetFieldValue( objField.FieldOnValue, null );
}

string strFileName = objDoc.Save( Server.MapPath( "filledform.pdf"), false );
VB.NET
' create instance of the PDF manager
Dim objPDF As PdfManager = New PdfManager()

' Open existing form
Dim objDoc As PdfDocument = objPDF.OpenDocument( Server.MapPath( "SimpleForm.pdf" ) )

Dim objFont As PdfFont = objDoc.Fonts("Helvetica-Bold") ' a standard font

' Remove XFA support from it
objDoc.Form.RemoveXFA()

' Fill in Name
Dim objField As PdfAnnot = objDoc.Form.FindField("form1[0].#subform[0].Name[0]")
objField.SetFieldValue( txtName.Text, objFont )

' Fill in Address
objField = objDoc.Form.FindField("form1[0].#subform[0].Address[0]")
objField.SetFieldValue( txtAddress.Text, objFont )

' Fill in marital status
Dim nIndex As Integer = 1
If rdMarried.Checked Then nIndex = 2

If rdDivorced.Checked Then nIndex = 3

objField = objDoc.Form.FindField("form1[0].#subform[0].RadioButtonList[0]")
Dim objChildField As PdfAnnot = objField.Children(nIndex)
objChildField.SetFieldValue( objChildField.FieldOnValue, Nothing )

' Fill in "How did you hear about us" checkboxes
If chkInternet.Checked Then
	objField = objDoc.Form.FindField("form1[0].#subform[0].Internet[0]")
	objField.SetFieldValue( objField.FieldOnValue, Nothing )
End If

If chkWordOfMouth.Checked Then
	objField = objDoc.Form.FindField("form1[0].#subform[0].WordOfMouth[0]")
	objField.SetFieldValue( objField.FieldOnValue, Nothing )
End If

If chkNewspaper.Checked Then
	objField = objDoc.Form.FindField("form1[0].#subform[0].Newspaper[0]")
	objField.SetFieldValue( objField.FieldOnValue, Nothing )
End If

Dim strFileName As String = objDoc.Save( Server.MapPath( "filledform.pdf"), False )

Click the links below to run this code sample:

http://localhost/asppdf.net/manual_12/12_fill.cs.aspx
http://localhost/asppdf.net/manual_12/12_fill.vb.aspx  Make sure AspPDF.NET is installed on your local machine and IIS is running for these links to work.

Chapter 13: Barcodes Chapter 11: Form Creation

Search AspPDF.net

Newsletter Signup

Other Products
AspPDF
AspUpload
AspJpeg
AspEmail
AspEncrypt
AspGrid
AspUser
  This site is owned and maintained by Persits Software, Inc. Copyright © 2003 - 2009. All Rights Reserved.