Using cell events to add special content

Tags: tablescell eventiText 5
Files: 
/*
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/36253087
 */
package sandbox.tables;
 
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
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;
 
/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class DiagonalCell {
 
    public static final String DEST = "results/tables/diagonal_cell.pdf";
 
    class Diagonal implements PdfPCellEvent {
        protected String columns;
        protected String rows;
 
        public Diagonal(String columns, String rows) {
            this.columns = columns;
            this.rows = rows;
        }
 
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
            ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, 
                new Phrase(columns), position.getRight(2), position.getTop(12), 0);
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, 
                new Phrase(rows), position.getLeft(2), position.getBottom(2), 0);
            canvas = canvases[PdfPTable.LINECANVAS];
            canvas.moveTo(position.getLeft(), position.getTop());
            canvas.lineTo(position.getRight(), position.getBottom());
            canvas.stroke();
        }
    }
 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DiagonalCell().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(6);
        table.getDefaultCell().setMinimumHeight(30);
        PdfPCell cell = new PdfPCell();
        cell.setCellEvent(new Diagonal("Gravity", "Occ"));
        table.addCell(cell);
        table.addCell("1");
        table.addCell("2");
        table.addCell("3");
        table.addCell("4");
        table.addCell("5");
        for (int i = 0; i < 5; ) {
            table.addCell(String.valueOf(++i));
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
        }
        document.add(table);
        document.close();
    }
}
package sandbox.tables;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
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 sandbox.WrapToTest;
 
@WrapToTest
public class SubTotal {
    public static final String DEST = "results/tables/subtotal.pdf";
 
    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SubTotal().createPdf(DEST);
    }
 
    class Totals {
    	double subtotal = 0;
    	double total = 0;
    }
 
    class SubTotalEvent implements PdfPCellEvent {
 
    	Double price;
    	Totals totals;
 
    	public SubTotalEvent(Totals totals, double price) {
    		this.totals = totals;
    		this.price = price;
    	}
 
    	public SubTotalEvent(Totals totals) {
    		this.totals = totals;
    		price = null;
    	}
 
		@Override
		public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
			if (price == null) {
				PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
				ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
						new Phrase(String.valueOf(totals.subtotal)),
						position.getLeft() + 2, position.getBottom() + 2, 0);
				totals.subtotal = 0;
				return;
			}
			totals.subtotal += price;
			totals.total += price;
		}
 
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
 
    	Totals totals = new Totals();
 
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(5);
        table.setWidths(new int[]{1, 1, 1, 3, 3});
        // header
        table.addCell("Pos");
        table.addCell("Menge");
        table.addCell("Text");
        table.addCell("Einzerpreis");
        table.addCell("Summe");
        // footer
        PdfPCell cell = new PdfPCell(new Phrase("Subtotal"));
        cell.setColspan(4);
        table.addCell(cell);
        cell = new PdfPCell();
        cell.setCellEvent(new SubTotalEvent(totals));
        table.addCell(cell);
        // definitions
        table.setHeaderRows(2);
        table.setFooterRows(1);
        // table body
        for(int r = 0; r < 50; ){
            table.addCell(String.valueOf(++r));
            table.addCell("1");
            table.addCell("text");
            table.addCell("10.0");
            cell = new PdfPCell(new Phrase("10.0"));
            cell.setCellEvent(new SubTotalEvent(totals, 10));
            table.addCell(cell);
        }
        document.add(table);
        // extra footer
        table = new PdfPTable(5);
        table.setWidths(new int[]{1, 1, 1, 3, 3});
        cell = new PdfPCell(new Phrase("Grand total"));
        cell.setColspan(4);
        table.addCell(cell);
        table.addCell(String.valueOf(totals.total));
        document.add(table);
        document.close();
    }
}
File nameRaw URLUpdated
DiagonalCell.javaDiagonalCell.java2016-03-29 3:41 pm
SubTotal.javaSubTotal.java2016-09-26 4:25 pm
Results: 
File nameRaw URLUpdated
cmp_diagonal_cell.pdfcmp_diagonal_cell.pdf2016-03-29 3:43 pm