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. ---> 
<table> 
<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"  
            Value="Partnership">Partnership</td></tr> 
    <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> 
    </tr> 
</cfform> 
</table>

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:

<!--- Create a variable for the business type selected from the form. ---> 
<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\#filelist.name#"> 
    <cfpdf action="GetInfo" source="#fPath#" name="kInfo"></cfpdf> 
    <cfif #kInfo.keywords# contains "#bizType#"> 
        <cfset tempPath=#tempPath# & #fPath# & ","> 
    </cfif> 
</cfloop> 
 
<!--- 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>