Adding a background to a table

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

31st May 2016
admin-marketing

Switch code for this example

ColoredBackground.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 and Nishanthi Grashia in answer to the following question:
  10.  * http://stackoverflow.com/questions/27871574/appears-space-between-cells-without-border-itextpdf
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.Color;
  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.layout.Document;
  21. import com.itextpdf.layout.border.Border;
  22. import com.itextpdf.layout.element.Cell;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.element.Table;
  25. import com.itextpdf.layout.property.TextAlignment;
  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 ColoredBackground extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/tables/colored_background.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new ColoredBackground().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  46.         Document doc = new Document(pdfDoc);
  47.  
  48.         Table table;
  49.         Cell cell;
  50.         PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  51.         table = new Table(16);
  52.         for (int aw = 0; aw < 16; aw++) {
  53.             cell = new Cell().add(new Paragraph("hi").setFont(font).setFontColor(Color.WHITE));
  54.             cell.setBackgroundColor(Color.BLUE);
  55.             cell.setBorder(Border.NO_BORDER);
  56.             cell.setTextAlignment(TextAlignment.CENTER);
  57.             table.addCell(cell);
  58.         }
  59.         doc.add(table);
  60.  
  61.         doc.close();
  62.     }
  63. }
ImageBackground.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/24162974/how-to-set-background-image-in-pdfpcell-in-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.color.DeviceGray;
  17. import com.itextpdf.kernel.font.PdfFont;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Cell;
  23. import com.itextpdf.layout.element.Image;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.layout.element.Table;
  26. import com.itextpdf.layout.renderer.CellRenderer;
  27. import com.itextpdf.layout.renderer.DrawContext;
  28. import com.itextpdf.samples.GenericTest;
  29. import com.itextpdf.test.annotations.type.SampleTest;
  30.  
  31. import org.junit.experimental.categories.Category;
  32.  
  33. import java.io.File;
  34.  
  35. @Category(SampleTest.class)
  36. public class ImageBackground extends GenericTest {
  37.     public static final String DEST = "./target/test/resources/sandbox/tables/image_background.pdf";
  38.     public static final String IMG = "./src/test/resources/img/bruno.jpg";
  39.  
  40.     public static void main(String[] args) throws Exception {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new ImageBackground().manipulatePdf(DEST);
  44.     }
  45.  
  46.     @Override
  47.     protected void manipulatePdf(String dest) throws Exception {
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  49.         Document doc = new Document(pdfDoc);
  50.  
  51.         Table table = new Table(1);
  52.         table.setWidth(400);
  53.         Cell cell = new Cell();
  54.         PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  55.         Paragraph p = new Paragraph("A cell with an image as background color.")
  56.                 .setFont(font)
  57.                 .setFontColor(DeviceGray.WHITE);
  58.         cell.add(p);
  59.         Image img = new Image(ImageDataFactory.create(IMG));
  60.         cell.setNextRenderer(new ImageBackgroundCellRenderer(cell, img));
  61.         cell.setHeight(600 * img.getImageHeight() / img.getImageWidth());
  62.         table.addCell(cell);
  63.         doc.add(table);
  64.  
  65.         doc.close();
  66.     }
  67.  
  68.  
  69.     private class ImageBackgroundCellRenderer extends CellRenderer {
  70.         protected Image img;
  71.  
  72.         public ImageBackgroundCellRenderer(Cell modelElement, Image img) {
  73.             super(modelElement);
  74.             this.img = img;
  75.         }
  76.  
  77.         @Override
  78.         public void draw(DrawContext drawContext) {
  79.             img.scaleToFit(getOccupiedAreaBBox().getWidth(), getOccupiedAreaBBox().getHeight());
  80.             drawContext.getCanvas().addXObject(img.getXObject(), getOccupiedAreaBBox());
  81.             super.draw(drawContext);
  82.         }
  83.     }
  84. }
SimpleTable10.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/30267169/cannot-display-background-color-when-using-rowspan-with-itext-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  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.layout.Document;
  18. import com.itextpdf.layout.element.Cell;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class SimpleTable10 extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table10.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new SimpleTable10().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         Document doc = new Document(pdfDoc);
  41.  
  42.         Table table = new Table(5);
  43.         Cell sn = new Cell(2, 1).add("S/N");
  44.         sn.setBackgroundColor(Color.YELLOW);
  45.         table.addCell(sn);
  46.         Cell name = new Cell(1, 3).add("Name");
  47.         name.setBackgroundColor(Color.CYAN);
  48.         table.addCell(name);
  49.         Cell age = new Cell(2, 1).add("Age");
  50.         age.setBackgroundColor(Color.GRAY);
  51.         table.addCell(age);
  52.         Cell surname = new Cell().add("SURNAME");
  53.         surname.setBackgroundColor(Color.BLUE);
  54.         table.addCell(surname);
  55.         Cell firstname = new Cell().add("FIRST NAME");
  56.         firstname.setBackgroundColor(Color.RED);
  57.         table.addCell(firstname);
  58.         Cell middlename = new Cell().add("MIDDLE NAME");
  59.         middlename.setBackgroundColor(Color.GREEN);
  60.         table.addCell(middlename);
  61.         Cell f1 = new Cell().add("1");
  62.         f1.setBackgroundColor(Color.PINK);
  63.         table.addCell(f1);
  64.         Cell f2 = new Cell().add("James");
  65.         f2.setBackgroundColor(Color.MAGENTA);
  66.         table.addCell(f2);
  67.         Cell f3 = new Cell().add("Fish");
  68.         f3.setBackgroundColor(Color.ORANGE);
  69.         table.addCell(f3);
  70.         Cell f4 = new Cell().add("Stone");
  71.         f4.setBackgroundColor(Color.DARK_GRAY);
  72.         table.addCell(f4);
  73.         Cell f5 = new Cell().add("17");
  74.         f5.setBackgroundColor(Color.LIGHT_GRAY);
  75.         table.addCell(f5);
  76.         doc.add(table);
  77.  
  78.         doc.close();
  79.     }
  80. }
TiledBackground.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/23374557/itextsharp-why-cell-background-image-is-rotated-90-degress-clockwise
  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.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.layout.element.Cell;
  25. import com.itextpdf.layout.element.Table;
  26. import com.itextpdf.layout.renderer.CellRenderer;
  27. import com.itextpdf.layout.renderer.DrawContext;
  28. import com.itextpdf.samples.GenericTest;
  29. import com.itextpdf.test.annotations.type.SampleTest;
  30.  
  31. import org.junit.experimental.categories.Category;
  32.  
  33. import java.io.File;
  34.  
  35. @Category(SampleTest.class)
  36. public class TiledBackground extends GenericTest {
  37.     public static final String DEST = "./target/test/resources/sandbox/tables/tiled_background.pdf";
  38.     public static final String IMG1 = "./src/test/resources/img/ALxRF.png";
  39.     public static final String IMG2 = "./src/test/resources/img/bulb.gif";
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new TiledBackground().manipulatePdf(DEST);
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  50.         Document doc = new Document(pdfDoc);
  51.  
  52.         Table table = new Table(2);
  53.         Cell cell = new Cell();
  54.         ImageData image = ImageDataFactory.create(IMG1);
  55.         cell.setNextRenderer(new TiledImageBackgroundCellRenderer(cell, image));
  56.         cell.setHeight(770);
  57.         table.addCell(cell);
  58.         cell = new Cell();
  59.         image = ImageDataFactory.create(IMG2);
  60.         cell.setNextRenderer(new TiledImageBackgroundCellRenderer(cell, image));
  61.         cell.setHeight(770);
  62.         table.addCell(cell);
  63.         doc.add(table);
  64.  
  65.         doc.close();
  66.     }
  67.  
  68.  
  69.     private class TiledImageBackgroundCellRenderer extends CellRenderer {
  70.         protected ImageData img;
  71.  
  72.         public TiledImageBackgroundCellRenderer(Cell modelElement, ImageData img) {
  73.             super(modelElement);
  74.             this.img = img;
  75.         }
  76.  
  77.         public void colorRectangle(PdfCanvas canvas, Color color, float x, float y, float width, float height) {
  78.             canvas.saveState().setFillColor(color).rectangle(x, y, width, height).fillStroke().restoreState();
  79.         }
  80.  
  81.         @Override
  82.         public void draw(DrawContext drawContext) {
  83.             PdfPattern.Tiling img_pattern = new PdfPattern.Tiling(img.getWidth(), img.getHeight(), img.getWidth(),
  84.                     img.getHeight());
  85.             new PdfPatternCanvas(img_pattern, drawContext.getDocument()).addImage(img, 0, 0, false);
  86.             PdfCanvas canvas = drawContext.getCanvas();
  87.             colorRectangle(canvas, new PatternColor(img_pattern), getOccupiedAreaBBox().getX(),
  88.                     getOccupiedAreaBBox().getY(), getOccupiedAreaBBox().getWidth(), getOccupiedAreaBBox().getHeight());
  89.             canvas.setFillColor(new PatternColor(img_pattern));
  90.             canvas.rectangle(getOccupiedAreaBBox());
  91.             canvas.stroke();
  92.         }
  93.     }
  94. }
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