How to add a watermark to a page with an opaque image?

Is it possible to add a watermark that will be transparent on top of an image?

31st May 2016

I currently use:


PdfContentByte canvas = pdfStamper.getUnderContent(i);
    canvas, Element.ALIGN_CENTER, watermark, 298, 421, 45);
It works fine, but if the content consists of an image, the watermark is hidden by that image.

Posted on JIRA (our closed ticketing system for customers) on Mar 19, 2015

If you have opaque shapes in your PDF (such as images, but also colored shapes), you need to add the Watermark on top of the existing content:

PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamAfter(), new PdfResources(), pdfDoc);

Now the text will cover the images, but it may hide some important information. If you want to avoid this, you need to introduce transparency.

I have written a simple example that shows how this is done. It is called TransparentWatermark. Let's take a look at the result:

Three different watermarks

Three different watermarks

First I add the text "This watermark is added UNDER the existing content" under the existing content. Part of the text is hidden (as you indicate in your question). Then I add the text "This watermark is added ON TOP OF the existing content" on top of the existing content. This may be sufficient, unless you fear that some crucial information will get lost by covering the existing content. In that case, take a look at how I add the text "This TRANSPARENT watermark is added ON TOP OF the existing content":

protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
        PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
        PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.HELVETICA));
        Paragraph p = new Paragraph("This watermark is added UNDER the existing content")
        new Canvas(under, pdfDoc, pdfDoc.getDefaultPageSize())
                .showTextAligned(p, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
        p = new Paragraph("This watermark is added ON TOP OF the existing content")
        new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
                .showTextAligned(p, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        p = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
        PdfExtGState gs1 = new PdfExtGState();
        new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
                .showTextAligned(p, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);

Some extra tips and tricks:

  • Always use saveState() and restoreState() when you change the graphics state. If you don't you may get undesirable effects such as other content that is affected by the changes you make (e.g. you don't want all the content to become transparent).

  • The default rendering mode of text is "fill", hence I change the fill opacity.

  • In this case, I defined a fill opacity of 50% (0.5f). Choose any value between 0.0f and 1.0f if you want to change the transparency of the text.

Click this link if you want to see how to answer this question in iText 5.


Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now