Adding a background to a table

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

24th October 2015
admin-marketing

Switch code for this example

ImageBackground.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/24162974/how-to-set-background-image-in-pdfpcell-in-itext
  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.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.GrayColor;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfPCell;
  18. import com.itextpdf.text.pdf.PdfPCellEvent;
  19. import com.itextpdf.text.pdf.PdfPTable;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import sandbox.WrapToTest;
  26.  
  27. @WrapToTest
  28. public class ImageBackground {
  29.  
  30.     class ImageBackgroundEvent implements PdfPCellEvent {
  31.  
  32.         protected Image image;
  33.        
  34.         public ImageBackgroundEvent(Image image) {
  35.             this.image = image;
  36.         }
  37.        
  38.         public void cellLayout(PdfPCell cell, Rectangle position,
  39.                 PdfContentByte[] canvases) {
  40.             try {
  41.                 PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
  42.                 image.scaleAbsolute(position);
  43.                 image.setAbsolutePosition(position.getLeft(), position.getBottom());
  44.                 cb.addImage(image);
  45.             } catch (DocumentException e) {
  46.                 throw new ExceptionConverter(e);
  47.             }
  48.         }
  49.        
  50.     }
  51.    
  52.     public static final String DEST = "results/tables/imagebackground.pdf";
  53.     public static final String IMG1 = "resources/images/bruno.jpg";
  54.    
  55.     public static void main(String[] args) throws IOException, DocumentException {
  56.         File file = new File(DEST);
  57.         file.getParentFile().mkdirs();
  58.         new ImageBackground().createPdf(DEST);
  59.     }
  60.    
  61.     public void createPdf(String dest) throws IOException, DocumentException {
  62.         Document document = new Document();
  63.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  64.         document.open();
  65.         PdfPTable table = new PdfPTable(1);
  66.         table.setTotalWidth(400);
  67.         table.setLockedWidth(true);
  68.         PdfPCell cell = new PdfPCell();
  69.         Font font = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, GrayColor.GRAYWHITE);
  70.         Paragraph p = new Paragraph("A cell with an image as background color.", font);
  71.         cell.addElement(p);
  72.         Image image = Image.getInstance(IMG1);
  73.         cell.setCellEvent(new ImageBackgroundEvent(image));
  74.         cell.setFixedHeight(600 * image.getScaledHeight() / image.getScaledWidth());
  75.         table.addCell(cell);
  76.         document.add(table);
  77.         document.close();
  78.     }
  79. }
ColoredBackground.java
  1. /**
  2.  * Example written by Bruno Lowagie and Nishanthi Grashia in answer to the following question:
  3.  * http://stackoverflow.com/questions/27871574/appears-space-between-cells-without-border-itextpdf
  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.Element;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.FontFactory;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.PdfPCell;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class ColoredBackground {
  26.     public static final String DEST = "results/tables/colored_background.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException,
  29.             DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new ColoredBackground().createPdf(DEST);
  33.     }
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         PdfPTable table;
  39.         PdfPCell cell;
  40.         Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, BaseColor.WHITE);
  41.         table = new PdfPTable(16);
  42.         for(int aw = 0; aw < 16; aw++){
  43.             cell = new PdfPCell(new Phrase("hi", font));
  44.             cell.setBackgroundColor(BaseColor.BLUE);
  45.             cell.setBorder(Rectangle.NO_BORDER);
  46.             cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  47.             cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  48.             table.addCell(cell);
  49.         }
  50.         document.add(table);
  51.         document.close();
  52.     }
  53.  
  54. }
SimpleTable10.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/30267169/cannot-display-background-color-when-using-rowspan-with-itext-pdf
  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.Phrase;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  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 SimpleTable10 {
  22.     public static final String DEST = "results/tables/simple_table10.pdf";
  23.  
  24.     public static void main(String[] args) throws IOException,
  25.             DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new SimpleTable10().createPdf(DEST);
  29.     }
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         PdfPTable table = new PdfPTable(5);
  35.         PdfPCell sn = new PdfPCell(new Phrase("S/N"));
  36.         sn.setRowspan(2);
  37.         sn.setBackgroundColor(BaseColor.YELLOW);
  38.         table.addCell(sn);
  39.         PdfPCell name = new PdfPCell(new Phrase("Name"));
  40.         name.setColspan(3);
  41.         name.setBackgroundColor(BaseColor.CYAN);
  42.         table.addCell(name);
  43.         PdfPCell age = new PdfPCell(new Phrase("Age"));
  44.         age.setRowspan(2);
  45.         age.setBackgroundColor(BaseColor.GRAY);
  46.         table.addCell(age);
  47.         PdfPCell surname = new PdfPCell(new Phrase("SURNAME"));
  48.         surname.setBackgroundColor(BaseColor.BLUE);
  49.         table.addCell(surname);
  50.         PdfPCell firstname = new PdfPCell(new Phrase("FIRST NAME"));
  51.         firstname.setBackgroundColor(BaseColor.RED);
  52.         table.addCell(firstname);
  53.         PdfPCell middlename = new PdfPCell(new Phrase("MIDDLE NAME"));
  54.         middlename.setBackgroundColor(BaseColor.GREEN);
  55.         table.addCell(middlename);
  56.         PdfPCell f1 = new PdfPCell(new Phrase("1"));
  57.         f1.setBackgroundColor(BaseColor.PINK);
  58.         table.addCell(f1);
  59.         PdfPCell f2 = new PdfPCell(new Phrase("James"));
  60.         f2.setBackgroundColor(BaseColor.MAGENTA);
  61.         table.addCell(f2);
  62.         PdfPCell f3 = new PdfPCell(new Phrase("Fish"));
  63.         f3.setBackgroundColor(BaseColor.ORANGE);
  64.         table.addCell(f3);
  65.         PdfPCell f4 = new PdfPCell(new Phrase("Stone"));
  66.         f4.setBackgroundColor(BaseColor.DARK_GRAY);
  67.         table.addCell(f4);
  68.         PdfPCell f5 = new PdfPCell(new Phrase("17"));
  69.         f5.setBackgroundColor(BaseColor.LIGHT_GRAY);
  70.         table.addCell(f5);
  71.         document.add(table);
  72.         document.close();
  73.     }
  74.  
  75. }
TiledBackground.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/23374557/itextsharp-why-cell-background-image-is-rotated-90-degress-clockwise
  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.PdfPatternPainter;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class TiledBackground {
  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.                 PdfPatternPainter patternPainter = cb.createPattern(image.getScaledWidth(), image.getScaledHeight());
  40.                 image.setAbsolutePosition(0, 0);
  41.                 patternPainter.addImage(image);
  42.                 cb.saveState();
  43.                 cb.setPatternFill(patternPainter);
  44.                 cb.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
  45.                 cb.fill();
  46.                 cb.restoreState();
  47.             } catch (DocumentException e) {
  48.                 throw new ExceptionConverter(e);
  49.             }
  50.         }
  51.        
  52.     }
  53.    
  54.     public static final String DEST = "results/tables/tiled_pattern.pdf";
  55.     public static final String IMG1 = "resources/images/ALxRF.png";
  56.     public static final String IMG2 = "resources/images/bulb.gif";
  57.    
  58.     public static void main(String[] args) throws IOException, DocumentException {
  59.         File file = new File(DEST);
  60.         file.getParentFile().mkdirs();
  61.         new TiledBackground().createPdf(DEST);
  62.     }
  63.    
  64.     public void createPdf(String dest) throws IOException, DocumentException {
  65.         Document document = new Document();
  66.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  67.         document.open();
  68.         PdfPTable table = new PdfPTable(2);
  69.         PdfPCell cell = new PdfPCell();
  70.         Image image = Image.getInstance(IMG1);
  71.         cell.setCellEvent(new TiledImageBackground(image));
  72.         cell.setFixedHeight(770);
  73.         table.addCell(cell);
  74.         cell = new PdfPCell();
  75.         image = Image.getInstance(IMG2);
  76.         cell.setCellEvent(new TiledImageBackground(image));
  77.         cell.setFixedHeight(770);
  78.         table.addCell(cell);
  79.         document.add(table);
  80.         document.close();
  81.     }
  82. }
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