Skip to main content
Skip table of contents

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?

I currently use:

 

PdfContentByte canvas = pdfStamper.getUnderContent(i);
ColumnText.showTextAligned(
    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")
                .setFont(font).setFontSize(15);
        new Canvas(under, pdfDoc, pdfDoc.getDefaultPageSize())
                .showTextAligned(p, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
        over.setFillColor(ColorConstants.BLACK);
        p = new Paragraph("This watermark is added ON TOP OF the existing content")
                .setFont(font).setFontSize(15);
        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")
                .setFont(font).setFontSize(15);
        over.saveState();
        PdfExtGState gs1 = new PdfExtGState();
        gs1.setFillOpacity(0.5f);
        over.setExtGState(gs1);
        new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
                .showTextAligned(p, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        over.restoreState();
        pdfDoc.close();
    }
}

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 How to add a watermark to a page with an opaque image? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.