Skip to main content
Skip table of contents

Pattern colors

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

shadedfill

JAVA
JAVA
/*
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/26586093/how-to-add-a-shading-pattern-to-a-custom-shape-in-itext
 */
package sandbox.objects;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfShading;
import com.itextpdf.text.pdf.PdfShadingPattern;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class ShadedFill {
    
    public static final String DEST = "results/objects/shaded_fill.pdf";
    
        
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ShadedFill().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        // step 3
        document.open();
        // step 4
        PdfContentByte canvas = writer.getDirectContent();
        float x = 36;
        float y = 740;
        float side = 70;
        PdfShading axial = PdfShading.simpleAxial(writer, x, y,
                x + side, y, BaseColor.PINK, BaseColor.BLUE);
        PdfShadingPattern shading = new PdfShadingPattern(axial);
        canvas.setShadingFill(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();
        // step 5
        document.close();
    }
}

gradienttoptobottom

JAVA
JAVA
/*
 * This example was written in answer to:
 * http://stackoverflow.com/questions/34433510
 */
package sandbox.graphics;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfShading;
import com.itextpdf.text.pdf.PdfShadingPattern;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author iText
 */
public class GradientTopToBottom {

    public static final String DEST = "results/graphics/gradient_top_to_bottom.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new GradientTopToBottom().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Rectangle pageSize = new Rectangle(150, 300);
        Document document = new Document(pageSize);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfShading shading = PdfShading.simpleAxial(writer,
                0, pageSize.getHeight(),
                0, 0,
                BaseColor.WHITE, BaseColor.GREEN);
        PdfShadingPattern pattern = new PdfShadingPattern(shading);
        PdfContentByte canvas = writer.getDirectContent();
        canvas.setShadingFill(pattern);
        canvas.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
        canvas.fill();
        document.close();
    }
    
}

textpattern

JAVA
JAVA
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/31262460/how-to-add-x-offset-to-text-pattern-with-every-x-step-itext
 */
package sandbox.objects;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPatternPainter;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 * @author iText
 */
@WrapToTest
public class TextPattern {
    
    public static final String DEST = "results/objects/text_pattern.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TextPattern().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont();
        String filltext = "this is the fill text! ";
        float filltextWidth = bf.getWidthPoint(filltext, 6);
        PdfPatternPainter pattern = canvas.createPattern(filltextWidth, 60, filltextWidth, 60);
        pattern.beginText();
        pattern.setFontAndSize(bf, 6.0f);
        float x = 0;
        for (float y = 0; y < 60; y += 10) {
            pattern.setTextMatrix(x - filltextWidth, y);
            pattern.showText(filltext);
            pattern.setTextMatrix(x, y);
            pattern.showText(filltext);
            x += (filltextWidth / 6);
        }
        pattern.endText();

        canvas.rectangle(0, 0, 595, 842);
        canvas.setPatternFill(pattern);
        canvas.fill();
        document.close();
    }
    
}

tiledbackgroundcolor

JAVA
JAVA
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34177025
 */
package sandbox.tables;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PatternColor;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPatternPainter;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author Bruno Lowagie (iText Software)
 */
public class TiledBackgroundColor {
    
    public static final String DEST = "results/tables/tiled_patterncolor.pdf";
    public static final String IMG = "resources/images/bulb.gif";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TiledBackgroundColor().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        Image image = Image.getInstance(IMG);
        PdfPatternPainter img_pattern = canvas.createPattern(
                image.getScaledWidth(), image.getScaledHeight());
        image.setAbsolutePosition(0, 0);
        img_pattern.addImage(image);
        BaseColor color = new PatternColor(img_pattern);
        PdfPTable table = new PdfPTable(2);
        table.addCell("Behold a cell with an image pattern:");
        PdfPCell cell = new PdfPCell();
        cell.setFixedHeight(60);
        cell.setBackgroundColor(color);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}

tiledbackgroundcolor2

JAVA
JAVA
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34177025
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author Bruno Lowagie (iText Software)
 */
public class TiledBackgroundColor2 {
    
    class TiledImageBackground implements PdfPCellEvent {

        protected Image image;
        
        public TiledImageBackground(Image image) {
            this.image = image;
        }
        
        public void cellLayout(PdfPCell cell, Rectangle position,
                PdfContentByte[] canvases) {
            try {
                PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
                image.scaleToFit(10000000, position.getHeight());
                float x = position.getLeft();
                float y = position.getBottom();
                while (x + image.getScaledWidth() < position.getRight()) {
                    image.setAbsolutePosition(x, y);
                    cb.addImage(image);
                    x += image.getScaledWidth();
                }
            } catch (DocumentException e) {
                throw new ExceptionConverter(e);
            }
        }
        
    }
    
    public static final String DEST = "results/tables/tiled_patterncolor2.pdf";
    public static final String IMG = "resources/images/bulb.gif";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TiledBackgroundColor2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.addCell("Behold a cell with an image pattern:");
        PdfPCell cell = new PdfPCell();
        Image image = Image.getInstance(IMG);
        cell.setCellEvent(new TiledImageBackground(image));
        cell.setFixedHeight(60);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}

Resources

bulb.gif

Results

cmp_shaded_fill.pdf

cmp_gradient_top_to_bottom.pdf

cmp_text_pattern.pdf

cmp_tiled_patterncolor.pdf

cmp_tiled_patterncolor2.pdf

JavaScript errors detected

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

If this problem persists, please contact our support.