How to secure your PDF files using ColdFusion

At 39 Degrees North we had a requirement to have PDF files on our website that was only for viewing and not for printing or anything else. I posted this question to the Indiana University web community and had two major suggestions , one was using Adobe Acrobat to secure the files (the cheaper solution costing about 750$ ) and the more expensive LiveCycle services from Adobe. The cheaper solution was good for our purpose. What I did not discover until later on was that ColdFusion can do exactly the same thing that Adobe Acrobat can do using the cfpdf tag and since we already had ColdFusion it was free which is better than  cheap. I wanted to share that code. Its nothing fancy just a page with a form where you can put the directory with the PDF files and the same page processes all the PDF files in that directory and secures it providing only view access.

Heres a sample PDF protected using this method http://egis.39dn.com/eGISClark/PlatsEncrypted/Pleasant_Run.pdf 

<cfsetting requesttimeout="7200"/>
<cfif structKeyExists(form, "directorylocation")>

<cfif directoryExists(form.directorylocation)>
<cfset counter = 0/>
<cfdirectory action="LIST" directory="#form.directorylocation#" name="dirlist" filter="*.pdf">

<cfloop query="dirlist">
<cfset counter = counter + 1/>
<cfoutput>
<cfoutput> #counter#</cfoutput>

</cfoutput>
<cfif isPDFFile(#dirlist.directory#&"\"&#dirlist.name#)>
<cfpdf action = "protect" 
  source = "#dirlist.directory#\#dirlist.name#" 
  newOwnerPassword = "PASSWORDHERE" 
  permissions = "None" 
  encrypt = "AES_128" 
  overwrite = "yes"
  destination= "#dirlist.directory#Encrypted\#dirlist.name#"
  >
</cfif>
</cfloop>
<cfelse>
<cfset error = "Invalid Directory " & form.directorylocation />
<cfabort showerror="#error#">
</cfif>

<cfelse>
<cfform name="InputLocation" action="" method="post">
<cflayout type="vbox" >
<cflayoutarea>
PDF Directory: <cfinput name="directorylocation" type="text" />
</cflayoutarea>
<cflayoutarea>
<cfinput name="submit" type="submit" />
</cflayoutarea>

</cflayout>

</cfform>
</cfif>