Attempting to add a watermark to a pdf generates a null pointer exception

Hi there,
I’m running on 5.2.4.37 using the latest PDF extension 1.0.0.66.

This is the code that renders the output below. I’m dumping the outPath and proving that both the pdf file and the watermark image I want to use exist before executing the cfpdf tag, which blows up. I can’t determine if the combination of attributes is incorrect for the addWatermark action, or if this is a bug in system. The pdf file opens correctly so I doubt it’s corrupt, so I’ve run out of things to try. If anyone has experience adding watermarks to PDFs, let me know how you pulled it off . I’ll attempt to add a screenshot of the error I get (or paste it in if that fails).

	<cfif url.draft EQ 1>

		<cfscript>
		WriteDump(outPath);
		WriteDump(fileexists(outPath))
		WriteDump(fileexists("/Users/johnbarbic/workspace/centerline/wwwroot/images/po-watermark.png"));


		</cfscript>


	<cfpdf
		action="addWatermark"
		opacity="6"
		source="#outPath#"
		image="/Users/johnbarbic/workspace/centerline/wwwroot/images/po-watermark.png"
		destination="/Users/johnbarbic/Desktop/test.pdf"
		overwrite="yes">

	</cfif>

it’s an known issue, please vote for it
https://luceeserver.atlassian.net/browse/LDEV-1519

Thanks for letting me know.

Until, or even after, cfpdf is working again in Lucee 5, you can use the following itext code to add a watermark image to a pdf file.

<!--- Name of destination file --->
<CFSET PDFDestFile = "destfile_#dateFormat(now(), 'DDMMYYY')#-#timeFormat(now(), 'HHMMSS')#">

<!--- Absolute path to destination file in temp folder --->
<CFSET FullPathToOutputFile = "#Application_datapath#temp/#PDFDestFile#.pdf">

<CFSCRIPT>
	//Source file
	fullPathToInputFile = "#Application_FilePath#/#inputfile#";
	//Destination file in temp directory
	fullPathToOutputFile1 = "#FullPathToOutputFile#";

	try {
		//Create PDF reader 
		pdfReader1 = createObject("java", "com.lowagie.text.pdf.PdfReader").init(fullPathToInputFile);
		//Fill form fields
		outputStream1 = createObject("java", "java.io.FileOutputStream").init(fullPathToOutputFile1);
		pdfStamper1 = createObject("java", "com.lowagie.text.pdf.PdfStamper").init(pdfReader1, outputStream1);
	
		/* Create image object for watermark image */
		image = createobject("java", "com.lowagie.text.Image");

		/* Read watermark image */
		img = image.getInstance('#Application_datapath#temp/Watermark.png');

		x = #WatermarkXPosValue#;
		y = #WatermarkYPosValue#;

		/* Rotate image for vertical alignment */
		img.setRotationDegrees(90);

		/* Add watermark image to every page */

		i = 0;
		/* Loop PDF pages */
		while (i LT pdfReader1.getNumberOfPages()) {
			i = i + 1;
			/* Read lopoed page */
			pdfContentByte = pdfStamper1.getUnderContent( javacast("int", i) );
			/* Set position in image */
			img.setAbsolutePosition(javacast("float", x),javacast("float", y));

			/* Add watermark image to looped page */
			pdfContentByte.addImage(img);
		}

		// flattern form 
		pdfStamper1.setFormFlattening(true);

	}
	catch (java.language.Exception de) {
		FileErrorMessage = de;
	}
	//Close and create destination pdf file
	pdfStamper1.close();
	outputStream1.close();
</CFSCRIPT>
2 Likes