CFPDF - Issues When Using Transparent Images as a Watermark
I saw an interesting question on the abode forums yesterday, about problems with watermarks and cfpdf. The issue involved using transparent png's or gif's as a watermark. The transparent parts of the image seem to be rendered as white, instead of maintaining their transparency.
As I was curious, I tried a number of different things but nothing seemed to work except a bit of iText magic. The work-around comes from an adaptation of two great iText examples. The code is very simple. It uses PdfGState to set the watermark to 50% opacity, but you can change that (and other properties like blendMode) as well.
If anyone knows a way around this issue (using cfpdf or ddx), I would love to hear it.
Update July 13,2009: This issue appears to be fixed in CF9 beta.
iText Example Java Source:
<!---
Add a centered watermark with 50% opacity
--->
<cfscript>
savedErrorMessage = "";
fullPathToInputFile = ExpandPath("mySourceFile.pdf");
fullPathToWatermark = ExpandPath("myTransparentImage.png");
fullPathToOutputFile = ExpandPath("mySourceFile_Watermarked.pdf");
try {
// create PdfReader instance to read in source pdf
pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init(fullPathToInputFile);
totalPages = pdfReader.getNumberOfPages();
// create PdfStamper instance to create new watermarked file
outStream = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile);
pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(pdfReader, outStream);
// Read in the watermark image
img = createObject("java", "com.lowagie.text.Image").getInstance(fullPathToWatermark);
// Use PdfGState to change fill,blendMode, etcetera as needed
gState = createObject("java", "com.lowagie.text.pdf.PdfGState").init();
gState.setFillOpacity(0.5);
// adding content to each page
p = 0;
while (p LT totalPages) {
p = p + 1;
// Prepare to place image on OVERcontent
content = pdfStamper.getOverContent( javacast("int", p) );
// Only needed if you are changing the opacity, blending, etcetera ..
content.setGState(gState);
// Center the watermark. Note - using deprecated methods for CF8/iText 1.4 compatability
rectangle = pdfStamper.getReader().getPageSizeWithRotation( javacast("int", p) );
x = rectangle.left() + (rectangle.width() - img.plainWidth()) / 2;
y = rectangle.bottom() + (rectangle.height() - img.plainHeight()) / 2;
img.setAbsolutePosition(x, y);
content.addImage(img);
WriteOutput("Watermarked page "& p &"<hr>");
}
WriteOutput("Finished!");
}
catch (java.lang.Exception e) {
savedErrorMessage = e;
}
// closing PdfStamper will generate the new PDF file
if (IsDefined("pdfStamper")) {
pdfStamper.close();
}
if (IsDefined("outStream")) {
outStream.close();
}
</cfscript>
<!--- show any errors --->
<cfif len(savedErrorMessage) gt 0>
ERROR - Unable to create document
<cfdump var="#savedErrorMessage#">
</cfif>
0 comments:
Post a Comment