The following examples show how you can use PDF forms in your applications.
The following example shows how to populate fields in a PDF form created in LiveCycle Designer based on an employee's 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 to be 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.
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>