ColdFusion 8.0.1 - Easier to add PDF Watermarks

This post is more than 2 years old.

ColdFusion 8.0.1 makes it much easier to add watermarks to PDF documents. In the past you had to either use an image or another PDF, but now you can simply pass in text. You can even pass in styled text. Here is a simple example.

First we generate a PDF dynamically.

<cfdocument format="pdf" name="mypdf"> <cfloop index="x" from="1" to="9"> <p> Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. </p> </cfloop> </cfdocument>

Now let's add the watermark:

<cfpdf action="addWatermark" text="<b>TOP SECRET!</b>" source="mypdf" foreground="true">

The foreground attribute is critical for PDFs made with cfdocument. If you don't use it - your watermark will be behind the text.

Now I can serve the PDF to the user:

<cfheader name="content-disposition" value="attachment; filename=""test.pdf"""/> <cfcontent type="application/pdf" variable="#toBinary(mypdf)#">

Note the toBinary thing. This is an bug that was not fixed in CF8. Even though "mypdf" is a PDF document, when I performed the addWatermark action, I converted what was pure binary data into a PDF object recognized by ColdFusion. If I had used destinaiton= in the cfpdf tag, it would have worked fine, but I wanted to serve the document directly to the user, so I had to wrap it with toBinary.

Anyway - even with that little hitch at the end, it's far easier now to add watermarks to PDFs!

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by David Buhler posted on 4/4/2008 at 8:59 PM


I wasn't aware of the toBinary() method.

Comment 2 by Anthony Webb posted on 4/4/2008 at 10:09 PM

Thanks for the example Ray, can't wait to try it out.

One question though, the whole cfheader thing, is that file "test.pdf" an actual file placeholder that I should place in my webroot?

I've been using the following to show PDF's:
<cfcontent type="application/pdf">

Which works great in al browsers on windows, and mac safari is fine, but in mac firefox for some reason it will actually try to kick off a download?

I've been looking for a solution for some time now, and thought I'd try your code from the example above, but was a little confused about the whole "test.pdf" thing.

Thanks again Ray.

Comment 3 by Raymond Camden posted on 4/4/2008 at 10:13 PM

No, you dont make test.pdf. This tells the code to serve the file up with a name already (test.pdf).

Comment 4 by Anthony Webb posted on 4/4/2008 at 10:23 PM

Thanks for the clarification Ray. I'll give it a shot.

Comment 5 by Joshua Curtiss posted on 4/4/2008 at 10:35 PM

Oh yes!! BSG, can't wait!! :-)

Comment 6 by Gary Funk posted on 4/5/2008 at 2:14 AM

@Ray. Are you readfy to order your BSGF pizza tonight?

Just call up and say "I want to order a fracking pizza." They will ask "How long has Starbuck been gone?" and you say "Just over two months."

Yes, I know you think it's a prank.

Comment 7 by t b posted on 4/5/2008 at 1:51 PM

Regarding Anthony Webbs firefox issue could it be the download pdf extension running. This extension causes firefox to offer to download the file rather than open a pdf that is clicked on. Just a thought,

Comment 8 by microgluf posted on 9/14/2008 at 7:31 PM

I know it's an old post, but I thought I would comment on Anthony's issue anyway :-)

I found the cfcontent method to be more accurate across browsers/os
I'm on OSX and I found that safari & ff have sometimes odd behaviours, being a bit more anal on the quality and proper syntax of the header response.

I use
<cfheader name="Content-Disposition" value="inline; filename=#outFilename#" />
<cfheader name="Content-Disposition" value="attachment; filename=#outFilename#" />
<cfcontent type="application/pdf" file="#ExpandPath( outFilename )#" deletefile="yes" />

This allows you to control the way you like your delivery to be.
** note this example already uses a temporary disk file,

The drawback is that cfcontent uses a CF process for the delivery rather than leaving that task to the frontend.
So you can use the cflocation delivery instead.
<cflocation url="./#outFilename#" addtoken="no" />

** each case is different, just a thougth

Comment 9 by Kurt posted on 7/23/2009 at 9:33 PM

You know what?
That binary trick worked for me, but the watermark doesn't print.
Isn't it supposed to print?
If so, how do you make it print?

Comment 10 by Raymond Camden posted on 7/23/2009 at 9:40 PM

So you see the watermark in Acrobat, but not on print?

Comment 11 by Kurt posted on 7/24/2009 at 2:03 AM

Here's how you make it print (with a few more options added):
<cfpdf action="addWatermark" text="<b>SAMPLE COPY</b>" source="printOnDemand" foreground="true" showonprint = "yes" rotation = "45" position = "50,30" opacity = "1">

Comment 12 by Grant Powell posted on 12/6/2013 at 7:19 PM

Anyone know how to stylize the text? I've researched half a dozen demos using DDX and not one single one them works. Has anyone successfully manipulated text in PDF? I have not yet found a successful implementation.