Pattern colors

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing
GradientTopToBottom.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * This example was written in answer to:
 * http://stackoverflow.com/questions/34433510
 */
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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class GradientTopToBottom extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }
 
    @Override
    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();
    }
}
ShadedFill.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * 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 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class ShadedFill extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }
 
    public void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
 
        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        float x = 36;
        float y = 740;
        float side = 70;
        PdfShading.Axial axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), x, y, ColorConstants.PINK.getColorValue(),
                x + side, y, ColorConstants.BLUE.getColorValue());
        PdfPattern.Shading shading = new PdfPattern.Shading(axial);
        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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * 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 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class TextPattern extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }
 
    public 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(0, 0, 595, 842);
        canvas.setFillColor(new PatternColor(tilingPattern));
        canvas.fill();
 
        doc.close();
    }
}
TiledBackgroundColor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34177025
 */
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.property.UnitValue;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class TiledBackgroundColor extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/tiled_background_color.pdf";
    public static final String IMG = "./src/test/resources/img/bulb.gif";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TiledBackgroundColor().manipulatePdf(DEST);
    }
 
    @Override
    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 img_pattern = new PdfPattern.Tiling(image.getImageScaledWidth(), image.getImageScaledHeight());
        new PdfPatternCanvas(img_pattern, pdfDoc).addImage(img, 0, 0, false);
        Color color = new PatternColor(img_pattern);
        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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34177025
 */
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.Table;
import com.itextpdf.layout.property.UnitValue;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class TiledBackgroundColor2 extends GenericTest {
    public static final String DEST
            = "./target/test/resources/sandbox/tables/tiled_background_color2.pdf";
    public static final String IMG
            = "./src/test/resources/img/bulb.gif";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TiledBackgroundColor2().manipulatePdf(DEST);
    }
 
    @Override
    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();
    }
 
 
    class TiledImageBackgroundRenderer extends CellRenderer {
        protected ImageData img;
 
        public TiledImageBackgroundRenderer(Cell modelElement, ImageData img) {
            super(modelElement);
            this.img = img;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            PdfCanvas canvas = drawContext.getCanvas();
            Rectangle position = getOccupiedAreaBBox();
            com.itextpdf.layout.element.Image image = new com.itextpdf.layout.element.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.addImage(img, x, y, image.getImageScaledWidth(), false);
                x += image.getImageScaledWidth();
            }
        }
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now