ColdFusion 9.0 Resources |
Adding and removing watermark imagesUse the addWatermark and removeWatermark actions to add and remove watermarks from PDF documents. You can create a watermark and apply it to a PDF document in one of the following ways:
Note: You can also use the Watermark or Background DDX
elements with the processddx action to create a
text-string watermark. For more information, see Using DDX to perform advanced tasks.
Using an image file as a watermarkThe following example shows how to specify an image file as a watermark: <cfpdf action="addWatermark" source="artBook.pdf" image="../cfdocs/images/artgallery/raquel05.jpg" destination="output.pdf" overwrite="yes"> By default, ColdFusion centers the image on the page, sets the opacity of the image to 3 out of 10 (opaque), and displays the image in the background of each page in the output file. In the following example, ColdFusion displays the watermark in the foreground, offset 100 pixels from the left margin of the page and 100 pixels from the bottom margin of the page. Because the opacity is set to 1, the image does not obscure the page content. <cfpdf action="addWatermark" source="artBook.pdf" image="../cfdocs/images/artgallery/raquel05.jpg" destination="output.pdf" overwrite="yes" foreground="yes" opacity=1 showOnPrint="no" position="100,100"> For a complete list of attributes and settings, see the cfpdf tag in the CFML Reference. With the ColdFusion 9 release, the addWatermark action now supports the rgb and argb formats also. The following example shows that if you set the parameters for a new image to rgb or argb and then use the cfpdf action=addwatermark, ColdFusion allows this action: <!---setting the argb format for myImage---> <cfset myImage = ImageNew("",200,200,"argb","gray")> <!---adding watermark for myImage---> <cfpdf action="addwatermark" rotation="45" foreground="true" image="#myImage#" source="RemoveArts.pdf" destination="dest.pdf" overwrite="yes"> Using a variable that contains an image fileYou can specify a variable that contains an image as a watermark. The following example shows how to create a form from which the user can select an image: <!--- The following code creates a form where you can choose an image to use as a watermark. ---> <h3>Choosing a Watermark</h3> <p>Please choose the image you would like to use as a watermark.</p> <!--- Create the ColdFusion form to select an image. ---> <table> <cfform action="addWatermark2.cfm" method="post" enctype="multipart/form-data"> <tr> <td><img src="../cfdocs/images/artgallery/maxwell01.jpg"/><br/> <cfinput type="radio" name="art" value="../cfdocs/images/artgallery/maxwell01.jpg" checked="yes"> Birch Forest</td> <td><img src="../cfdocs/images/artgallery/raquel05.jpg"/><br/> <cfinput type="radio" name="art" value="../cfdocs/images/artgallery/raquel05.jpg"> Lounging Woman</td> <td><img src="../cfdocs/images/artgallery/jeff01.jpg"/><br/> <cfinput type="radio" name="art" value="../cfdocs/images/artgallery/jeff01.jpg">Celebration</td> <td><img src="../cfdocs/images/artgallery/paul01.jpg"/><br/> <cfinput type="radio" name="art" value="../cfdocs/images/artgallery/paul01.jpg">Guitarist </td> </tr> </table> <br/> <cfinput type="Submit" name="submit" value="Submit"></p> </cfform> The processing page uses the image selected from the form as the watermark for a PDF file: <!--- ColdFusion applies the image selected from the form as the watermark in a PDF document by using the input variable form.art. ---> <cfpdf action="addwatermark" source="check.pdf" image="#form.art#" destination="output.pdf" foreground="yes" overwrite="true"> <p>The watermark has been added to your personalized checks.</p> Using a ColdFusion image as a watermarkYou can specify a ColdFusion image as a watermark. You can extract an image from a database and manipulate the image in memory, but you don’t have to write the manipulated image to a file. Instead, you can apply the manipulated image as a watermark in a PDF document. In the following example, the first ColdFusion page extracts images from a database and populates a pop-up menu with the titles of the artwork: <!--- Create a query to extract artwork from the cfartgallery database. ---> <cfquery name="artwork" datasource="cfartgallery"> SELECT ARTID, ARTNAME, LARGEIMAGE FROM ART ORDER BY ARTNAME </cfquery> <!--- Create a form that lists the artwork titles generated by the query. Set the value to LARGEIMAGE so that the image file is passed to the processing page. ---> <cfform action="addWatermarkB.cfm" method="post"> <p>Please choose a title:</p> <cfselect name="art" query="artwork" display="ARTNAME" value="LARGEIMAGE" required="yes" multiple="no" size="8"> </cfselect> <br/> <cfinput type="submit" name="submit" value="OK"> </cfform> The action page generates a ColdFusion image from the selected file by using the cfimage tag. The ImageScaleToFit function resizes the image and applies the bicubic interpolation method to improve the resolution. To use the manipulated image as a watermark, specify the image variable, as the following example shows: <!--- Verify that an image file exists and is in a valid format. ---> <cfif IsImageFile("../cfdocs/images/artgallery/#form.art#")> <!--- Use the cfimage tag to create a ColdFusion image from the file chosen from the list. ---> <cfimage source="../cfdocs/images/artgallery/#form.art#" action="read" name="myWatermark"> <!--- Use the ImageScaleToFit function to resize the image by using the bicubic interpolation method for better resolution. ---> <cfset ImageScaleToFit(myWatermark,450,450,"bicubic")> <!--- Use the ColdFusion image variable as the watermark in a PDF document. ---> <cfpdf action="addWatermark" source="title.pdf" image="#myWatermark#" destination="watermarkTitle.pdf" overwrite="yes"> <cfelse> <p>I'm sorry, no image exists for that title. Please click the Back button and try again.</p> </cfif> For more information on ColdFusion images, see Creating and Manipulating ColdFusion Images. Creating a text image and using it as a watermarkYou can use the ImageDrawText function to create a text image in ColdFusion and apply the image as a watermark, as the following example shows: <!--- Create a blank image that is 500 pixels square. ---> <cfset myImage=ImageNew("",500,500)> <!--- Set the background color for the image to white. ---> <cfset ImageSetBackgroundColor(myImage,"white")> <!---Clear the rectangle specified on myImage and apply the background color. ---> <cfset ImageClearRect(myImage,0,0,500,500)> <!--- Turn on antialiasing. ---> <cfset ImageSetAntialiasing(myImage)> <!--- Draw the text. ---> <cfset attr=StructNew()> <cfset attr.size=50> <cfset attr.style="bold"> <cfset attr.font="Verdana"> <cfset ImageSetDrawingColor(myImage,"blue")> <cfset ImageDrawText(myImage,"PROOF",100,250,attr)> <!--- Write the text image to a file. ---> <cfimage action="write" source="#myImage#" destination="text.tiff" overwrite ="yes"> <!--- Use the text image as a watermark in the PDF document. ---> <cfpdf action="addwatermark" source="c:/book/1.pdf" image="text.tiff" destination="watermarked.pdf" overwrite="yes"> For more information on ColdFusion images, see Creating and Manipulating ColdFusion Images. For an example of using DDX elements to create a text-string watermark, see Adding text-string watermarks. Using a PDF page as a watermarkUse the copyFrom attribute to create a watermark from the first page of a PDF file and apply it to another PDF document. In the following example, ColdFusion creates a watermark from the first page of image.PDF, applies the watermark to the second page of artBook.pdf, and writes the output to a new file called output.pdf: <cfpdf action="addWatermark" copyFrom="image.pdf" source="artBook.pdf" pages="2" destination="output.pdf" overwrite="yes"> In this example, image.pdf appears in the background of the second page of artBook.pdf. ColdFusion does not change the size of the watermark image to fit the page. The page used as a watermark can contain text, graphics, or both. Removing watermarksUse the removeWatermark action to remove a watermark from one or more pages in a PDF document. The following example shows how to remove a watermark from the entire PDF document and write the document to a new output file: <cfpdf action="removeWatermark" source="artBook.pdf" destination="noWatermark.pdf"> The following example shows how to remove a watermark from the first two pages of a PDF document and overwrite the source document: <cfpdf action="removeWatermark" source="artBook.pdf" destination="artBook.pdf" overwrite="yes" pages="1-2"> Because the source and the destination are the same and the overwrite attribute is set to yes, ColdFusion overwrites the source file with the output file. |