Pattern colors

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

31st May 2016
admin-marketing

Switch code for this example

GradientTopToBottom.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written in answer to:
  10.  * http://stackoverflow.com/questions/34433510
  11.  */
  12. package com.itextpdf.samples.sandbox.graphics;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.geom.PageSize;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.kernel.pdf.colorspace.PdfDeviceCs;
  20. import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
  21. import com.itextpdf.kernel.pdf.colorspace.PdfShading;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import java.io.File;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. @Category(SampleTest.class)
  30. public class GradientTopToBottom extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/graphics/gradient_top_to_bottom.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new GradientTopToBottom().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PageSize pageSize = new PageSize(150, 300);
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         pdfDoc.setDefaultPageSize(pageSize);
  44.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  45.         PdfShading.Axial axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), 0, pageSize.getHeight(), Color.WHITE.getColorValue(),
  46.                 0, 0, Color.GREEN.getColorValue());
  47.         PdfPattern.Shading pattern = new PdfPattern.Shading(axial);
  48.         canvas.setFillColorShading(pattern);
  49.         canvas.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
  50.         canvas.fill();
  51.         pdfDoc.close();
  52.     }
  53. }
ShadedFill.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/26586093/how-to-add-a-shading-pattern-to-a-custom-shape-in-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  18. import com.itextpdf.kernel.pdf.colorspace.PdfDeviceCs;
  19. import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
  20. import com.itextpdf.kernel.pdf.colorspace.PdfShading;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27. import java.io.IOException;
  28.  
  29. @Category(SampleTest.class)
  30. public class ShadedFill extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/objects/shaded_fill.pdf";
  32.  
  33.     public static void main(String[] args) throws IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new ShadedFill().manipulatePdf(DEST);
  37.     }
  38.  
  39.     public void manipulatePdf(String dest) throws IOException {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  41.  
  42.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  43.         float x = 36;
  44.         float y = 740;
  45.         float side = 70;
  46.         PdfShading.Axial axial = new PdfShading.Axial(new PdfDeviceCs.Rgb(), x, y, Color.PINK.getColorValue(),
  47.                 x + side, y, Color.BLUE.getColorValue());
  48.         PdfPattern.Shading shading = new PdfPattern.Shading(axial);
  49.         canvas.setFillColorShading(shading);
  50.         canvas.moveTo(x, y);
  51.         canvas.lineTo(x + side, y);
  52.         canvas.lineTo(x + (side / 2), (float) (y + (side * Math.sin(Math.PI / 3))));
  53.         canvas.closePathFillStroke();
  54.  
  55.         pdfDoc.close();
  56.     }
  57. }
TextPattern.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/31262460/how-to-add-x-offset-to-text-pattern-with-every-x-step-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.objects;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.PatternColor;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  21. import com.itextpdf.kernel.pdf.canvas.PdfPatternCanvas;
  22. import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
  23. import com.itextpdf.layout.Document;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30. import java.io.IOException;
  31.  
  32. @Category(SampleTest.class)
  33. public class TextPattern extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/objects/text_pattern.pdf";
  35.  
  36.     public static void main(String[] args) throws IOException {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new TextPattern().manipulatePdf(DEST);
  40.     }
  41.  
  42.     public void manipulatePdf(String dest) throws IOException {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  44.         Document doc = new Document(pdfDoc);
  45.  
  46.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  47.         PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  48.         String filltext = "this is the fill text! ";
  49.         float filltextWidth = font.getWidth(filltext, 6);
  50.  
  51.         PdfPattern.Tiling tilingPattern = new PdfPattern.Tiling(filltextWidth, 60, filltextWidth, 60);
  52.         PdfPatternCanvas patternCanvas = new PdfPatternCanvas(tilingPattern, pdfDoc);
  53.         patternCanvas.beginText().setFontAndSize(font, 6.f);
  54.         float x = 0;
  55.         for (float y = 0; y < 60; y += 10) {
  56.             patternCanvas.setTextMatrix(x - filltextWidth, y);
  57.             patternCanvas.showText(filltext);
  58.             patternCanvas.setTextMatrix(x, y);
  59.             patternCanvas.showText(filltext);
  60.             x += (filltextWidth / 6);
  61.         }
  62.         patternCanvas.endText();
  63.  
  64.         canvas.rectangle(0, 0, 595, 842);
  65.         canvas.setFillColor(new PatternColor(tilingPattern));
  66.         canvas.fill();
  67.  
  68.         doc.close();
  69.     }
  70. }
TiledBackgroundColor.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/34177025
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.color.Color;
  17. import com.itextpdf.kernel.color.PatternColor;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.PdfPatternCanvas;
  21. import com.itextpdf.kernel.pdf.colorspace.PdfPattern;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Cell;
  24. import com.itextpdf.layout.element.Image;
  25. import com.itextpdf.layout.element.Table;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32.  
  33. @Category(SampleTest.class)
  34. public class TiledBackgroundColor extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/tables/tiled_background_color.pdf";
  36.     public static final String IMG = "./src/test/resources/img/bulb.gif";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new TiledBackgroundColor().manipulatePdf(DEST);
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  47.         Document doc = new Document(pdfDoc);
  48.         ImageData img = ImageDataFactory.create(IMG);
  49.         Image image = new Image(img);
  50.         PdfPattern.Tiling img_pattern = new PdfPattern.Tiling(image.getImageScaledWidth(), image.getImageScaledHeight());
  51.         new PdfPatternCanvas(img_pattern, pdfDoc).addImage(img, 0, 0, false);
  52.         Color color = new PatternColor(img_pattern);
  53.         Table table = new Table(2);
  54.         table.addCell(new Cell().add("Behold a cell with an image pattern:"));
  55.         Cell cell = new Cell();
  56.         cell.setHeight(60);
  57.         cell.setBackgroundColor(color);
  58.         table.addCell(cell);
  59.         doc.add(table);
  60.         doc.close();
  61.     }
  62. }
TiledBackgroundColor2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/34177025
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.layout.renderer.CellRenderer;
  24. import com.itextpdf.layout.renderer.DrawContext;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import java.io.File;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. @Category(SampleTest.class)
  33. public class TiledBackgroundColor2 extends GenericTest {
  34.     public static final String DEST
  35.             = "./target/test/resources/sandbox/tables/tiled_background_color2.pdf";
  36.     public static final String IMG
  37.             = "./src/test/resources/img/bulb.gif";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new TiledBackgroundColor2().manipulatePdf(DEST);
  43.     }
  44.  
  45.     @Override
  46.     protected void manipulatePdf(String dest) throws Exception {
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  48.         Document doc = new Document(pdfDoc);
  49.         Table table = new Table(2);
  50.         table.addCell("Behold a cell with an image pattern:");
  51.         Cell cell = new Cell();
  52.         ImageData img = ImageDataFactory.create(IMG);
  53.         cell.setNextRenderer(new TiledImageBackgroundRenderer(cell, img));
  54.         cell.setHeight(60);
  55.         table.addCell(cell);
  56.         doc.add(table);
  57.         doc.close();
  58.     }
  59.  
  60.  
  61.     class TiledImageBackgroundRenderer extends CellRenderer {
  62.         protected ImageData img;
  63.  
  64.         public TiledImageBackgroundRenderer(Cell modelElement, ImageData img) {
  65.             super(modelElement);
  66.             this.img = img;
  67.         }
  68.  
  69.         @Override
  70.         public void draw(DrawContext drawContext) {
  71.             PdfCanvas canvas = drawContext.getCanvas();
  72.             Rectangle position = getOccupiedAreaBBox();
  73.             com.itextpdf.layout.element.Image image = new com.itextpdf.layout.element.Image(img);
  74.             image.scaleToFit(10000000, position.getHeight());
  75.             float x = position.getLeft();
  76.             float y = position.getBottom();
  77.             while (x + image.getImageScaledWidth() < position.getRight()) {
  78.                 image.setFixedPosition(x, y);
  79.                 canvas.addImage(img, x, y, image.getImageScaledWidth(), false);
  80.                 x += image.getImageScaledWidth();
  81.             }
  82.         }
  83.     }
  84. }
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