The following examples show you how to use the cfpdf tag to perform PDF document operations in simple applications.

Merging documents based on a keyword search

The following example shows how to use the getInfo and merge actions to assemble a PDF document from multiple tax files based on business type (Sole Proprietor, Partnership, or S Corporation). The application assembles the tax forms and information booklets based on a radio button selection. Some tax forms and booklets apply to more than one business type (for example, Partnership and S Corporations both use the tax form f8825.pdf). For instructions on setting keywords for PDF documents, see Managing PDF document information.
This example shows how to perform the following tasks:

  • Use the getInfo action to perform a keyword search on PDF files in a directory.
  • Create a comma-separated list of files that match the search criteria.
  • Use the merge action to merge the PDF documents in the comma-separated list into an output file.
    The first CFM page creates a form for selecting the tax documents based on the business type:

<h3>Downloading Federal Tax Documents</h3>
<p>Please choose the type of your business.</p>
<!--- Create the ColdFusion form to determine which PDF documents to merge. --->
<cfform action="cfpdfMergeActionTest.cfm" method="post">
<tr><td><cfinput type="radio" name="businessType"
Value="Sole Proprieter">Sole Proprietor</td></tr>
<tr><td><cfinput type="radio" name="businessType"
<tr><td><cfinput type="radio" name="businessType" Value="S Corporation">
S Corporation</td></tr>
<cfinput type = "hidden" name = "selection required" value = "must make a selection">
<tr><td><cfinput type="Submit" name="OK" label="OK"></td></tr>

The action page loops through the files in the taxes subdirectory and uses the getInfo action to retrieve the keywords for each file. If the PDF file contains the business type keyword (Sole Proprietor, Partnership, or S Corporation), ColdFusion adds the absolute path of the file to a comma-separated list. The merge action assembles the files in the list into an output PDF file:

<cfset bizType=#form.businessType#>
<!--- Create a variable for the path of the current directory. --->
<cfset thisPath=ExpandPath(".")>

<!--- List the files in the taxes subdirectory. --->
<cfdirectory action="list" directory="#thisPath#\taxes" name="filelist">

<!--- The following code loops through the files in the taxes subdirectory. The getInfo
action to retrieves the keywords for each file and determines whether the business type
matches one of the keywords in the file. If the file contains the business type keyword,
ColdFusion adds the file to a comma-separated list. --->
<cfset tempPath="">
<cfloop query="filelist">
<cfset fPath="#thisPath#\taxes\">
<cfpdf action="GetInfo" source="#fPath#" name="kInfo"></cfpdf>
<cfif #kInfo.keywords# contains "#bizType#">
<cfset tempPath=#tempPath# & #fPath# & ",">

<!--- Merge the files in the comma-separated list into a PDF output file called "taxMerge.pdf". --->
<cfpdf action="merge" source="#tempPath#" destination="taxMerge.pdf" overwrite="yes"/>

<h3>Assembled Tax Document</h3>
<p>Click the following link to view your assembled tax document:</p>
<a href="http://localhost:8500/Lion/taxmerge.pdf">
<p>Your Assembled Tax Document</a></p>

Using DDX instructions to create a book

The following example shows how to create a book using DDX instructions with the processddx action. Specifically, it shows how to perform the following tasks:

  • Merge several PDF documents into an output file.
  • Add a generated table of contents page.
  • Add headers and footers.
  • Add automatic page numbers.
  • Apply different styles to the table of contents and the body of the book.
    The following code shows the DDX file:

<DDX xmlns=""
xsi:schemaLocation=" coldfusion_ddx.xsd">
<PDF result="Out1">
<PDF source="Doc0"/>
<TableOfContents maxBookmarkLevel="3" bookmarkTitle="Table of Contents"
<Header styleReference="TOCheaderStyle"/>
<Footer styleReference="TOCFooterStyle"/>
<Footer styleReference="FooterStyle"/>
<PDF source="Doc1"/>
<PDF source="Doc2"/>
<PDF source="Doc3"/>
<PDF source="Doc4"/>

<StyleProfile name="TOCheaderStyle">
<p color="red" font-weight="bold" font="Arial">Table of Contents</p>

<StyleProfile name="TOCFooterStyle">
<p font-size="9pt">Page <_PageNumber/> of <_LastPageNumber/></p>

<StyleProfile name="FooterStyle">
<p font-size="9pt"><i>CFML Reference</i></p>
<p font-size="9pt">Page <_PageNumber/> of <_LastPageNumber/></p>


The following code shows the ColdFusion page that processes the DDX instructions:

<cfset inputStruct=StructNew()>
<cfset inputStruct.Doc0="Title.pdf">
<cfset inputStruct.Doc1="Chap1.pdf">
<cfset inputStruct.Doc2="Chap2.pdf">
<cfset inputStruct.Doc3="Chap3.pdf">
<cfset inputStruct.Doc4="Chap4.pdf">

<cfset outputStruct=StructNew()>
<cfset outputStruct.Out1="myBook.pdf">

<cfpdf action="processddx" ddxfile="book.ddx" inputfiles="#inputStruct#"
outputfiles="#outputStruct#" name="ddxVar">


Applying a watermark to a form created in Acrobat

The following example shows how to prefill an interactive Acrobat tax form and apply a text-string watermark to the completed form that the user posted. Specifically, this example shows how to perform the following tasks:

  • Use the cfpdfform and cfpdfformparam tags to populate a form created in Acrobat.
  • Use the cfpdfform tag to write the output of a PDF post submission to a file.
  • Use the cfpdf processddx action to apply a text-string watermark to the completed form.

Note: This example uses the cfdocexamples database and the 1040 and 1040ez Federal tax forms. A valid user name is "cpeterson." To download the 1040 and 1040ez IRS tax forms used in this example, go to the IRS website. Open the forms in Acrobat (not LiveCycle Designer) and add a submit button that points to the URL for the ColdFusion processing page. Also, add a hidden field with a variable that contains a unique filename used for the completed tax form.

The first ColdFusion page creates a login form that prompts for the user name and Social Security Number:

code does not include password verification. --->

<h3>Tax Login Form</h3>
<p>Please enter your user name and your social security number.</p>
<cfform name="loginform" action="TaxFile2.cfm" method="post">
<td>User name:</td>
<td><cfinput type="text" name="username" required="yes"
message="A user name is required."></td>
<td><cfinput type="text" name="SS1" maxLength="3" size="3"
required="yes" mask="999"> -
<cfinput type="text" name="SS2" maxLength="2" size="2" required="yes"
mask="99"> -
<cfinput type="text" name="SS3" maxLength="4" size="4" required="yes"
<cfinput type="submit" name="submit" value="Submit">

The second ColdFusion page retrieves the user information from the cfdocexamples database. Also, it creates a pop-up menu with a list of available tax forms:

user name entered on the login page. --->
<cfquery name="getEmpInfo" datasource="cfdocexamples">
WHERE EMAIL = <cfqueryparam value="#FORM.username#">

<h3>Choose a tax form</h3>
<p>Hello <cfoutput>#getEmpInfo.firstname#</cfoutput>,</p>
<p>Please choose a tax form from the list:</p>
<!--- Create a pop-up menu with a list of tax forms. --->
<cfset thisPath=ExpandPath(".")>
<!--- Create a variable called filerID that is a combination of the username and the last
three digits of the Social Security number. --->
<cfset filerID="#form.username#_#form.SS3#">
<cfdirectory action="list" name="taxForms" directory="#thisPath#/taxforms">
<cfform name="taxList" method="post" action="TaxFile3.cfm">
<cfselect query="taxForms" value="name" size="10" required="yes" multiple="no"
<cfinput type="Submit" name="OK" label="OK">
<!--- Use hidden fields to pass the first name, last name, and the three parts of
the SSN# to the tax form. Also, create a hidden field for the filerID variable. --->
<cfinput type="hidden" name="FirstName" value="#getEmpInfo.FirstName#">
<cfinput type="hidden" name="LastName" value="#getEmpInfo.LastName#">
<cfinput type="hidden" name="Phone" value="#getEmpInfo.Phone#">
<cfinput type="hidden" name="SS1" value="#form.SS1#">
<cfinput type="hidden" name="SS2" value="#form.SS2#">
<cfinput type="hidden" name="SS3" value="#form.SS3#">
<cfinput type="hidden" name="taxFiler" value="#filerID#">

The third ColdFusion page uses the cfpdfform and cfpdfformparam tags to populate the tax form with the user information. ColdFusion displays the tax prefilled tax form in the browser window where the user can complete the rest of the form fields. When the user clicks the submit button, Acrobat sends the completed PDF form to the ColdFusion processing page.

Note: To prefill forms, map each PDF form field name to the corresponding data element in a cfpdfformparam tag. To view the form fields, open the form in Acrobat Professional and select Forms > Edit Forms in Acrobat. For more information about prefilling forms, see Manipulating PDF Forms in ColdFusion.


information from the database query and the login form. Because no destination is
specified, ColdFusion displays the interactive PDF form in the browser. A hidden field
in the PDF form contains the name of the output file to write. It is a combination of
the user name and the last three numerals of the user SSN#. The submit button added to
the form created in Acrobat contains a URL to the ColdFusion processing page. --->

<cfpdfform source="taxForms/#form.myTaxForm#" action="populate">
<cfif "taxForms/#form.myTaxForm#" is "taxForms/f1040.pdf">
<cfpdfformparam name="f1_04(0)" value="#form.Firstname#">
<cfpdfformparam name="f1_05(0)" value="#form.Lastname#">
<cfpdfformparam name="f2_115(0)" value="#form.Phone#">
<cfpdfformparam name="f1_06(0)" value="#form.SS1#">
<cfpdfformparam name="f1_07(0)" value="#form.SS2#">
<cfpdfformparam name="f1_08(0)" value="#form.SS3#">
<cfpdfformparam name="filerID" value="#form.taxFiler#_1040">
<cfelseif "taxForms/#form.myTaxForm#" is "taxForms/f1040ez.pdf">
<cfpdfformparam name="f1_001(0)" value="#form.Firstname#">
<cfpdfformparam name="f1_002(0)" value="#form.Lastname#">
<cfpdfformparam name="f1_070(0)" value="#form.Phone#">
<cfpdfformparam name="f1_003(0)" value="#form.SS1#">
<cfpdfformparam name="f1_004(0)" value="#form.SS2#">
<cfpdfformparam name="f1_005(0)" value="#form.SS3#">
<cfpdfformparam name="filerID" value="#form.taxFiler#_1040ez">

The fourth ColdFusion page uses the cfpdfform tag to process the PDF post submission and generate an output file. The filename is generated from the value of the hidden field in the tax form. The processddx action of the cfpdf tag uses the DDX instructions in the watermark.ddx file to generate a text-string watermark and apply it to the form.
The following code shows the contents of the watermark.ddx file:

<DDX xmlns=""
xsi:schemaLocation=" coldfusion_ddx.xsd">
<PDF result="Out1">
<PDF source="Doc1">
<Watermark rotation="30" opacity="65%">
<StyledText><p font-size="85pt" font-weight="bold" color="gray"

<!--- 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"/>

<cfpdfform action="populate" source="#PDF.content#"
destination="FiledForms\#fields.filerID#.pdf" overwrite="yes"/>

<!--- The following code verifies that the DDX file exists and the DDX instructions are
valid. --->
<cfif IsDDX("watermark.ddx")>
<!--- The following code uses the processddx action of the cfpdf tag to create a text-
string watermark. --->

<!--- This code creates a structure for the input files. --->
<cfset inputStruct=StructNew()>
<cfset inputStruct.Doc1="FiledForms\#fields.filerID#.pdf">

<!--- This code creates a structure for the output file. --->
<cfset outputStruct=StructNew()>
<cfset outputStruct.Out1="FiledForms\#fields.filerID#.pdf">

<!--- This code processes the DDX instructions and applies the watermark to the form. --->
<cfpdf action="processddx" ddxfile="watermark.ddx" inputfiles="#inputStruct#" outputfiles="#outputStruct#" name="Final">

<h3>Tax Form Completed</h3>
<p>Thank you for filing your tax form on line. Copy this URL to view or download your filed
tax form:</p>
<a href="http://localhost:8500/Lion/FiledForms/#fields.filerID#.pdf">
Link to your completed tax form</a>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy