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


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

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>






it’s an known issue, please vote for it


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">

	//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", "").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 */

		/* 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 */

		// flattern form 

	catch (java.language.Exception de) {
		FileErrorMessage = de;
	//Close and create destination pdf file