Pattern colors

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

23rd October 2015
admin-marketing

Switch code for this example

ShadedFill.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/26586093/how-to-add-a-shading-pattern-to-a-custom-shape-in-itext
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfShading;
  12. import com.itextpdf.text.pdf.PdfShadingPattern;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ShadedFill {
  22.    
  23.     public static final String DEST = "results/objects/shaded_fill.pdf";
  24.    
  25.        
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ShadedFill().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         // step 1
  34.         Document document = new Document();
  35.         // step 2
  36.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         // step 3
  38.         document.open();
  39.         // step 4
  40.         PdfContentByte canvas = writer.getDirectContent();
  41.         float x = 36;
  42.         float y = 740;
  43.         float side = 70;
  44.         PdfShading axial = PdfShading.simpleAxial(writer, x, y,
  45.                 x + side, y, BaseColor.PINK, BaseColor.BLUE);
  46.         PdfShadingPattern shading = new PdfShadingPattern(axial);
  47.         canvas.setShadingFill(shading);
  48.         canvas.moveTo(x,y);        
  49.         canvas.lineTo(x + side, y);
  50.         canvas.lineTo(x + (side / 2), (float)(y + (side * Math.sin(Math.PI / 3))));
  51.         canvas.closePathFillStroke();
  52.         // step 5
  53.         document.close();
  54.     }
  55. }
GradientTopToBottom.java
  1. /*
  2.  * This example was written in answer to:
  3.  * http://stackoverflow.com/questions/34433510
  4.  */
  5. package sandbox.graphics;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfShading;
  13. import com.itextpdf.text.pdf.PdfShadingPattern;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. /**
  20.  * @author iText
  21.  */
  22. public class GradientTopToBottom {
  23.  
  24.     public static final String DEST = "results/graphics/gradient_top_to_bottom.pdf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new GradientTopToBottom().createPdf(DEST);
  30.     }
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Rectangle pageSize = new Rectangle(150, 300);
  33.         Document document = new Document(pageSize);
  34.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         PdfShading shading = PdfShading.simpleAxial(writer,
  37.                 0, pageSize.getHeight(),
  38.                 0, 0,
  39.                 BaseColor.WHITE, BaseColor.GREEN);
  40.         PdfShadingPattern pattern = new PdfShadingPattern(shading);
  41.         PdfContentByte canvas = writer.getDirectContent();
  42.         canvas.setShadingFill(pattern);
  43.         canvas.rectangle(0, 0, pageSize.getWidth(), pageSize.getHeight());
  44.         canvas.fill();
  45.         document.close();
  46.     }
  47.    
  48. }
TextPattern.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31262460/how-to-add-x-offset-to-text-pattern-with-every-x-step-itext
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.BaseFont;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfPatternPainter;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  * @author iText
  21.  */
  22. @WrapToTest
  23. public class TextPattern {
  24.    
  25.     public static final String DEST = "results/objects/text_pattern.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new TextPattern().createPdf(DEST);
  31.     }
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         PdfContentByte canvas = writer.getDirectContent();
  37.         BaseFont bf = BaseFont.createFont();
  38.         String filltext = "this is the fill text! ";
  39.         float filltextWidth = bf.getWidthPoint(filltext, 6);
  40.         PdfPatternPainter pattern = canvas.createPattern(filltextWidth, 60, filltextWidth, 60);
  41.         pattern.beginText();
  42.         pattern.setFontAndSize(bf, 6.0f);
  43.         float x = 0;
  44.         for (float y = 0; y < 60; y += 10) {
  45.             pattern.setTextMatrix(x - filltextWidth, y);
  46.             pattern.showText(filltext);
  47.             pattern.setTextMatrix(x, y);
  48.             pattern.showText(filltext);
  49.             x += (filltextWidth / 6);
  50.         }
  51.         pattern.endText();
  52.  
  53.         canvas.rectangle(0, 0, 595, 842);
  54.         canvas.setPatternFill(pattern);
  55.         canvas.fill();
  56.         document.close();
  57.     }
  58.    
  59. }
TiledBackgroundColor.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/34177025
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.pdf.PatternColor;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfPatternPainter;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20.  
  21. /**
  22.  *
  23.  * @author Bruno Lowagie (iText Software)
  24.  */
  25. public class TiledBackgroundColor {
  26.    
  27.     public static final String DEST = "results/tables/tiled_patterncolor.pdf";
  28.     public static final String IMG = "resources/images/bulb.gif";
  29.    
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new TiledBackgroundColor().createPdf(DEST);
  34.     }
  35.    
  36.     public void createPdf(String dest) throws IOException, DocumentException {
  37.         Document document = new Document();
  38.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  39.         document.open();
  40.         PdfContentByte canvas = writer.getDirectContent();
  41.         Image image = Image.getInstance(IMG);
  42.         PdfPatternPainter img_pattern = canvas.createPattern(
  43.                 image.getScaledWidth(), image.getScaledHeight());
  44.         image.setAbsolutePosition(0, 0);
  45.         img_pattern.addImage(image);
  46.         BaseColor color = new PatternColor(img_pattern);
  47.         PdfPTable table = new PdfPTable(2);
  48.         table.addCell("Behold a cell with an image pattern:");
  49.         PdfPCell cell = new PdfPCell();
  50.         cell.setFixedHeight(60);
  51.         cell.setBackgroundColor(color);
  52.         table.addCell(cell);
  53.         document.add(table);
  54.         document.close();
  55.     }
  56. }
TiledBackgroundColor2.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/34177025
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.ExceptionConverter;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPCellEvent;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20.  
  21. /**
  22.  *
  23.  * @author Bruno Lowagie (iText Software)
  24.  */
  25. public class TiledBackgroundColor2 {
  26.    
  27.     class TiledImageBackground implements PdfPCellEvent {
  28.  
  29.         protected Image image;
  30.        
  31.         public TiledImageBackground(Image image) {
  32.             this.image = image;
  33.         }
  34.        
  35.         public void cellLayout(PdfPCell cell, Rectangle position,
  36.                 PdfContentByte[] canvases) {
  37.             try {
  38.                 PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
  39.                 image.scaleToFit(10000000, position.getHeight());
  40.                 float x = position.getLeft();
  41.                 float y = position.getBottom();
  42.                 while (x + image.getScaledWidth() < position.getRight()) {
  43.                     image.setAbsolutePosition(x, y);
  44.                     cb.addImage(image);
  45.                     x += image.getScaledWidth();
  46.                 }
  47.             } catch (DocumentException e) {
  48.                 throw new ExceptionConverter(e);
  49.             }
  50.         }
  51.        
  52.     }
  53.    
  54.     public static final String DEST = "results/tables/tiled_patterncolor2.pdf";
  55.     public static final String IMG = "resources/images/bulb.gif";
  56.    
  57.     public static void main(String[] args) throws IOException, DocumentException {
  58.         File file = new File(DEST);
  59.         file.getParentFile().mkdirs();
  60.         new TiledBackgroundColor2().createPdf(DEST);
  61.     }
  62.    
  63.     public void createPdf(String dest) throws IOException, DocumentException {
  64.         Document document = new Document();
  65.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  66.         document.open();
  67.         PdfPTable table = new PdfPTable(2);
  68.         table.addCell("Behold a cell with an image pattern:");
  69.         PdfPCell cell = new PdfPCell();
  70.         Image image = Image.getInstance(IMG);
  71.         cell.setCellEvent(new TiledImageBackground(image));
  72.         cell.setFixedHeight(60);
  73.         table.addCell(cell);
  74.         document.add(table);
  75.         document.close();
  76.     }
  77. }
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