Skip to main content
Skip table of contents

Watermark examples

These examples were written in response to questions such as: 


transparentwatermark

JAVA
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfResources;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;

import java.io.File;

public class TransparentWatermark {
    public static final String DEST = "./target/sandbox/stamper/transparent_watermark.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hero.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new TransparentWatermark().manipulatePdf(DEST);
    }

    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 paragraph = new Paragraph("This watermark is added UNDER the existing content")
                .setFont(font)
                .setFontSize(15);

        Canvas canvasWatermark1 = new Canvas(under, pdfDoc.getDefaultPageSize())
                .showTextAligned(paragraph, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark1.close();
        PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
        over.setFillColor(ColorConstants.BLACK);
        paragraph = new Paragraph("This watermark is added ON TOP OF the existing content")
                .setFont(font)
                .setFontSize(15);

        Canvas canvasWatermark2 = new Canvas(over, pdfDoc.getDefaultPageSize())
                .showTextAligned(paragraph, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark2.close();
        paragraph = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
                .setFont(font)
                .setFontSize(15);
        over.saveState();

        // Creating a dictionary that maps resource names to graphics state parameter dictionaries
        PdfExtGState gs1 = new PdfExtGState();
        gs1.setFillOpacity(0.5f);
        over.setExtGState(gs1);
        Canvas canvasWatermark3 = new Canvas(over, pdfDoc.getDefaultPageSize())
                .showTextAligned(paragraph, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        canvasWatermark3.close();
        over.restoreState();

        pdfDoc.close();
    }
}
C#
using System;
using System.IO;
using iText.IO.Font;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Stamper 
{
    public class TransparentWatermark 
    {
        public static readonly String DEST = "results/sandbox/stamper/transparent_watermark.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hero.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new TransparentWatermark().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            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 paragraph = new Paragraph("This watermark is added UNDER the existing content")
                    .SetFont(font)
                    .SetFontSize(15);
            
            Canvas canvasWatermark1 = new Canvas(under, pdfDoc.GetDefaultPageSize())
                    .ShowTextAligned(paragraph, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark1.Close();
            PdfCanvas over = new PdfCanvas(pdfDoc.GetFirstPage());
            over.SetFillColor(ColorConstants.BLACK);
            paragraph = new Paragraph("This watermark is added ON TOP OF the existing content")
                    .SetFont(font)
                    .SetFontSize(15);
            
            Canvas canvasWatermark2 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                    .ShowTextAligned(paragraph, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark2.Close();
            paragraph = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
                    .SetFont(font)
                    .SetFontSize(15);
            over.SaveState();
            
            // Creating a dictionary that maps resource names to graphics state parameter dictionaries
            PdfExtGState gs1 = new PdfExtGState();
            gs1.SetFillOpacity(0.5f);
            over.SetExtGState(gs1);
            Canvas canvasWatermark3 = new Canvas(over, pdfDoc.GetDefaultPageSize())
                    .ShowTextAligned(paragraph, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            canvasWatermark3.Close();
            over.RestoreState();
            
            pdfDoc.Close();
        }
    }
}

transparentwatermark2

JAVA
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;

import java.io.File;

public class TransparentWatermark2 {
    public static final String DEST = "./target/sandbox/stamper/transparent_watermark2.pdf";
    public static final String IMG = "./src/main/resources/img/itext.png";
    public static final String SRC = "./src/main/resources/pdfs/pages.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new TransparentWatermark2().manipulatePdf(DEST);
    }


    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
        Paragraph paragraph = new Paragraph("My watermark (text)")
                .setFont(font)
                .setFontSize(30);
        ImageData img = ImageDataFactory.create(IMG);

        float w = img.getWidth();
        float h = img.getHeight();

        PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.5f);

        // Implement transformation matrix usage in order to scale image
        for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {

            PdfPage pdfPage = pdfDoc.getPage(i);
            Rectangle pageSize = pdfPage.getPageSizeWithRotation();

            // When "true": in case the page has a rotation, then new content will be automatically rotated in the
            // opposite direction. On the rotated page this would look as if new content ignores page rotation.
            pdfPage.setIgnorePageRotationForContent(true);

            float x = (pageSize.getLeft() + pageSize.getRight()) / 2;
            float y = (pageSize.getTop() + pageSize.getBottom()) / 2;
            PdfCanvas over = new PdfCanvas(pdfDoc.getPage(i));
            over.saveState();
            over.setExtGState(gs1);
            if (i % 2 == 1) {
                doc.showTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            } else {
                over.addImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), false);
            }
            over.restoreState();
        }

        doc.close();
    }
}
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Stamper 
{
    public class TransparentWatermark2 
    {
        public static readonly String DEST = "results/sandbox/stamper/transparent_watermark2.pdf";
        public static readonly String IMG = "../../../resources/img/itext.png";
        public static readonly String SRC = "../../../resources/pdfs/pages.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new TransparentWatermark2().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            Paragraph paragraph = new Paragraph("My watermark (text)")
                    .SetFont(font)
                    .SetFontSize(30);
            ImageData img = ImageDataFactory.Create(IMG);
            
            float w = img.GetWidth();
            float h = img.GetHeight();
            
            PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.5f);
            
            // Implement transformation matrix usage in order to scale image
            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) 
            {
                PdfPage pdfPage = pdfDoc.GetPage(i);
                Rectangle pageSize = pdfPage.GetPageSizeWithRotation();
                
                // When "true": in case the page has a rotation, then new content will be automatically rotated in the
                // opposite direction. On the rotated page this would look as if new content ignores page rotation.
                pdfPage.SetIgnorePageRotationForContent(true);
                
                float x = (pageSize.GetLeft() + pageSize.GetRight()) / 2;
                float y = (pageSize.GetTop() + pageSize.GetBottom()) / 2;
                PdfCanvas over = new PdfCanvas(pdfDoc.GetPage(i));
                over.SaveState();
                over.SetExtGState(gs1);
                if (i % 2 == 1) 
                {
                    doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
                }
                else 
                {
                    over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), false);
                }
                over.RestoreState();
            }
            
            doc.Close();
        }
    }
}

transparentwatermark3

JAVA
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;

import java.io.File;

public class TransparentWatermark3 {
    public static final String DEST = "./target/sandbox/stamper/transparent_watermark3.pdf";
    public static final String IMG = "./src/main/resources/img/itext.png";
    public static final String SRC = "./src/main/resources/pdfs/pages.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new TransparentWatermark3().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.HELVETICA));
        Paragraph paragraph = new Paragraph("My watermark (text)").setFont(font).setFontSize(30);
        ImageData img = ImageDataFactory.create(IMG);

        float w = img.getWidth();
        float h = img.getHeight();

        PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.5f);

        // Implement transformation matrix usage in order to scale image
        for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
            PdfPage pdfPage = pdfDoc.getPage(i);
            Rectangle pageSize = pdfPage.getPageSize();
            float x = (pageSize.getLeft() + pageSize.getRight()) / 2;
            float y = (pageSize.getTop() + pageSize.getBottom()) / 2;
            PdfCanvas over = new PdfCanvas(pdfPage);
            over.saveState();
            over.setExtGState(gs1);
            if (i % 2 == 1) {
                doc.showTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
            } else {
                over.addImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), true);
            }
            over.restoreState();
        }

        doc.close();
    }
}
C#
using System;
using System.IO;
using iText.IO.Font;
using iText.IO.Font.Constants;
using iText.IO.Image;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Extgstate;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Stamper 
{
    public class TransparentWatermark3 
    {
        public static readonly String DEST = "results/sandbox/stamper/transparent_watermark3.pdf";
        public static readonly String IMG = "../../../resources/img/itext.png";
        public static readonly String SRC = "../../../resources/pdfs/pages.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new TransparentWatermark3().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            PdfFont font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(StandardFonts.HELVETICA));
            Paragraph paragraph = new Paragraph("My watermark (text)").SetFont(font).SetFontSize(30);
            ImageData img = ImageDataFactory.Create(IMG);
            
            float w = img.GetWidth();
            float h = img.GetHeight();
            
            PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.5f);
            
            // Implement transformation matrix usage in order to scale image
            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) {
                PdfPage pdfPage = pdfDoc.GetPage(i);
                Rectangle pageSize = pdfPage.GetPageSize();
                float x = (pageSize.GetLeft() + pageSize.GetRight()) / 2;
                float y = (pageSize.GetTop() + pageSize.GetBottom()) / 2;
                PdfCanvas over = new PdfCanvas(pdfPage);
                over.SaveState();
                over.SetExtGState(gs1);
                if (i % 2 == 1) 
                {
                    doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
                }
                else 
                {
                    over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), true);
                }
                over.RestoreState();
            }
            
            doc.Close();
        }
    }
}

Resources



JavaScript errors detected

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

If this problem persists, please contact our support.