Using cell events to add special content

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

29th March 2016
admin-marketing

Switch code for this example

DiagonalCell.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/36253087
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.Phrase;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfPCell;
  17. import com.itextpdf.text.pdf.PdfPCellEvent;
  18. import com.itextpdf.text.pdf.PdfPTable;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. /**
  26.  * @author Bruno Lowagie (iText Software)
  27.  */
  28. @WrapToTest
  29. public class DiagonalCell {
  30.    
  31.     public static final String DEST = "results/tables/diagonal_cell.pdf";
  32.    
  33.     class Diagonal implements PdfPCellEvent {
  34.         protected String columns;
  35.         protected String rows;
  36.        
  37.         public Diagonal(String columns, String rows) {
  38.             this.columns = columns;
  39.             this.rows = rows;
  40.         }
  41.        
  42.         public void cellLayout(PdfPCell cell, Rectangle position,
  43.             PdfContentByte[] canvases) {
  44.             PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  45.             ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT,
  46.                 new Phrase(columns), position.getRight(2), position.getTop(12), 0);
  47.             ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  48.                 new Phrase(rows), position.getLeft(2), position.getBottom(2), 0);
  49.             canvas = canvases[PdfPTable.LINECANVAS];
  50.             canvas.moveTo(position.getLeft(), position.getTop());
  51.             canvas.lineTo(position.getRight(), position.getBottom());
  52.             canvas.stroke();
  53.         }
  54.     }
  55.    
  56.     public static void main(String[] args) throws IOException, DocumentException {
  57.         File file = new File(DEST);
  58.         file.getParentFile().mkdirs();
  59.         new DiagonalCell().createPdf(DEST);
  60.     }
  61.    
  62.     public void createPdf(String dest) throws IOException, DocumentException {
  63.         Document document = new Document();
  64.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  65.         document.open();
  66.         PdfPTable table = new PdfPTable(6);
  67.         table.getDefaultCell().setMinimumHeight(30);
  68.         PdfPCell cell = new PdfPCell();
  69.         cell.setCellEvent(new Diagonal("Gravity", "Occ"));
  70.         table.addCell(cell);
  71.         table.addCell("1");
  72.         table.addCell("2");
  73.         table.addCell("3");
  74.         table.addCell("4");
  75.         table.addCell("5");
  76.         for (int i = 0; i < 5; ) {
  77.             table.addCell(String.valueOf(++i));
  78.             table.addCell("");
  79.             table.addCell("");
  80.             table.addCell("");
  81.             table.addCell("");
  82.             table.addCell("");
  83.         }
  84.         document.add(table);
  85.         document.close();
  86.     }
  87. }
SubTotal.java
  1. package sandbox.tables;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPCellEvent;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class SubTotal {
  23.     public static final String DEST = "results/tables/subtotal.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException,
  26.             DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new SubTotal().createPdf(DEST);
  30.     }
  31.  
  32.     class Totals {
  33.         double subtotal = 0;
  34.         double total = 0;
  35.     }
  36.    
  37.     class SubTotalEvent implements PdfPCellEvent {
  38.  
  39.         Double price;
  40.         Totals totals;
  41.        
  42.         public SubTotalEvent(Totals totals, double price) {
  43.             this.totals = totals;
  44.             this.price = price;
  45.         }
  46.        
  47.         public SubTotalEvent(Totals totals) {
  48.             this.totals = totals;
  49.             price = null;
  50.         }
  51.        
  52.         @Override
  53.         public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  54.             if (price == null) {
  55.                 PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  56.                 ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  57.                         new Phrase(String.valueOf(totals.subtotal)),
  58.                         position.getLeft() + 2, position.getBottom() + 2, 0);
  59.                 totals.subtotal = 0;
  60.                 return;
  61.             }
  62.             totals.subtotal += price;
  63.             totals.total += price;
  64.         }
  65.        
  66.     }
  67.    
  68.     public void createPdf(String dest) throws IOException, DocumentException {
  69.        
  70.         Totals totals = new Totals();
  71.        
  72.         Document document = new Document();
  73.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  74.         document.open();
  75.         PdfPTable table = new PdfPTable(5);
  76.         table.setWidths(new int[]{1, 1, 1, 3, 3});
  77.         // header
  78.         table.addCell("Pos");
  79.         table.addCell("Menge");
  80.         table.addCell("Text");
  81.         table.addCell("Einzerpreis");
  82.         table.addCell("Summe");
  83.         // footer
  84.         PdfPCell cell = new PdfPCell(new Phrase("Subtotal"));
  85.         cell.setColspan(4);
  86.         table.addCell(cell);
  87.         cell = new PdfPCell();
  88.         cell.setCellEvent(new SubTotalEvent(totals));
  89.         table.addCell(cell);
  90.         // definitions
  91.         table.setHeaderRows(2);
  92.         table.setFooterRows(1);
  93.         // table body
  94.         for(int r = 0; r < 50; ){
  95.             table.addCell(String.valueOf(++r));
  96.             table.addCell("1");
  97.             table.addCell("text");
  98.             table.addCell("10.0");
  99.             cell = new PdfPCell(new Phrase("10.0"));
  100.             cell.setCellEvent(new SubTotalEvent(totals, 10));
  101.             table.addCell(cell);
  102.         }
  103.         document.add(table);
  104.         // extra footer
  105.         table = new PdfPTable(5);
  106.         table.setWidths(new int[]{1, 1, 1, 3, 3});
  107.         cell = new PdfPCell(new Phrase("Grand total"));
  108.         cell.setColspan(4);
  109.         table.addCell(cell);
  110.         table.addCell(String.valueOf(totals.total));
  111.         document.add(table);
  112.         document.close();
  113.     }
  114. }
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