Posted in ColdFusion | Posted on 07-13-2007 | 11,724 views
So today's CFPDF entry will detail how you can add a watermark to a PDF. Why would you do that? You may want to mark a PDF for security reasons. As an example, this weeks episode of Entourage featured M. Night Shyamalan (will his movies ever be good again?) giving Ari (a hollywood agent) a copy of his latest script. On every single page of the script, Ari's name was printed across the text. That way if the script was leaked to the Internet, M. Night would know exactly who to blame.
ColdFusion 8 gives up multiple ways to work with watermarks.
- First off - you can use an existing PDF or an image for your watermark. I had some issues with images which I'll talk about at the very end.
- You can set your watermark to be in the background or foreground.
- You can make your watermark only show up on screen and not in print.
- You can set a rotation.
- You can set opacity.
- You can position where the watermark is applied.
- You can put the watermark only on certain pages.
So as you can see, you have a lot of options. Let's start with a simple example. First I'll create a PDF for my movie script:
2<cfloop index="x" from="1" to="20">
3<p>
4doloras lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
5will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
6<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
7</p>
8</cfloop>
9</cfdocument>
Now I'll create a watermark. I'm going to use a PDF which means I'll need to save it to the file system as well:
2<h1>Raymond Camden</h1><br />
3<h1><cfoutput>#dateformat(now(),"short")# #timeformat(now(), "short")#</cfoutput></h1>
4</cfdocument>
5
6<cfset wfile = getTempFile(getTempDirectory(), "wfile")>
7<cffile action="write" file="#wfile#" output="#watermark#">
All I've done here is used my name and the current date and time. I then save it to a temporary file using ColdFusion's built in getTempFile/getTempDirectory functions.
Now let me add it as a watermark:
The copyFrom attribute tells CFPDF to copy the watermark from a file. I've set a rotation of 30 degrees and put the watermark in the foreground. My source is a PDF in memory (the one built above). I also set the watermark to show up when printing as well. Lastly I've positioned where the PDF shows up. I've attached the PDF to this blog entry as an attachment. Pretty simple, eh? Here is the complete template.
2<cfloop index="x" from="1" to="20">
3<p>
4doloras lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
5will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
6<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
7</p>
8</cfloop>
9</cfdocument>
10
11<cfset wfile = getTempFile(getTempDirectory(), "wfile")>
12<cfdocument format="pdf" name="watermark">
13<h1>Raymond Camden</h1><br />
14<h1><cfoutput>#dateformat(now(),"short")# #timeformat(now(), "short")#</cfoutput></h1>
15</cfdocument>
16
17<cffile action="write" file="#wfile#" output="#watermark#">
18
19
20<cfpdf action="addWatermark" copyFrom="#wfile#" rotation="30" foreground="true" source="mydocument" name="mydocument" showonprint="true" overwrite="true" position="200,0">
21
22
23<cfcontent type="application/pdf" reset="true" variable="#toBinary(mydocument)#">
So I mentioned that I had a few issues with images. Here is what I found, and I want to thank Adobe for their help in hashing this out. First - if you want to use an image you create on the fly, it must be grayscale. If you have an image on the file system, it can be any color model. Another problem I ran into was rotating the image using imageRotate. When I did it created a background that messed with the PDF. But I was able to switch to using CFPDF's rotate instead. I'm pasting the image example below for folks who want to see how that looks.
2
3<cfloop index="x" from="1" to="20">
4<p>
5doloras lorem upsom doloras paris hilton is my hero loreum ipsom dsoio foom an to dht end of the world
6will anyone actually read this probably not but let me put more realtext in so it flows a bit nicely
7<cfloop index="y" from="1" to="#randRange(1,9)#">This sentence will appear a random amount of time.</cfloop>
8</p>
9</cfloop>
10
11</cfdocument>
12
13<cfset textImage=ImageNew("",500,500,"grayscale","white")>
14
15<cfset ImageSetDrawingColor(textImage,"black")>
16
17<cfset attr=StructNew()>
18<cfset attr.size=50>
19<cfset attr.style="bold">
20<cfset attr.font="ArialMT">
21
22<cfset ImageSetAntialiasing(textImage, "on")>
23
24<cfset ImageDrawText(textImage,"Raymond Camden",50,50,attr)>
25
26<cfpdf action="addWatermark" image="#textImage#" rotation="30" foreground="true" source="mydocument" name="mydocument" showonprint="true" overwrite="true">
27
28
29<cfcontent type="application/pdf" reset="true" variable="#toBinary(mydocument)#">


The parameter 1 of function ToBinary, which is now coldfusion.pdf.PDFDocWrapper@11bb202 must be a Base-64 encoded string.
I am getting this error
Check the page numbers 1 in the source.
<cfpdf action="removeWatermark" source="my.pdf" destination="myNew.pdf" pages="1" >
Cheers
When I create my own watermark in CF and then attempt to remove it, everything works fine
But maybe instead he really IS going ahead in time--or it's you who's stuck in the middle. Before the confusion makes you lose your mind, I can give you this advice: when you see Ray next, tell him to "set the device to 2.342 and oscillating at 11 hertz". That's for Ray and all the other Lost fans. :-)
"1.21 gigawatts? 1.21 gigawatts? Great Scott!"
(For those not following, he was quoting Dr Emmet Brown in "Back to the Future". Mine is the reply that George McFly said to him next.)
Im trying to get a picture attached as a watermark, instead of text and its not working out as I expected, any tips
all im getting is one error after another, but when i copy your code exactly like it is, it works fine
(Attribute validation error for the CFPDF tag.
When the value of the ACTION attribute is ADDWATERMARK, the tag requires the attribute(s): SOURCE.)
heres the code as I have it now
<cfpdf action="Addwatermark"
destination="#request.maindrivePath#temppdf\#pdfname#"
overwrite="yes">
<cfpdfparam source="#request.maindrivePath#testfiles\#qgetinvoice.INVOICE_PDFFile#">
<cfpdfparam source="#request.maindrivePath#templates\#qgettemplate.COMPANY_Template#">
</cfpdf>
<cflocation url="temppdf/#pdfname#">
#request.maindrivePath#templates\#qgettemplate.COMPANY_Template#
ive tried it and now i get this wonderful error message:
The pdf image format is not supported on this operating system.
Use GetReadableImageFormats() and GetWriteableImageFormats() to see which image formats are supported.
<cfpdf
action = "addwatermark"
source = "#request.maindrivePath#testfiles\#qgetinvoice.INVOICE_PDFFile#"
image = "#request.maindrivePath#templates\#qgettemplate.COMPANY_Template#"
foreground = "No"
overwrite = "yes"
pages = "1"
rotation = "45"
showonprint = "yes">
destination = "#request.maindrivePath#temppdf\#pdfname#"
<cflocation url="temppdf/#pdfname#">
image = "#request.maindrivePath#templates\#qgettemplate.COMPANY_Template#"
points to a valid image? How about hard coding that to a jpg temporarily.
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
I dont know what im doing wrong with it now
Biggest tip I can tell folks when debugging is to run the LEAST amount of code possible.
destination = "#request.maindrivePath#temppdf\#pdfname#"
but when i go to this location its not there ;-(
avatar
The code as it is now
<cfpdf
action = "addwatermark"
source = "#request.maindrivePath#testfiles\#qgetinvoice.INVOICE_PDFFile#"
image = "#request.maindrivePath#templates\#qgettemplate.COMPANY_Template#"
foreground = "No"
overwrite = "yes"
pages = "1"
rotation = "45"
showonprint = "yes">
destination = "#request.maindrivePath#temppdf\#pdfname#
See the > after showonprint?
all I have to do now is make the image smaller to fit on the screen
Thanks for all your help
<cfpdf action = "read" name = "test" source = "D:\Dummy.pdf" >
<cfoutput>#test#</cfoutput>
The above lines of code on execution returns coldfusion.pdf.PDFDocWrapper@3c4c33
I assume this is some sort of wrapper, but then how can i get the text written in pdf?
In CF8, you have to use DDX, or use my pdfutils CFC (http://pdfutils.riaforge.org)
In CF9, Adobe added it as a feature to the cfpdf tag.
I used the watermark tecnique to create several templates containing logos, background and some text common to a range of certificates our system produces.
The system just adds that template to the individual cert data (pdf of the students info and cert uniqueID etc).
Doing it this way changed the processing time from 17 seconds (it was getting hung up on a tiled background image) to less that 400 ms.
The certificates look a million times better too as I created the source PDF directly from illustrator so they now print in HD.
Awesome.
Existing system: contains PDFs which consists of statistical data in Tabular format when i search for a particular Criteria it displays the contents of the statistical data(i.e; numbers).
what i want / Proposed system: same pdfs (consists of description in their properties) when i search for particular criteria it must show the summary/contents in the form of characters (reason is it will be helpful to the users to choose among the selected topics)given in the metadata or properties .
when we do searching we will see the list of search results along with some little description(the description must contain the "CRITERIA" such that i can understand whether the search item is useful to me or not)
regarding indexing i had done it using the admin page can u please tell me how to get to my requirement using the 4 custom column and where can i get it
As for how to use custom fields, please read the documentation on cfindex.
Please. Read. The. Docs. Seriously - look at cfindex. Also, I have a presentation you can watch on using Solr in CF in general.
1.i will search in the collection and get the set of pdfs containing CRITERIA
2.now script for accessing PDF by PDF and reading the lines that contains CRITERIA
But it looks complicated can u suggest whether its worthdoing.
As to the docs, they are in the Adobe CF docs.
As to your second comment, I do not understand what you are saying unfortunately.
so please give me the information or where can i get the information.
[Add Comment] [Subscribe to Comments]