Skip to main content
Skip table of contents

Pattern colors

These examples were written in the context of questions such as:

gradienttoptobottom

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.graphics;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.colorspace.PdfDeviceCs;
import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
import com.itextpdf.kernel.pdf.colorspace.PdfShading;

import java.io.File;

public class GradientTopToBottom {
    public static final String DEST = "./target/sandbox/graphics/gradient_top_to_bottom.pdf";

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

        new GradientTopToBottom().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PageSize pageSize = new PageSize(150, 300);
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        pdfDoc.setDefaultPageSize(pageSize);

        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        PdfShading.Axial axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 0, pageSize.getHeight(),
                ColorConstants.WHITE.getColorValue(), 0, 0, ColorConstants.GREEN.getColorValue());
        PdfPattern.Shading pattern = new PdfPattern.Shading(axial);
        canvas.setFillColorShading(pattern);
        canvas.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
        canvas.fill();

        pdfDoc.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Colorspace;

namespace iText.Samples.Sandbox.Graphics
{
    public class GradientTopToBottom
    {
        public static readonly string DEST = "results/sandbox/graphics/gradient_top_to_bottom.pdf";

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

            new GradientTopToBottom().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PageSize pageSize = new PageSize(150, 300);
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            pdfDoc.SetDefaultPageSize(pageSize);

            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            PdfShading.Axial axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 0, pageSize.GetHeight(),
                ColorConstants.WHITE.GetColorValue(), 0, 0, ColorConstants.GREEN.GetColorValue());
            PdfPattern.Shading pattern = new PdfPattern.Shading(axial);
            canvas.SetFillColorShading(pattern);
            canvas.Rectangle(0, 0, pageSize.GetWidth(), pageSize.GetHeight());
            canvas.Fill();

            pdfDoc.Close();
        }
    }
}

shadedfill

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.objects;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.colorspace.PdfDeviceCs;
import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
import com.itextpdf.kernel.pdf.colorspace.PdfShading;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ShadedFill {
    public static final String DEST = "./target/sandbox/objects/shaded_fill.pdf";

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

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

        float x = 36;
        float y = 740;

        // Side of an equilateral triangle
        float side = 70;

        PdfShading.Axial axialShading = new PdfShading.Axial(new PdfDeviceCs.Rgb(), x, y, ColorConstants.PINK.getColorValue(),
                x + side, y, ColorConstants.BLUE.getColorValue());
        PdfPattern.Shading shading = new PdfPattern.Shading(axialShading);

        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        canvas.setFillColorShading(shading);
        canvas.moveTo(x, y);
        canvas.lineTo(x + side, y);
        canvas.lineTo(x + (side / 2), (float) (y + (side * Math.sin(Math.PI / 3))));
        canvas.closePathFillStroke();

        pdfDoc.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Colorspace;

namespace iText.Samples.Sandbox.Objects
{
    public class ShadedFill
    {
        public static readonly string DEST = "results/sandbox/objects/shaded_fill.pdf";

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

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            float x = 36;
            float y = 740;
            
            // Side of an equilateral triangle
            float side = 70;

            PdfShading.Axial axialShading = new PdfShading.Axial(new PdfDeviceCs.Rgb(), x, y,
                ColorConstants.PINK.GetColorValue(),
                x + side, y, ColorConstants.BLUE.GetColorValue());
            PdfPattern.Shading shading = new PdfPattern.Shading(axialShading);

            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            canvas.SetFillColorShading(shading);
            canvas.MoveTo(x, y);
            canvas.LineTo(x + side, y);
            canvas.LineTo(x + (side / 2), (float) (y + (side * Math.Sin(Math.PI / 3))));
            canvas.ClosePathFillStroke();

            pdfDoc.Close();
        }
    }
}

textpattern

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.objects;

import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.PatternColor;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.PdfPatternCanvas;
import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
import com.itextpdf.layout.Document;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TextPattern {
    public static final String DEST = "./target/sandbox/objects/text_pattern.pdf";

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

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

        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
        String fillText = "this is the fill text! ";
        float fillTextWidth = font.getWidth(fillText, 6);

        PdfPattern.Tiling tilingPattern = new PdfPattern.Tiling(fillTextWidth, 60, fillTextWidth, 60);
        PdfPatternCanvas patternCanvas = new PdfPatternCanvas(tilingPattern, pdfDoc);
        patternCanvas.beginText().setFontAndSize(font, 6.f);
        float x = 0;
        for (float y = 0; y < 60; y += 10) {
            patternCanvas.setTextMatrix(x - fillTextWidth, y);
            patternCanvas.showText(fillText);
            patternCanvas.setTextMatrix(x, y);
            patternCanvas.showText(fillText);
            x += (fillTextWidth / 6);
        }
        patternCanvas.endText();

        canvas.rectangle(10, 10, 575, 822);
        canvas.setFillColor(new PatternColor(tilingPattern));
        canvas.fill();

        doc.close();
    }
}
C#
C#
using System.IO;
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.Colorspace;
using iText.Layout;

namespace iText.Samples.Sandbox.Objects
{
    public class TextPattern
    {
        public static readonly string DEST = "results/sandbox/objects/text_pattern.pdf";

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

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

            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            string fillText = "this is the fill text! ";
            float fillTextWidth = font.GetWidth(fillText, 6);

            PdfPattern.Tiling tilingPattern = new PdfPattern.Tiling(fillTextWidth, 60, fillTextWidth, 60);
            PdfPatternCanvas patternCanvas = new PdfPatternCanvas(tilingPattern, pdfDoc);
            patternCanvas.BeginText().SetFontAndSize(font, 6f);
            float x = 0;
            for (float y = 0; y < 60; y += 10)
            {
                patternCanvas.SetTextMatrix(x - fillTextWidth, y);
                patternCanvas.ShowText(fillText);
                patternCanvas.SetTextMatrix(x, y);
                patternCanvas.ShowText(fillText);
                x += (fillTextWidth / 6);
            }

            patternCanvas.EndText();

            canvas.Rectangle(10, 10, 575, 822);
            canvas.SetFillColor(new PatternColor(tilingPattern));
            canvas.Fill();

            doc.Close();
        }
    }
}

tiledbackgroundcolor

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.tables;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.Color;
import com.itextpdf.kernel.colors.PatternColor;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfPatternCanvas;
import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
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.UnitValue;

import java.io.File;

public class TiledBackgroundColor {
    public static final String DEST = "./target/sandbox/tables/tiled_background_color.pdf";

    public static final String IMG = "./src/main/resources/img/bulb.gif";

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

        new TiledBackgroundColor().manipulatePdf(DEST);
    }

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

        ImageData img = ImageDataFactory.create(IMG);
        Image image = new Image(img);

        PdfPattern.Tiling imgPattern = new PdfPattern.Tiling(image.getImageScaledWidth(),
                image.getImageScaledHeight());

        PdfPatternCanvas canvas = new PdfPatternCanvas(imgPattern, pdfDoc);
        canvas.addImageAt(img, 0, 0, false);

        Color color = new PatternColor(imgPattern);

        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        table.addCell(new Cell().add(new Paragraph("Behold a cell with an image pattern:")));

        Cell cell = new Cell();
        cell.setHeight(60);
        cell.setBackgroundColor(color);
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Colorspace;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Tables
{
    public class TiledBackgroundColor
    {
        public static readonly string DEST = "results/sandbox/tables/tiled_background_color.pdf";

        public static readonly String IMG = "../../../resources/img/bulb.gif";

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

            new TiledBackgroundColor().ManipulatePdf(DEST);
        }

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

            ImageData img = ImageDataFactory.Create(IMG);
            Image image = new Image(img);

            PdfPattern.Tiling imgPattern = new PdfPattern.Tiling(image.GetImageScaledWidth(),
                image.GetImageScaledHeight());

            PdfPatternCanvas canvas = new PdfPatternCanvas(imgPattern, pdfDoc);
            canvas.AddImageAt(img, 0, 0, false);
            
            Color color = new PatternColor(imgPattern);

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            table.AddCell(new Cell().Add(new Paragraph("Behold a cell with an image pattern:")));

            Cell cell = new Cell();
            cell.SetHeight(60);
            cell.SetBackgroundColor(color);
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
    }
}

tiledbackgroundcolor2

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.tables;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
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.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

public class TiledBackgroundColor2 {
    public static final String DEST
            = "./target/sandbox/tables/tiled_background_color2.pdf";

    public static final String IMG
            = "./src/main/resources/img/bulb.gif";

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

        new TiledBackgroundColor2().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(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        table.addCell("Behold a cell with an image pattern:");

        Cell cell = new Cell();
        ImageData img = ImageDataFactory.create(IMG);
        cell.setNextRenderer(new TiledImageBackgroundRenderer(cell, img));
        cell.setHeight(60);
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }


    private static class TiledImageBackgroundRenderer extends CellRenderer {
        protected ImageData img;

        public TiledImageBackgroundRenderer(Cell modelElement, ImageData img) {
            super(modelElement);
            this.img = img;
        }

        // 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 TiledImageBackgroundRenderer((Cell) modelElement, img);
        }

        @Override
        public void draw(DrawContext drawContext) {
            PdfCanvas canvas = drawContext.getCanvas();
            Rectangle position = getOccupiedAreaBBox();

            Image image = new Image(img);
            image.scaleToFit(10000000, position.getHeight());

            float x = position.getLeft();
            float y = position.getBottom();

            while (x + image.getImageScaledWidth() < position.getRight()) {
                image.setFixedPosition(x, y);
                canvas.addImageFittedIntoRectangle(img, new Rectangle(x, y, image.getImageScaledWidth(), image.getImageScaledHeight()), false);
                x += image.getImageScaledWidth();
            }
        }
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;

namespace iText.Samples.Sandbox.Tables
{
    public class TiledBackgroundColor2
    {
        public static readonly string DEST = "results/sandbox/tables/tiled_background_color2.pdf";

        public static readonly String IMG = "../../../resources/img/bulb.gif";

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

            new TiledBackgroundColor2().ManipulatePdf(DEST);
        }

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

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            table.AddCell("Behold a cell with an image pattern:");

            Cell cell = new Cell();
            ImageData img = ImageDataFactory.Create(IMG);
            cell.SetNextRenderer(new TiledImageBackgroundRenderer(cell, img));
            cell.SetHeight(60);
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }

        private class TiledImageBackgroundRenderer : CellRenderer
        {
            private ImageData img;

            public TiledImageBackgroundRenderer(Cell modelElement, ImageData img)
                : base(modelElement)
            {
                this.img = img;
            }            
            
            // 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 TiledImageBackgroundRenderer((Cell) modelElement, img);
            }

            public override void Draw(DrawContext drawContext)
            {
                PdfCanvas canvas = drawContext.GetCanvas();
                Rectangle position = GetOccupiedAreaBBox();

                Image image = new Image(img);
                image.ScaleToFit(10000000, position.GetHeight());

                float x = position.GetLeft();
                float y = position.GetBottom();

                while (x + image.GetImageScaledWidth() < position.GetRight())
                {
                    image.SetFixedPosition(x, y);
                    canvas.AddImageFittedIntoRectangle(img, new Rectangle(x, y, image.GetImageScaledWidth(), image.GetImageScaledHeight()), false);
                    x += image.GetImageScaledWidth();
                }
            }
        }
    }
}

Resources

bulb.gif

Results

cmp_gradient_top_to_bottom.pdf

cmp_tiled_background_color.pdf

cmp_tiled_background_color2.pdf

cmp_shaded_fill.pdf

cmp_text_pattern.pdf

JavaScript errors detected

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

If this problem persists, please contact our support.