Adobe ColdFusion 8

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 a 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 new 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>