Hello All,

I am using PDFBox 2.0 dev snapshot to create a PDF with a watermark. My use
case is to create PDF with watermark. I am using two kinds of watermarks:
a) text string and b) image

When I am using the watermark text or image, the watermark in the generated
PDF is not transparent but it is overlay on top of the PDF which makes the
PDF text unreadable. I have uploaded sample output PDFs for your reference:

Text string: http://www.filedropper.com/unicodewatermarktext
Binary Image: http://www.filedropper.com/imagewatermarktest

In 1.8.x, I used to implement the transparency of the watermark using the
appendRawCommands("/TransparentState gs\n"); method which is deprecated in
2.0.

In 2.0 I am implementing the transparency by creating
PDExtendedGraphicsState object and setting it on PDResource.

Below is the code snippet that I have:

PDPageContentStream contentStream = new PDPageContentStream(pdf, page,
true, true);

PDResources resources = page.getResources();
if (resources != null) {
PDExtendedGraphicsState extendedGraphicsState = new
PDExtendedGraphicsState();
// Set the transparency/opacity
extendedGraphicsState.setNonStrokingAlphaConstant(0.5f);
PDExtendedGraphicsState graphicsState =
resources.getExtGState(COSName.TRANSPARENCY);
if (graphicsState == null) {
graphicsState = extendedGraphicsState;
}
resources.add(graphicsState);
}

//deprecated method in 2.0
//contentStream.appendRawCommands("/TransparentState gs\n");
contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
contentStream.beginText();
contentStream.setFont(font, 75);
AffineTransform at = new AffineTransform(2, 0, 0, 2, rect.getUpperRightX()
/ 4, rect.getUpperRightY() / 4);
at.rotate(Math.toRadians(45));
Matrix matrix = new Matrix(at);
contentStream.setTextMatrix(matrix);
//contentStream.drawString( watermark );
contentStream.showText(watermark);
contentStream.endText();
contentStream.close();

Any idea what I am missing? Any pointers how to make watermarks (text and
image) transparent in 2.0 would be of great help!

TIA,
Rakshit

Reply via email to