Application examples that use PDF forms



The following examples show how you can use PDF forms in your applications.

PDF submission example

The following example shows how to populate fields in a PDF form created in LiveCycle Designer based on an employee login information. When the employee completes the form and clicks the PDF Submit button, the entire PDF form with the data is submitted to a second processing page where ColdFusion writes the completed form to a file.

On the ColdFusion login page, an employee enters a user name and password:

<!--- The following code creates a simple form for entering a user name and password.  
    The code does not include password verification. ---> 
<h3>Timesheet Login Form</h3> 
<p>Please enter your user name and password.</p> 
<cfform name="loginform" action="loginform_proc.cfm" method="post"> 
<table> 
    <tr> 
        <td>user name:</td> 
        <td><cfinput type="text" name="username" required="yes"  
                message="A user name is required."></td> 
    </tr> 
    <tr> 
        <td>password:</td> 
        <td><cfinput type="password" name="password" required="yes"  
                message="A password is required."></td> 
    </tr> 
</table> 
    <br/> 
    <cfinput type="submit" name="submit" value="Submit"> 
</cfform>

On the first processing page, a query retrieves all of the information associated with the user name from the cfdocexamples database. The cfpdfform tag populates an associated PDF form created in LiveCycle Designer (called timesheetForm.pdf) with the employee name, phone number, e-mail address, and department. ColdFusion displays the populated form in the browser, where the employee can complete the form and submit it.

<!--- The following code retrieves all of the employee information for the user name entered 
    on the login page. ---> 
<cfquery name="getEmpInfo" datasource="cfdocexamples"> 
    SELECT * FROM EMPLOYEES 
    WHERE EMAIL = <cfqueryparam value="#FORM.username#"> 
</cfquery> 
 
<!---  
The following code populates the template called "timesheetForm.pdf" with data from the query and displays the interactive PDF form in the browser. A field in the PDF form contains the name of the output file being written. It is a combination of the user name and the current date.  
---> 
 
<!--- Notice the use of the cfpdfsubform tag. Forms created from templates in LiveCycle Designer include a subform called form1. Use the cfpdfsubform tag to match the structure of the form in ColdFusion. Likewise, the field names in the cfpdfformparam tags must match the field names in the PDF form. If the form structures and field names do not match exactly, ColdFusion does not populate the form fields. ---> 
 
<cfpdfform source="c:\forms\timesheetForm.pdf" action="populate"> 
    <cfpdfsubform name="form1"> 
        <cfpdfformparam name="txtEmpName" value="#getEmpInfo.FIRSTNAME# 
            #getEmpInfo.LASTNAME#"> 
        <cfpdfformparam name="txtDeptName" value="#getEmpInfo.DEPARTMENT#"> 
        <cfpdfformparam name="txtEmail" value="#getEmpInfo.IM_ID#"> 
        <cfpdfformparam name="txtPhoneNum" value="#getEmpInfo.PHONE#"> 
        <cfpdfformparam name="txtManagerName" value="Randy Nielsen"> 
        <cfpdfformparam name="txtSheet" 
            value="#form.username#_#DateFormat(Now())#"> 
    </cfpdfsubform> 
</cfpdfform>

When the user completes the timesheet form (by filling in the time period, projects, and hours for the week) and clicks the Submit button, Acrobat sends the PDF file in binary format to a second ColdFusion processing page.

Note: In LiveCycle Designer, use the standard Submit button on the PDF form and specify “submit as: PDF” in the button Object Properties. Also, ensure that you enter the URL to the ColdFusion processing page in the Submit to URL field.

The cfpdfform tag read action reads the PDF content into a result structure named fields. The cfpdfform tag populate action writes the completed form to a file in the timesheets subdirectory.

<!--- The following code reads the PDF file submitted in binary format and generates a result structure called fields. The cfpdfform populate action and the cfoutput tags reference the fields in the structure. ---> 
 
<cfpdfform source="#PDF.content#" action="read" result="fields"/> 
<cfset empForm="#fields.form1#"> 
<cfpdfform action="populate" source="#PDF.content#" destination="timesheets\#empForm.txtsheet#.pdf" overwrite="yes"/> 
 
<h3>Timesheet Completed</h3> 
<p><cfoutput>#empForm.txtempname#</cfoutput>,</p> 
<p>Thank you for submitting your timesheet for the week of <cfoutput>#DateFormat(empForm.dtmForPeriodFrom, "long")#</cfoutput> through <cfoutput>#DateFormat(empForm.dtmForPeriodto, "long")#</cfoutput>. Your manager, <cfoutput>#empForm.txtManagerName#</cfoutput>, will notify you upon approval.</p>

HTTP post example

The following example shows how to extract data from a PDF form submitted with HTTP post and use it to update an employee database. The form was created in LiveCycle Designer.

On the ColdFusion login page, an employee enters a user name and password:

<!--- The following code creates a simple form for entering a user name and password. The code does not include password verification. ---> 
 
<h3>Employee Update Login Form</h3> 
<p>Please enter your user name and password.</p> 
<cfform name="loginform" action="loginform_procHTTP.cfm" method="post"> 
<table> 
    <tr> 
        <td>user name:</td> 
        <td><cfinput type="text" name="username" required="yes"  
                message="A user name is required."></td> 
    </tr> 
    <tr> 
        <td>password:</td> 
        <td><cfinput type="password" name="password" required="yes"  
                message="A password is required."></td> 
    </tr> 
</table> 
    <br/> 
    <cfinput type="submit" name="submit" value="Submit"> 
</cfform>

On the first processing page, a query retrieves all of the information associated with the user name from the cfdocexamples database. The cfpdfform tag populates an associated PDF form created in LiveCycle Designer (called employeeInfoHTTP.pdf) with the employee name, phone number, e-mail address, and department. The form also includes the employee ID as a hidden field. ColdFusion displays the populated form in the browser where the employee can change personal information in the form and submit it.

<!--- The following code retrieves all of the employee information for the user name entered on the form page. ---> 
<cfquery name="getEmpInfo" datasource="cfdocexamples"> 
SELECT * FROM EMPLOYEES 
WHERE EMAIL = <cfqueryparam value="#FORM.username#"> 
</cfquery> 
 
<!--- The following code populates the template called "employeeInfoHTTP.pdf" with data from the query. As in the previous example, notice the use of the cfpdfsubform tag. The txtEmpID field is a hidden field on the PDF form. ---> 
 
<cfquery name="getEmpInfo" datasource="cfdocexamples"> 
SELECT * FROM EMPLOYEES 
WHERE EMAIL = <cfqueryparam value="#FORM.username#"> 
</cfquery> 
 
<cfpdfform source="c:\forms\employeeInfoHTTP.pdf" action="populate"> 
    <cfpdfsubform name="form1"> 
        <cfpdfformparam name="txtFirstName" value="#getEmpInfo.FIRSTNAME#"> 
        <cfpdfformparam name="txtLastName" value="#getEmpInfo.LASTNAME#"> 
        <cfpdfformparam name="txtDeptName" value="#getEmpInfo.DEPARTMENT#"> 
        <cfpdfformparam name="txtEmail" value="#getEmpInfo.IM_ID#"> 
        <cfpdfformparam name="txtPhoneNum" value="#getEmpInfo.PHONE#"> 
        <cfpdfformparam name="txtEmpID" value="#getEmpInfo.Emp_ID#"> 
    </cfpdfsubform> 
</cfpdfform>

When the employee updates the information in the form and clicks the HTTP post Submit button, Acrobat sends the form data (but not the form itself) to a second ColdFusion processing page.

Note: In LiveCycle Designer, use the HTTP Submit button on the PDF form. Also, ensure that you enter the URL to the ColdFusion processing page in the URL field of button Object Properties.

Reproduce the structure, not just the field name, when you reference form data. To determine the structure of the form data, use the cfdump tag.

<!--- The following code reads the form data from the PDF form and uses it to update 
    corresponding fields in the database. ---> 
 
<cfquery name="updateEmpInfo" datasource="cfdocexamples"> 
UPDATE EMPLOYEES 
    SET FIRSTNAME = "#FORM1.SUBFORM.HEADER.TXTFIRSTNAME#", 
        LASTNAME = "#FORM1.SUBFORM.HEADER.TXTLASTNAME#", 
        DEPARTMENT = "#FORM1.SUBFORM.HEADER.TXTDEPTNAME#", 
        IM_ID = "#FORM1.SUBFORM.HEADER.TXTEMAIL#", 
        PHONE = "#FORM1.SUBFORM.HEADER.TXTPHONENUM#" 
    WHERE EMP_ID = <cfqueryparam value="#FORM1.SUBFORM.TXTEMPID#"> 
</cfquery> 
<h3>Employee Information Updated</h3> 
<p><cfoutput>#FORM1.SUBFORM.HEADER.TXTFIRSTNAME#</cfoutput>,</p> 
<p>Thank you for updating your employee information in the employee database.</p>

Embedded PDF form example

The following example shows how to embed an interactive PDF form in a PDF document created with the cfdocument tag.

On the login page, an employee enters a user name and password:

<h3>Employee Login Form</h3> 
<p>Please enter your user name and password.</p> 
<cfform name="loginform" action="embed2.cfm" method="post"> 
    <table> 
        <tr> 
            <td>user name:</td> 
            <td><cfinput type="text" name="username" required="yes"  
                message="A user name is required."></td> 
        </tr> 
        <tr> 
            <td>password:</td> 
            <td><cfinput type="password" name="password" required="yes"  
                message="A password is required."></td> 
        </tr> 
    </table> 
    <br/> 
    <cfinput type="submit" name="submit" value="Submit"> 
</cfform>

On the processing page, a query populates an interactive PDF form from the cfdocexamples database. The interactive PDF form is embedded in a PDF document created with the cfdocument tag. The PDF document comprises three sections: the cfdocumentsection tags define the first and last sections of the document; the cfpdfform tag defines the second section embedded in the PDF document. Each section starts a new page in the PDF document. The Print button on the PDF form prints the entire document, including the pages in the sections before and after the interactive PDF form.

<cfquery name="getEmpInfo" datasource="cfdocexamples"> 
SELECT * FROM EMPLOYEES 
WHERE EMAIL = <cfqueryparam value="#FORM.username#"> 
</cfquery> 
 
<!--- The following code creates a PDF document with headers and footers. 
---> 
<cfdocument format="pdf"> 
    <cfdocumentitem type="header"> 
    <font size="-1" align="center"><i>Nondisclosure Agreement</i></font> 
    </cfdocumentitem> 
    <cfdocumentitem type="footer"> 
    <font size="-1"><i>Page <cfoutput>#cfdocument.currentpagenumber# of#cfdocument.totalpagecount#</cfoutput></i></font> 
    </cfdocumentitem> 
     
<!--- The following code creates the first section in the PDF document. ---> 
    <cfdocumentsection> 
    <h3>Employee Nondisclosure Agreement</h3> 
    <p>Please verify the information in the enclosed form. Make any of the necessary changes 
    in the online form and click the <b>Print</b> button. Sign and date the last page. Staple 
    the pages together and return the completed form to your manager.</p> 
    </cfdocumentsection> 
     
<!--- The following code embeds an interactive PDF form within the PDF document with fields 
    populated by the database query. The cfpdpfform tag automatically creates a section in 
    the PDF document. Do not embed the cfpdfform within cfdocumentsection tags. ---> 
 
    <cfpdfform action="populate" source="c:\forms\embed.pdf"> 
        <cfpdfsubform name="form1"> 
            <cfpdfformparam name="txtEmpName"  
                value="#getEmpInfo.FIRSTNAME# #getEmpInfo.LASTNAME#"> 
            <cfpdfformparam name="txtDeptName" value="#getEmpInfo.DEPARTMENT#"> 
            <cfpdfformparam name="txtEmail" value="#getEmpInfo.IM_ID#"> 
            <cfpdfformparam name="txtPhoneNum" value="#getEmpInfo.PHONE#"> 
            <cfpdfformparam name="txtManagerName" value="Randy Nielsen"> 
        </cfpdfsubform> 
    </cfpdfform> 
 
<!--- The following code creates the last document section. Page numbering resumes in this     section. ---> 
    <cfdocumentsection> 
    <p>I, <cfoutput>#getEmpInfo.FIRSTNAME# #getEmpInfo.LASTNAME#</cfoutput>, hereby attest 
    that the information in this document is accurate and complete.</p> 
    <br/><br/> 
    <table border="0" cellpadding="20"> 
        <tr><td width="300"> 
        <hr/> 
        <p><i>Signature</i></p></td> 
        <td width="150"> 
        <hr/> 
        <p><i>Today's Date</i></p></td></tr> 
    </table> 
    </cfdocumentsection> 
</cfdocument>

Update PDF form example

The following example shows how ColdFusion lets you update a PDF form while retaining existing data. The application lets a user create an office supply request from a blank form created in LiveCycle or modify an existing supply request. The user has the option to submit the completed form as an e-mail attachment.

<!--- supplyReq1.cfm ---> 
<!--- The following code prefills fields in a blank form in LiveCycle and writes the prefilled 
    form to a new file called NewRequest.pdf in the supplyReqs directory. ---> 
<cfpdfform source="SupplyReq.pdf" action="populate" destination="supplyReqs/NewRequest.pdf" 
    overwrite="yes"> 
    <cfpdfsubform name="form1"> 
        <cfpdfformparam name="txtContactName" value="Constance Gardner"> 
        <cfpdfformparam name="txtCompanyName" value="Wild Ride Systems"> 
        <cfpdfformparam name="txtAddress" value="18 Melrose Place"> 
        <cfpdfformparam name="txtPhone" value="310-654-3298"> 
        <cfpdfformparam name="txtCity" value="Hollywood"> 
        <cfpdfformparam name="txtStateProv" value="CA"> 
        <cfpdfformparam name="txtZipCode" value="90210"> 
    </cfpdfsubform> 
</cfpdfform> 
     
<!--- The following code lets users choose an existing supply request form or create a new 
    request from the NewRequest.pdf form. ---> 
<h3>Office Supply Request Form</h3> 
<p>Please choose the office supply request form that you would like to open. Choose <b>New 
    Supply Request</b> to create a request.</p> 
 
<!--- The following code populates a list box in a form with files located in the specified 
    directory. ---> 
<cfset thisDir = expandPath(".")> 
<cfdirectory directory="#thisDir#/supplyReqs" action="list" name="supplyReqs"> 
 
<cfif #supplyReqs.name# is "NewRequest.pdf"> 
    <cfset #supplyReqs.name# = "---New Supply Request---"> 
</cfif> 
 
<cfform name="fileList" action="supplyReq2.cfm" method="post"> 
    <cfselect name="file" query="supplyReqs" value="name" display="name" 
        required="yes" size="8" multiple="no"/><br/> 
    <cfinput type="submit" name="submit" value="OK"> 
</cfform> 
 
<!--- supplyReq2.cfm ---> 
<!--- The following code displays the PDF form that the user selected. ---> 
<cfif #form.file# is "---New Supply Request---"> 
    <cfset #form.file# = "NewRequest.pdf"> 
</cfif> 
<cfpdfform source="supplyReqs/#form.file#" action="populate"/> 
 
 
<!--- supplyReq3.cfm ---> 
<!--- The following code reads the PDF file content from the submitted PDF form. ---> 
<cfpdfform source="#PDF.content#" action="read" result="fields"/> 
 
<!--- The following code writes the PDF form to a file and overwrites the file if it exists. ---> 
<cfpdfform action="populate" source="#PDF.content#" destination="SupplyReqs/supplyReq_#fields.form1.txtRequestNum#.pdf" overwrite="yes"/> 
 
<!--- The following code customizes the display based on field values extracted from the PDF 
    form. ---> 
<p><cfoutput>#fields.form1.txtRequester#</cfoutput>,</p> 
<p>Your changes have been recorded for supply request <cfoutput>#fields.form1.txtRequestNum#</cfoutput>.</p> 
<p>If the form is complete and you would like to submit it to <cfoutput>#fields.form1.txtContactName#</cfoutput> for processing, click <b>Submit</b>. 
 
<!--- The following code gives the option to e-mail the submitted form as an attachment or 
    return to the home page. ---> 
<cfform name="send" method="post" action="supplyReq4.cfm"> 
    <cfinput type="hidden" 
value="SupplyReqs/supplyReq_#fields.form1.txtRequestNum#.pdf" name="request"> 
    <cfinput type="hidden" value="#fields.form1.txtRequester#" name="requester"> 
    <cfinput type="submit" value="Submit" name="Submit"> 
</cfform> 
<p>If you would like to modify your request or choose another request,  
<a href="supplyReq1.cfm">click here</a>.</p> 
 
<!--- supplyReq4.cfm ---> 
<!--- The following code sends the completed PDF form as an attachment to the person 
    responsible for processing the form. ---> 
<p>Your request has been submitted.</p> 
<cfmail from="#form.requester#@wildride.com" to="cgardener@wildride.com"  
    subject="see attachment"> 
    Please review the attached PDF supply request form. 
    <cfmailparam file="#form.request#"> 
</cfmail>