Adding a watermark to a PDF using ColdFusion MX7 and iText

    Posted At : March 31, 2007 7:01 AM

I was recently asked to help create a basic e-document signature system. One of the requirements was to add a watermark to existing PDF documents. As you may know, ColdFusion MX7 ships with the iText library. iText is simple to use and has an extensive API.

The following is a proof of concept for watermarking a PDF document using ColdFusion. (Of course after coding it I found the following post that does the exact same thing)

<cfif NOT isdefined("form.submit")>

   <cfform enctype="multipart/form-data">
      <strong>PDF*:</strong><cfinput type="file" name="pdfFile" required="yes"><br />
      <strong>JPG*:</strong><cfinput type="file" name="jpgFile" required="yes"><br />
      New file name:<cfinput type="text" name="newFile"><br />
      X:<cfinput type="text" name="x" size="2" validate="integer">/Y:<cfinput type="text" name="y" size="2" validate="integer"><br />
      <cfinput type="submit" name="submit" value="submit">
   </cfform>

<cfelse>

   <!--- Set defualt position --->
   <cfif not isnumeric(form.x)>
      <cfset form.x = 0>
   </cfif>
   <cfif not isnumeric(form.y)>
      <cfset form.y = 0>
   </cfif>


   <!--- Upload PDF file --->
   <cffile action="upload" destination="#expandpath('.')#" filefield="pdfFile" nameconflict="makeunique">
   <cfset pdfFileName = file.ServerFile>
   <cfif not len(trim(form.newFile))>
      <cfset form.newFile = "#listfirst(pdfFileName,'.')#_signed.pdf">
   </cfif>

   <!--- Upload JPG file --->
   <cffile action="upload" destination="#expandpath('.')#" filefield="jpgFile" nameconflict="makeunique">
   <cfset jpgFileName = file.ServerFile>

   <CFSCRIPT>
Çspan style='color: #808080'ÈÇemÈ   ///////////////////////////////////////////////////////////////
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   // This function applies a jpg watermark to an existing PDF
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   // pdfFile - Relative path to pdf file
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   // jpgFile - Relative path to jpg file
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   // newFile - name of new PDF file
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   // x - horizontal position from bottom left of pdf
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   // y - vertical position from bottom left of pdf
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   //
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   // Author: Shlomy Gantz
Ç/emÈÇ/spanÈÇspan style='color: #808080'ÈÇemÈ   //
Ç/emÈÇ/spanÈ         
   watermarkPDF(pdfFileName,jpgFileName,form.newFile,form.x,form.y);      
               
   function watermarkPDF(pdfFile,jpgFile,newFile,x,y) {
               
            Çspan style='color: #808080'ÈÇemÈ   // get original document
Ç/emÈÇ/spanÈ               reader = CreateObject("java", "com.lowagie.text.pdf.PdfReader").init(expandpath(arguments.pdfFile));
            Çspan style='color: #808080'ÈÇemÈ   // create new document
Ç/emÈÇ/spanÈ               fileIO = CreateObject("java", "java.io.FileOutputStream").init(expandpath(arguments.newFile));
            Çspan style='color: #808080'ÈÇemÈ   // initiliza stamper
Ç/emÈÇ/spanÈ               stamper= CreateObject("java", "com.lowagie.text.pdf.PdfStamper").init(reader,FileIO);
            Çspan style='color: #808080'ÈÇemÈ   // create watermark images
Ç/emÈÇ/spanÈ               
               Image          = CreateObject("java", "com.lowagie.text.Image");
               jpg          = Image.getInstance(expandpath(arguments.jpgFile));
               jpg.setAbsolutePosition(arguments.x, arguments.y);
                  
            Çspan style='color: #808080'ÈÇemÈ   // stamp all pages with watermark
Ç/emÈÇ/spanÈ               i = 0;
               n = reader.getNumberOfPages();
               
               while (i lt n) {
                        i=i+1;

                        b = stamper.getUnderContent(javacast("Int",i));
                        b.addImage(jpg);
                     }
         
               stamper.close();

               }
   
   

   </CFSCRIPT>
   
   <cfoutput>
   <a href="#form.newFile#">Click here to download the signed PDF</a>
   </cfoutput>

</cfif>

Comments
Bruno Lowagie's Gravatar Thanks for the blog entry and the link to the other blog.
I've added both links to the FAQ entry:
http://itext.ugent.be/library/question.php?id=37
# Posted By Bruno Lowagie | 4/1/07 4:08 AM
Jeff's Gravatar The version that ships with coldfusion is an older version or has been changed from the current one.

Some things don't work as expected and causes errors.

I refactored the library to work with CFMX7.

http://blog.internetdatabases.com/?p=7


the original post before CFMX7 :

http://cephas.net/blog/2004/03/14/using-itext-pdf-...
# Posted By Jeff | 4/2/07 3:23 PM
Howard Fore's Gravatar @Jeff: Yeah, the version with CFMX 7 is 1.02, but ColdFusion 8 ships with 1.4, much better. You can get these numbers yourself by calling the iText Document.getVersion method.
# Posted By Howard Fore | 10/22/07 11:48 AM
Powered by BlogCFC

HostMySite.com Web Hosting Provided by www.HostMySite.com

Aggregated by fullasagoog.com
[Valid RSS]

Add to Google