Skip to main content
Skip table of contents

Adding watermarks to images

These examples were written in answer to the following questions:


watermarkedimages1

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.images;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.properties.TextAlignment;

import java.io.File;

public class WatermarkedImages1 {
    public static final String DEST = "./target/sandbox/images/watermarked_images1.pdf";

    public static final String IMAGE1 = "./src/main/resources/img/bruno.jpg";
    public static final String IMAGE2 = "./src/main/resources/img/dog.bmp";
    public static final String IMAGE3 = "./src/main/resources/img/fox.bmp";
    public static final String IMAGE4 = "./src/main/resources/img/bruno_ingeborg.jpg";

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

        new WatermarkedImages1().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Image image = getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE1)), "Bruno");
        doc.add(image);

        image = getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE2)), "Dog");
        doc.add(image);

        image = getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE3)), "Fox");
        doc.add(image);

        Image srcImage = new Image(ImageDataFactory.create(IMAGE4));
        srcImage.scaleToFit(400, 700);
        image = getWatermarkedImage(pdfDoc, srcImage, "Bruno and Ingeborg");
        doc.add(image);

        doc.close();
    }

    private static Image getWatermarkedImage(PdfDocument pdfDoc, Image img, String watermark) {
        float width = img.getImageScaledWidth();
        float height = img.getImageScaledHeight();
        PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
        new Canvas(template, pdfDoc)
                .add(img)
                .setFontColor(DeviceGray.WHITE)
                .showTextAligned(watermark, width / 2, height / 2, TextAlignment.CENTER, (float) Math.PI / 6)
                .close();
        return new Image(template);
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Images
{
    public class WatermarkedImages1
    {
        public static readonly String DEST = "results/sandbox/images/watermarked_images1.pdf";

        public static readonly String IMAGE1 = "../../../resources/img/bruno.jpg";
        public static readonly String IMAGE2 = "../../../resources/img/dog.bmp";
        public static readonly String IMAGE3 = "../../../resources/img/fox.bmp";
        public static readonly String IMAGE4 = "../../../resources/img/bruno_ingeborg.jpg";

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

            new WatermarkedImages1().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Image image = GetWatermarkedImage(pdfDoc, new Image(ImageDataFactory.Create(IMAGE1)), "Bruno");
            doc.Add(image);

            image = GetWatermarkedImage(pdfDoc, new Image(ImageDataFactory.Create(IMAGE2)), "Dog");
            doc.Add(image);

            image = GetWatermarkedImage(pdfDoc, new Image(ImageDataFactory.Create(IMAGE3)), "Fox");
            doc.Add(image);

            Image srcImage = new Image(ImageDataFactory.Create(IMAGE4));
            srcImage.ScaleToFit(400, 700);
            image = GetWatermarkedImage(pdfDoc, srcImage, "Bruno and Ingeborg");
            doc.Add(image);

            doc.Close();
        }

        private static Image GetWatermarkedImage(PdfDocument pdfDoc, Image img, String watermark)
        {
            float width = img.GetImageScaledWidth();
            float height = img.GetImageScaledHeight();
            PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
            new Canvas(template, pdfDoc)
                .Add(img)
                .SetFontColor(DeviceGray.WHITE)
                .ShowTextAligned(watermark, width / 2, height / 2, TextAlignment.CENTER, (float) Math.PI / 6)
                .Close();
            return new Image(template);
        }
    }
}


watermarkedimages2

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.images;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

public class WatermarkedImages2 {
    public static final String DEST = "./target/sandbox/images/watermarked_images2.pdf";

    public static final String IMAGE1 = "./src/main/resources/img/bruno.jpg";
    public static final String IMAGE2 = "./src/main/resources/img/dog.bmp";
    public static final String IMAGE3 = "./src/main/resources/img/fox.bmp";
    public static final String IMAGE4 = "./src/main/resources/img/bruno_ingeborg.jpg";

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

        new WatermarkedImages2().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Table table = new Table(1).useAllAvailableWidth();

        Image image = new Image(ImageDataFactory.create(IMAGE1)).setAutoScaleWidth(true);
        Cell cell = new Cell().add(image);
        cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Bruno"));
        table.addCell(cell);

        image = new Image(ImageDataFactory.create(IMAGE2)).setAutoScaleWidth(true);
        cell = new Cell().add(image);
        cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Dog"));
        table.addCell(cell);

        image = new Image(ImageDataFactory.create(IMAGE3)).setAutoScaleWidth(true);
        cell = new Cell().add(image);
        cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Fox"));
        table.addCell(cell);

        image = new Image(ImageDataFactory.create(IMAGE4)).setAutoScaleWidth(true);
        cell = new Cell().add(image);
        cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Bruno and Ingeborg"));
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }


    private static class WatermarkedCellRenderer extends CellRenderer {
        private String content;

        public WatermarkedCellRenderer(Cell modelElement, String content) {
            super(modelElement);
            this.content = content;
        }

        // If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
        // If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
        // renderer will be created
        @Override
        public IRenderer getNextRenderer() {
            return new WatermarkedCellRenderer((Cell) modelElement, content);
        }

        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            Paragraph p = new Paragraph(content).setFontColor(ColorConstants.WHITE);
            Rectangle rect = getOccupiedAreaBBox();
            float coordX = (rect.getLeft() + rect.getRight()) / 2;
            float coordY = (rect.getBottom() + rect.getTop()) / 2;
            float angle = (float) Math.PI / 6;
            new Canvas(drawContext.getCanvas(), rect)
                    .showTextAligned(p, coordX, coordY, getOccupiedArea().getPageNumber(),
                            TextAlignment.CENTER, VerticalAlignment.MIDDLE, angle)
                    .close();
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;

namespace iText.Samples.Sandbox.Images
{
    public class WatermarkedImages2
    {
        public static readonly String DEST = "results/sandbox/images/watermarked_images2.pdf";

        public static readonly String IMAGE1 = "../../../resources/img/bruno.jpg";
        public static readonly String IMAGE2 = "../../../resources/img/dog.bmp";
        public static readonly String IMAGE3 = "../../../resources/img/fox.bmp";
        public static readonly String IMAGE4 = "../../../resources/img/bruno_ingeborg.jpg";

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

            new WatermarkedImages2().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Table table = new Table(1).UseAllAvailableWidth();

            Image image = new Image(ImageDataFactory.Create(IMAGE1)).SetAutoScaleWidth(true);
            Cell cell = new Cell().Add(image);
            cell.SetNextRenderer(new WatermarkedCellRenderer(cell, "Bruno"));
            table.AddCell(cell);

            image = new Image(ImageDataFactory.Create(IMAGE2)).SetAutoScaleWidth(true);
            cell = new Cell().Add(image);
            cell.SetNextRenderer(new WatermarkedCellRenderer(cell, "Dog"));
            table.AddCell(cell);

            image = new Image(ImageDataFactory.Create(IMAGE3)).SetAutoScaleWidth(true);
            cell = new Cell().Add(image);
            cell.SetNextRenderer(new WatermarkedCellRenderer(cell, "Fox"));
            table.AddCell(cell);

            image = new Image(ImageDataFactory.Create(IMAGE4)).SetAutoScaleWidth(true);
            cell = new Cell().Add(image);
            cell.SetNextRenderer(new WatermarkedCellRenderer(cell, "Bruno and Ingeborg"));
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }

        private class WatermarkedCellRenderer : CellRenderer
        {
            private String content;

            public WatermarkedCellRenderer(Cell modelElement, String content) : base(modelElement)
            {
                this.content = content;
            }

            // If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
            // If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
            // renderer will be created
            public override IRenderer GetNextRenderer()
            {
                return new WatermarkedCellRenderer((Cell) modelElement, content);
            }

            public override void Draw(DrawContext drawContext)
            {
                base.Draw(drawContext);
                Paragraph p = new Paragraph(content).SetFontColor(ColorConstants.WHITE);
                Rectangle rect = GetOccupiedAreaBBox();
                float coordX = (rect.GetLeft() + rect.GetRight()) / 2;
                float coordY = (rect.GetBottom() + rect.GetTop()) / 2;
                float angle = (float) Math.PI / 6;
                new Canvas(drawContext.GetCanvas(), rect)
                    .ShowTextAligned(p, coordX, coordY, GetOccupiedArea().GetPageNumber(),
                        TextAlignment.CENTER, VerticalAlignment.MIDDLE, angle)
                    .Close();
            }
        }
    }
}

watermarkedimages3

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.images;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class WatermarkedImages3 {
    public static final String DEST = "./target/sandbox/images/watermarked_images3.pdf";

    public static final String IMAGE1 = "./src/main/resources/img/bruno.jpg";

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

        new WatermarkedImages3().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Table table = new Table(1).setWidth(UnitValue.createPercentValue(80));
        for (int i = 0; i < 35; i++) {
            table.addCell(new Cell().add(new Paragraph("rahlrokks doesn't listen to what people tell him")));
        }

        Image img = getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE1)), "Bruno")
                .setAutoScale(true);
        table.addCell(new Cell().add(img));
        doc.add(table);

        doc.showTextAligned("Bruno knows best", 260, 400,
                TextAlignment.CENTER, 45f * (float) Math.PI / 180f);

        doc.close();
    }

    private static Image getWatermarkedImage(PdfDocument pdfDoc, Image img, String watermark) {
        float width = img.getImageScaledWidth();
        float height = img.getImageScaledHeight();
        float coordX = width / 2;
        float coordY = height / 2;
        float angle = (float) Math.PI * 30f / 180f;
        PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
        new Canvas(template, pdfDoc)
                .add(img)
                .setFontColor(DeviceGray.WHITE)
                .showTextAligned(watermark, coordX, coordY, TextAlignment.CENTER, angle)
                .close();
        return new Image(template);
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Images
{
    public class WatermarkedImages3
    {
        public static readonly String DEST = "results/sandbox/images/watermarked_images3.pdf";

        public static readonly String IMAGE1 = "../../../resources/img/bruno.jpg";

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

            new WatermarkedImages3().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Table table = new Table(1).SetWidth(UnitValue.CreatePercentValue(80));
            for (int i = 0; i < 35; i++)
            {
                table.AddCell(new Cell().Add(new Paragraph("rahlrokks doesn't listen to what people tell him")));
            }

            Image img = GetWatermarkedImage(pdfDoc, new Image(ImageDataFactory.Create(IMAGE1)), "Bruno")
                .SetAutoScale(true);
            table.AddCell(new Cell().Add(img));
            doc.Add(table);

            doc.ShowTextAligned("Bruno knows best", 260, 400,
                TextAlignment.CENTER, 45f * (float) Math.PI / 180f);

            doc.Close();
        }

        private static Image GetWatermarkedImage(PdfDocument pdfDoc, Image img, String watermark)
        {
            float width = img.GetImageScaledWidth();
            float height = img.GetImageScaledHeight();
            float coordX = width / 2;
            float coordY = height / 2;
            float angle = (float) Math.PI * 30f / 180f;
            PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
            new Canvas(template, pdfDoc)
                .Add(img)
                .SetFontColor(DeviceGray.WHITE)
                .ShowTextAligned(watermark, coordX, coordY, TextAlignment.CENTER, angle)
                .Close();
            return new Image(template);
        }
    }
}


watermarkedimages4

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.images;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;

import java.io.File;

public class WatermarkedImages4 {
    public static final String DEST = "./target/sandbox/images/watermarked_images4.pdf";

    public static final String IMAGE1 = "./src/main/resources/img/bruno.jpg";
    public static final String IMAGE2 = "./src/main/resources/img/dog.bmp";
    public static final String IMAGE3 = "./src/main/resources/img/fox.bmp";
    public static final String IMAGE4 = "./src/main/resources/img/bruno_ingeborg.jpg";

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

        new WatermarkedImages4().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Image img = getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE1)));
        doc.add(img);

        img = getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE2)));
        doc.add(img);

        img = getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE3)));
        doc.add(img);

        Image srcImage = new Image(ImageDataFactory.create(IMAGE4));
        srcImage.scaleToFit(400, 700);
        img = getWatermarkedImage(pdfDoc, srcImage);
        doc.add(img);

        doc.close();
    }

    private static Image getWatermarkedImage(PdfDocument pdfDocument, Image img) {
        float width = img.getImageScaledWidth();
        float height = img.getImageScaledHeight();
        PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));

        // Use a highlevel Canvas to add the image because the scaling properties were set to the
        // highlevel Image object.
        new Canvas(template, pdfDocument)
                .add(img)
                .close();

        new PdfCanvas(template, pdfDocument)
                .saveState()
                .setStrokeColor(ColorConstants.GREEN)
                .setLineWidth(3)
                .moveTo(width * 0.25f, height * 0.25f)
                .lineTo(width * 0.75f, height * 0.75f)
                .moveTo(width * 0.25f, height * 0.75f)
                .lineTo(width * 0.25f, height * 0.25f)
                .stroke()
                .setStrokeColor(ColorConstants.WHITE)
                .ellipse(0, 0, width, height)
                .stroke()
                .restoreState();

        return new Image(template);
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Images
{
    public class WatermarkedImages4
    {
        public static readonly String DEST = "results/sandbox/images/watermarked_images4.pdf";

        public static readonly String IMAGE1 = "../../../resources/img/bruno.jpg";
        public static readonly String IMAGE2 = "../../../resources/img/dog.bmp";
        public static readonly String IMAGE3 = "../../../resources/img/fox.bmp";
        public static readonly String IMAGE4 = "../../../resources/img/bruno_ingeborg.jpg";

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

            new WatermarkedImages4().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Image img = GetWatermarkedImage(pdfDoc, new Image(ImageDataFactory.Create(IMAGE1)));
            doc.Add(img);

            img = GetWatermarkedImage(pdfDoc, new Image(ImageDataFactory.Create(IMAGE2)));
            doc.Add(img);

            img = GetWatermarkedImage(pdfDoc, new Image(ImageDataFactory.Create(IMAGE3)));
            doc.Add(img);

            Image srcImage = new Image(ImageDataFactory.Create(IMAGE4));
            srcImage.ScaleToFit(400, 700);
            img = GetWatermarkedImage(pdfDoc, srcImage);
            doc.Add(img);

            doc.Close();
        }

        private static Image GetWatermarkedImage(PdfDocument pdfDocument, Image img)
        {
            float width = img.GetImageScaledWidth();
            float height = img.GetImageScaledHeight();
            PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));

            // Use a highlevel Canvas to add the image because the scaling properties were set to the
            // highlevel Image object.
            new Canvas(template, pdfDocument)
                .Add(img)
                .Close();

            new PdfCanvas(template, pdfDocument)
                .SaveState()
                .SetStrokeColor(ColorConstants.GREEN)
                .SetLineWidth(3)
                .MoveTo(width * 0.25f, height * 0.25f)
                .LineTo(width * 0.75f, height * 0.75f)
                .MoveTo(width * 0.25f, height * 0.75f)
                .LineTo(width * 0.25f, height * 0.25f)
                .Stroke()
                .SetStrokeColor(ColorConstants.WHITE)
                .Ellipse(0, 0, width, height)
                .Stroke()
                .RestoreState();

            return new Image(template);
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.