Adobe ColdFusion 8

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>