Adding a background to a table

Tags: tablesbackgroundbackground imageiText 5imagesimage in cellcell event

Examples written in answer to questions such as:

Files: 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/24162974/how-to-set-background-image-in-pdfpcell-in-itext
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ImageBackground {
 
    class ImageBackgroundEvent implements PdfPCellEvent {
 
        protected Image image;
 
        public ImageBackgroundEvent(Image image) {
            this.image = image;
        }
 
        public void cellLayout(PdfPCell cell, Rectangle position,
                PdfContentByte[] canvases) {
            try {
                PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
                image.scaleAbsolute(position);
                image.setAbsolutePosition(position.getLeft(), position.getBottom());
                cb.addImage(image);
            } catch (DocumentException e) {
                throw new ExceptionConverter(e);
            }
        }
 
    }
 
    public static final String DEST = "results/tables/imagebackground.pdf";
    public static final String IMG1 = "resources/images/bruno.jpg";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ImageBackground().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(1);
        table.setTotalWidth(400);
        table.setLockedWidth(true);
        PdfPCell cell = new PdfPCell();
        Font font = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, GrayColor.GRAYWHITE);
        Paragraph p = new Paragraph("A cell with an image as background color.", font);
        cell.addElement(p);
        Image image = Image.getInstance(IMG1);
        cell.setCellEvent(new ImageBackgroundEvent(image));
        cell.setFixedHeight(600 * image.getScaledHeight() / image.getScaledWidth());
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie and Nishanthi Grashia in answer to the following question:
 * http://stackoverflow.com/questions/27871574/appears-space-between-cells-without-border-itextpdf
 */
package sandbox.tables;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class ColoredBackground {
    public static final String DEST = "results/tables/colored_background.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ColoredBackground().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table;
        PdfPCell cell;
        Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, BaseColor.WHITE);
        table = new PdfPTable(16);
        for(int aw = 0; aw < 16; aw++){
            cell = new PdfPCell(new Phrase("hi", font));
            cell.setBackgroundColor(BaseColor.BLUE);
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
        }
        document.add(table);
        document.close();
    }
 
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/30267169/cannot-display-background-color-when-using-rowspan-with-itext-pdf
 */
package sandbox.tables;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class SimpleTable10 {
    public static final String DEST = "results/tables/simple_table10.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable10().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(5);
        PdfPCell sn = new PdfPCell(new Phrase("S/N"));
        sn.setRowspan(2);
        sn.setBackgroundColor(BaseColor.YELLOW);
        table.addCell(sn);
        PdfPCell name = new PdfPCell(new Phrase("Name"));
        name.setColspan(3);
        name.setBackgroundColor(BaseColor.CYAN);
        table.addCell(name);
        PdfPCell age = new PdfPCell(new Phrase("Age"));
        age.setRowspan(2);
        age.setBackgroundColor(BaseColor.GRAY);
        table.addCell(age);
        PdfPCell surname = new PdfPCell(new Phrase("SURNAME"));
        surname.setBackgroundColor(BaseColor.BLUE);
        table.addCell(surname);
        PdfPCell firstname = new PdfPCell(new Phrase("FIRST NAME"));
        firstname.setBackgroundColor(BaseColor.RED);
        table.addCell(firstname);
        PdfPCell middlename = new PdfPCell(new Phrase("MIDDLE NAME"));
        middlename.setBackgroundColor(BaseColor.GREEN);
        table.addCell(middlename);
        PdfPCell f1 = new PdfPCell(new Phrase("1"));
        f1.setBackgroundColor(BaseColor.PINK);
        table.addCell(f1);
        PdfPCell f2 = new PdfPCell(new Phrase("James"));
        f2.setBackgroundColor(BaseColor.MAGENTA);
        table.addCell(f2);
        PdfPCell f3 = new PdfPCell(new Phrase("Fish"));
        f3.setBackgroundColor(BaseColor.ORANGE);
        table.addCell(f3);
        PdfPCell f4 = new PdfPCell(new Phrase("Stone"));
        f4.setBackgroundColor(BaseColor.DARK_GRAY);
        table.addCell(f4);
        PdfPCell f5 = new PdfPCell(new Phrase("17"));
        f5.setBackgroundColor(BaseColor.LIGHT_GRAY);
        table.addCell(f5);
        document.add(table);
        document.close();
    }
 
}
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/23374557/itextsharp-why-cell-background-image-is-rotated-90-degress-clockwise
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPatternPainter;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class TiledBackground {
 
    class TiledImageBackground implements PdfPCellEvent {
 
        protected Image image;
 
        public TiledImageBackground(Image image) {
            this.image = image;
        }
 
        public void cellLayout(PdfPCell cell, Rectangle position,
                PdfContentByte[] canvases) {
            try {
                PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
                PdfPatternPainter patternPainter = cb.createPattern(image.getScaledWidth(), image.getScaledHeight());
                image.setAbsolutePosition(0, 0);
                patternPainter.addImage(image);
                cb.saveState();
                cb.setPatternFill(patternPainter);
                cb.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
                cb.fill();
                cb.restoreState();
            } catch (DocumentException e) {
                throw new ExceptionConverter(e);
            }
        }
 
    }
 
    public static final String DEST = "results/tables/tiled_pattern.pdf";
    public static final String IMG1 = "resources/images/ALxRF.png";
    public static final String IMG2 = "resources/images/bulb.gif";
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TiledBackground().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        PdfPCell cell = new PdfPCell();
        Image image = Image.getInstance(IMG1);
        cell.setCellEvent(new TiledImageBackground(image));
        cell.setFixedHeight(770);
        table.addCell(cell);
        cell = new PdfPCell();
        image = Image.getInstance(IMG2);
        cell.setCellEvent(new TiledImageBackground(image));
        cell.setFixedHeight(770);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}
Resources: 
File nameRaw URLUpdated
bruno.jpgbruno.jpg2015-10-23 7:47 pm
ALxRF.pngALxRF.png2015-11-08 4:48 pm
bulb.gifbulb.gif2015-11-08 12:36 pm