You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Rakshit Ashtekar <ar...@tibco.com> on 2015/09/01 00:24:41 UTC

Implementing transparency for watermarks in writing PDF

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

Re: Implementing transparency for watermarks in writing PDF

Posted by Rakshit Ashtekar <ar...@tibco.com>.
Hi Tilman,

setGraphicsStateParameters(PDExtendedGraphicsState state) was the thing
missing :) Both text and image watermarks are transparent now. Thanks a lot
for your help!

-
Rakshit

On Mon, Aug 31, 2015 at 3:56 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 01.09.2015 um 00:24 schrieb Rakshit Ashtekar:
>
>> //deprecated method in 2.0
>> //contentStream.appendRawCommands("/TransparentState gs\n");
>>
>
> You could use
>
> setGraphicsStateParameters(PDExtendedGraphicsState state)
>
> from the source code
>
> public void setGraphicsStateParameters(PDExtendedGraphicsState state)
> throws IOException
>     {
>         writeOperand(resources.add(state));
>         writeOperator("gs");
>     }
>
>
> you don't even have to add it to the resources yourself.
>
> (I didn't test this. If it still doesn't work, try to create a "good" PDF
> with deprecated methods, and a "bad" PDF. We could then analyse the
> difference with PDFDebugger)
>
>
> Tilman
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Implementing transparency for watermarks in writing PDF

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 01.09.2015 um 00:24 schrieb Rakshit Ashtekar:
> //deprecated method in 2.0
> //contentStream.appendRawCommands("/TransparentState gs\n");

You could use

setGraphicsStateParameters(PDExtendedGraphicsState state)

from the source code

public void setGraphicsStateParameters(PDExtendedGraphicsState state) 
throws IOException
     {
         writeOperand(resources.add(state));
         writeOperator("gs");
     }


you don't even have to add it to the resources yourself.

(I didn't test this. If it still doesn't work, try to create a "good" 
PDF with deprecated methods, and a "bad" PDF. We could then analyse the 
difference with PDFDebugger)


Tilman

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org