Table and cell events to draw borders

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

24th October 2015
admin-marketing

Switch code for this example

DottedLineCell.java
  1. package sandbox.tables;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Paragraph;
  6. import com.itextpdf.text.Phrase;
  7. import com.itextpdf.text.Rectangle;
  8. import com.itextpdf.text.pdf.PdfContentByte;
  9. import com.itextpdf.text.pdf.PdfPCell;
  10. import com.itextpdf.text.pdf.PdfPCellEvent;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfPTableEvent;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class DottedLineCell {
  23.  
  24.     public static final String DEST = "results/tables/dotted_line_cell.pdf";
  25.    
  26.     class DottedCells implements PdfPTableEvent {
  27.  
  28.         public void tableLayout(PdfPTable table, float[][] widths,
  29.             float[] heights, int headerRows, int rowStart,
  30.             PdfContentByte[] canvases) {
  31.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  32.             canvas.setLineDash(3f, 3f);
  33.             float llx = widths[0][0];
  34.             float urx = widths[0][widths.length];
  35.             for (int i = 0; i < heights.length; i++) {
  36.                 canvas.moveTo(llx, heights[i]);
  37.                 canvas.lineTo(urx, heights[i]);
  38.             }
  39.             for (int i = 0; i < widths.length; i++) {
  40.                 for (int j = 0; j < widths[i].length; j++) {
  41.                     canvas.moveTo(widths[i][j], heights[i]);
  42.                     canvas.lineTo(widths[i][j], heights[i+1]);
  43.                 }
  44.             }
  45.             canvas.stroke();
  46.         }
  47.     }
  48.    
  49.     class DottedCell implements PdfPCellEvent {
  50.         public void cellLayout(PdfPCell cell, Rectangle position,
  51.             PdfContentByte[] canvases) {
  52.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  53.             canvas.setLineDash(3f, 3f);
  54.             canvas.rectangle(position.getLeft(), position.getBottom(),
  55.                 position.getWidth(), position.getHeight());
  56.             canvas.stroke();
  57.         }
  58.     }
  59.    
  60.     public static void main(String[] args) throws IOException, DocumentException {
  61.         File file = new File(DEST);
  62.         file.getParentFile().mkdirs();
  63.         new DottedLineCell().createPdf(DEST);
  64.     }
  65.    
  66.     public void createPdf(String dest) throws IOException, DocumentException {
  67.         DottedLineCell app = new DottedLineCell();
  68.         Document document = new Document();
  69.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  70.         document.open();
  71.         document.add(new Paragraph("Table event"));
  72.         PdfPTable table = new PdfPTable(3);
  73.         table.setTableEvent(app.new DottedCells());
  74.         table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
  75.         table.addCell("A1");
  76.         table.addCell("A2");
  77.         table.addCell("A3");
  78.         table.addCell("B1");
  79.         table.addCell("B2");
  80.         table.addCell("B3");
  81.         table.addCell("C1");
  82.         table.addCell("C2");
  83.         table.addCell("C3");
  84.         document.add(table);
  85.         document.add(new Paragraph("Cell event"));
  86.         table = new PdfPTable(1);
  87.         PdfPCell cell = new PdfPCell(new Phrase("Test"));
  88.         cell.setCellEvent(app.new DottedCell());
  89.         cell.setBorder(PdfPCell.NO_BORDER);
  90.         table.addCell(cell);
  91.         document.add(table);
  92.         document.close();
  93.     }
  94. }
RoundedCorners.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following questions:
  3.  * http://stackoverflow.com/questions/30106862/left-and-right-top-round-corner-for-rectangelroundrectangle
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPCellEvent;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class RoundedCorners {
  24.  
  25.     public static final String DEST = "results/tables/rounded_corners.pdf";
  26.    
  27.     class SpecialRoundedCell implements PdfPCellEvent {
  28.         public void cellLayout(PdfPCell cell, Rectangle position,
  29.             PdfContentByte[] canvases) {
  30.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  31.             float llx = position.getLeft() + 2;
  32.             float lly = position.getBottom() + 2;
  33.             float urx = position.getRight() - 2;
  34.             float ury = position.getTop() - 2;
  35.             float r = 4;
  36.             float b = 0.4477f;
  37.             canvas.moveTo(llx, lly);
  38.             canvas.lineTo(urx, lly);
  39.             canvas.lineTo(urx, ury - r);
  40.             canvas.curveTo(urx, ury - r * b, urx - r * b, ury, urx - r, ury);
  41.             canvas.lineTo(llx + r, ury);
  42.             canvas.curveTo(llx + r * b, ury, llx, ury - r * b, llx, ury - r);
  43.             canvas.lineTo(llx, lly);
  44.             canvas.stroke();
  45.         }
  46.     }
  47.    
  48.     public static void main(String[] args) throws IOException, DocumentException {
  49.         File file = new File(DEST);
  50.         file.getParentFile().mkdirs();
  51.         new RoundedCorners().createPdf(DEST);
  52.     }
  53.    
  54.     public void createPdf(String dest) throws IOException, DocumentException {
  55.         Document document = new Document();
  56.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  57.         document.open();
  58.         PdfPTable table = new PdfPTable(3);
  59.         PdfPCell cell = getCell("These cells have rounded borders at the top.");
  60.         table.addCell(cell);
  61.         cell = getCell("These cells aren't rounded at the bottom.");
  62.         table.addCell(cell);
  63.         cell = getCell("A custom cell event was used to achieve this.");
  64.         table.addCell(cell);
  65.         document.add(table);
  66.         document.close();
  67.     }
  68.    
  69.     public PdfPCell getCell(String content) {
  70.         PdfPCell cell = new PdfPCell(new Phrase(content));
  71.         cell.setCellEvent(new SpecialRoundedCell());
  72.         cell.setPadding(5);
  73.         cell.setBorder(PdfPCell.NO_BORDER);
  74.         return cell;
  75.     }
  76. }
DottedLineHeader.java
  1. package sandbox.tables;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Paragraph;
  6. import com.itextpdf.text.Rectangle;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPCellEvent;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfPTableEvent;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class DottedLineHeader {
  21.  
  22.     public static final String DEST = "results/tables/dotted_line_header.pdf";
  23.    
  24.     class DottedHeader implements PdfPTableEvent {
  25.  
  26.         public void tableLayout(PdfPTable table, float[][] widths,
  27.             float[] heights, int headerRows, int rowStart,
  28.             PdfContentByte[] canvases) {
  29.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  30.             canvas.setLineDash(3f, 3f);
  31.             float x1 = widths[0][0];
  32.             float x2 = widths[0][widths.length];
  33.             canvas.moveTo(x1, heights[0]);
  34.             canvas.lineTo(x2, heights[0]);
  35.             canvas.moveTo(x1, heights[headerRows]);
  36.             canvas.lineTo(x2, heights[headerRows]);
  37.             canvas.stroke();
  38.         }
  39.     }
  40.    
  41.     class DottedCell implements PdfPCellEvent {
  42.         public void cellLayout(PdfPCell cell, Rectangle position,
  43.             PdfContentByte[] canvases) {
  44.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  45.             canvas.setLineDash(3f, 3f);
  46.             canvas.moveTo(position.getLeft(), position.getTop());
  47.             canvas.lineTo(position.getRight(), position.getTop());
  48.             canvas.moveTo(position.getLeft(), position.getBottom());
  49.             canvas.lineTo(position.getRight(), position.getBottom());
  50.             canvas.stroke();
  51.         }
  52.     }
  53.    
  54.     public static void main(String[] args) throws IOException, DocumentException {
  55.         File file = new File(DEST);
  56.         file.getParentFile().mkdirs();
  57.         new DottedLineHeader().createPdf(DEST);
  58.     }
  59.    
  60.     public void createPdf(String dest) throws IOException, DocumentException {
  61.         Document document = new Document();
  62.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  63.         document.open();
  64.         document.add(new Paragraph("Table event"));
  65.         PdfPTable table = new PdfPTable(3);
  66.         table.setTableEvent(new DottedHeader());
  67.         table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
  68.         table.addCell("A1");
  69.         table.addCell("A2");
  70.         table.addCell("A3");
  71.         table.setHeaderRows(1);
  72.         table.addCell("B1");
  73.         table.addCell("B2");
  74.         table.addCell("B3");
  75.         table.addCell("C1");
  76.         table.addCell("C2");
  77.         table.addCell("C3");
  78.         document.add(table);
  79.         document.add(new Paragraph("Cell event"));
  80.         table = new PdfPTable(3);
  81.         table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
  82.         table.getDefaultCell().setCellEvent(new DottedCell());
  83.         table.addCell("A1");
  84.         table.addCell("A2");
  85.         table.addCell("A3");
  86.         table.getDefaultCell().setCellEvent(null);
  87.         table.addCell("B1");
  88.         table.addCell("B2");
  89.         table.addCell("B3");
  90.         table.addCell("C1");
  91.         table.addCell("C2");
  92.         table.addCell("C3");
  93.         document.add(table);
  94.         document.close();
  95.     }
  96. }
CustomBorder.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPRow;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfPTableEventAfterSplit;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class CustomBorder {
  25.    
  26.     public static final String DEST = "results/tables/custom_border.pdf";
  27.  
  28.     public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point. It should result in rows that are split and rows that aren't.";
  29.    
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new CustomBorder().createPdf(DEST);
  34.     }
  35.    
  36.     class BorderEvent implements PdfPTableEventAfterSplit {
  37.        
  38.         protected int rowCount;
  39.         protected boolean bottom = true;
  40.         protected boolean top = true;
  41.        
  42.         public void setRowCount(int rowCount) {
  43.             this.rowCount = rowCount;
  44.         }
  45.        
  46.         public void splitTable(PdfPTable table) {
  47.             if (table.getRows().size() != rowCount) {
  48.                 bottom = false;
  49.             }
  50.         }
  51.  
  52.         public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
  53.             if (table.getRows().size() != rowCount) {
  54.             // if the table gains a row, a row was split
  55.                 rowCount = table.getRows().size();
  56.                 top = false;
  57.             }
  58.         }
  59.        
  60.         public void tableLayout(PdfPTable table, float[][] width, float[] height,
  61.                 int headerRows, int rowStart, PdfContentByte[] canvas) {
  62.             float widths[] = width[0];
  63.             float y1 = height[0];
  64.             float y2 = height[height.length - 1];
  65.             PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
  66.             for (int i = 0; i < widths.length; i++) {
  67.                 cb.moveTo(widths[i], y1);
  68.                 cb.lineTo(widths[i], y2);
  69.             }
  70.             float x1 = widths[0];
  71.             float x2 = widths[widths.length - 1];
  72.             for (int i = top ? 0 : 1; i < (bottom ? height.length : height.length - 1); i++) {
  73.                 cb.moveTo(x1, height[i]);
  74.                 cb.lineTo(x2, height[i]);
  75.             }
  76.             cb.stroke();
  77.             cb.resetRGBColorStroke();
  78.             bottom = true;
  79.             top = true;
  80.         }
  81.     }
  82.    
  83.     public void createPdf(String dest) throws IOException, DocumentException {
  84.         Document document = new Document();
  85.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  86.         document.open();
  87.         PdfPTable table = new PdfPTable(2);
  88.         table.setTotalWidth(500);
  89.         table.setLockedWidth(true);
  90.         BorderEvent event = new BorderEvent();
  91.         table.setTableEvent(event);
  92.         table.setWidthPercentage(100);
  93.         table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  94.         table.setSplitLate(false);
  95.         PdfPCell cell = new PdfPCell(new Phrase(TEXT));
  96.         cell.setBorder(Rectangle.NO_BORDER);
  97.         for (int i = 0; i < 60; ) {
  98.             table.addCell("Cell " + (++i));
  99.             table.addCell(cell);
  100.         }
  101.         event.setRowCount(table.getRows().size());
  102.         document.add(table);
  103.         document.close();
  104.     }
  105. }
CustomBorder2.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/23935566/table-borders-not-expanding-properly-in-pdf-using-itext
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPRow;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfPTableEventAfterSplit;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class CustomBorder2 {
  25.    
  26.     public static final String DEST = "results/tables/custom_border2.pdf";
  27.  
  28.     public static final String TEXT = "This is some long paragraph that will be added over and over again to prove a point. It should result in rows that are split and rows that aren't.";
  29.    
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new CustomBorder2().createPdf(DEST);
  34.     }
  35.    
  36.     class BorderEvent implements PdfPTableEventAfterSplit {
  37.        
  38.         protected boolean bottom = true;
  39.         protected boolean top = true;
  40.        
  41.         public void splitTable(PdfPTable table) {
  42.             bottom = false;
  43.         }
  44.  
  45.         public void afterSplitTable(PdfPTable table, PdfPRow startRow, int startIdx) {
  46.             top = false;
  47.         }
  48.        
  49.         public void tableLayout(PdfPTable table, float[][] width, float[] height,
  50.                 int headerRows, int rowStart, PdfContentByte[] canvas) {
  51.             float widths[] = width[0];
  52.             float y1 = height[0];
  53.             float y2 = height[height.length - 1];
  54.             float x1 = widths[0];
  55.             float x2 = widths[widths.length - 1];
  56.             PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
  57.             cb.moveTo(x1, y1);
  58.             cb.lineTo(x1, y2);
  59.             cb.moveTo(x2, y1);
  60.             cb.lineTo(x2, y2);
  61.             if (top) {
  62.                 cb.moveTo(x1, y1);
  63.                 cb.lineTo(x2, y1);
  64.             }
  65.             if (bottom) {
  66.                 cb.moveTo(x1, y2);
  67.                 cb.lineTo(x2, y2);
  68.             }
  69.             cb.stroke();
  70.             cb.resetRGBColorStroke();
  71.             bottom = true;
  72.             top = true;
  73.         }
  74.     }
  75.    
  76.     public void createPdf(String dest) throws IOException, DocumentException {
  77.         Document document = new Document();
  78.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  79.         document.open();
  80.         PdfPTable table = new PdfPTable(2);
  81.         table.setTotalWidth(500);
  82.         table.setLockedWidth(true);
  83.         BorderEvent event = new BorderEvent();
  84.         table.setTableEvent(event);
  85.         table.setWidthPercentage(100);
  86.         table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  87.         table.setSplitLate(false);
  88.         PdfPCell cell = new PdfPCell(new Phrase(TEXT));
  89.         cell.setBorder(Rectangle.NO_BORDER);
  90.         for (int i = 0; i < 60; ) {
  91.             table.addCell("Cell " + (++i));
  92.             table.addCell(cell);
  93.         }
  94.         document.add(table);
  95.         document.close();
  96.     }
  97. }
DottedLineCell2.java
  1. package sandbox.tables;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Phrase;
  6. import com.itextpdf.text.Rectangle;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPCellEvent;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class DottedLineCell2 {
  21.  
  22.     public static final String DEST = "results/tables/dotted_line_cell2.pdf";
  23.    
  24.     class DottedCell implements PdfPCellEvent {
  25.         private int border = 0;
  26.         public DottedCell(int border) {
  27.             this.border = border;
  28.         }
  29.         public void cellLayout(PdfPCell cell, Rectangle position,
  30.             PdfContentByte[] canvases) {
  31.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  32.             canvas.saveState();
  33.             canvas.setLineDash(0, 4, 2);
  34.             if ((border & PdfPCell.TOP) == PdfPCell.TOP) {
  35.                 canvas.moveTo(position.getRight(), position.getTop());
  36.                 canvas.lineTo(position.getLeft(), position.getTop());
  37.             }
  38.             if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) {
  39.                 canvas.moveTo(position.getRight(), position.getBottom());
  40.                 canvas.lineTo(position.getLeft(), position.getBottom());
  41.             }
  42.             if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) {
  43.                 canvas.moveTo(position.getRight(), position.getTop());
  44.                 canvas.lineTo(position.getRight(), position.getBottom());
  45.             }
  46.             if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) {
  47.                 canvas.moveTo(position.getLeft(), position.getTop());
  48.                 canvas.lineTo(position.getLeft(), position.getBottom());
  49.             }
  50.             canvas.stroke();
  51.             canvas.restoreState();
  52.         }
  53.     }
  54.    
  55.     public static void main(String[] args) throws IOException, DocumentException {
  56.         File file = new File(DEST);
  57.         file.getParentFile().mkdirs();
  58.         new DottedLineCell2().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.        
  66.         PdfPTable table;
  67.         PdfPCell cell;
  68.        
  69.         table = new PdfPTable(4);
  70.         table.setSpacingAfter(30);
  71.         cell = new PdfPCell(new Phrase("left border"));
  72.         cell.setBorder(PdfPCell.NO_BORDER);
  73.         cell.setCellEvent(new DottedCell(PdfPCell.LEFT));
  74.         table.addCell(cell);
  75.         cell = new PdfPCell(new Phrase("right border"));
  76.         cell.setBorder(PdfPCell.NO_BORDER);
  77.         cell.setCellEvent(new DottedCell(PdfPCell.RIGHT));
  78.         table.addCell(cell);
  79.         cell = new PdfPCell(new Phrase("top border"));
  80.         cell.setBorder(PdfPCell.NO_BORDER);
  81.         cell.setCellEvent(new DottedCell(PdfPCell.TOP));
  82.         table.addCell(cell);
  83.         cell = new PdfPCell(new Phrase("bottom border"));
  84.         cell.setBorder(PdfPCell.NO_BORDER);
  85.         cell.setCellEvent(new DottedCell(PdfPCell.BOTTOM));
  86.         table.addCell(cell);
  87.         document.add(table);
  88.        
  89.         table = new PdfPTable(4);
  90.         table.setSpacingAfter(30);
  91.         cell = new PdfPCell(new Phrase("left and top border"));
  92.         cell.setBorder(PdfPCell.NO_BORDER);
  93.         cell.setCellEvent(new DottedCell(PdfPCell.LEFT | PdfPCell.TOP));
  94.         table.addCell(cell);
  95.         cell = new PdfPCell(new Phrase("right and bottom border"));
  96.         cell.setBorder(PdfPCell.NO_BORDER);
  97.         cell.setCellEvent(new DottedCell(PdfPCell.RIGHT | PdfPCell.BOTTOM));
  98.         table.addCell(cell);
  99.         cell = new PdfPCell(new Phrase("no border"));
  100.         cell.setBorder(PdfPCell.NO_BORDER);
  101.         table.addCell(cell);
  102.         cell = new PdfPCell(new Phrase("full border"));
  103.         cell.setBorder(PdfPCell.NO_BORDER);
  104.         cell.setCellEvent(new DottedCell(PdfPCell.BOX));
  105.         table.addCell(cell);
  106.         document.add(table);
  107.         document.close();
  108.     }
  109. }
CustomBorder3.java
  1. package sandbox.tables;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Phrase;
  6. import com.itextpdf.text.Rectangle;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPCellEvent;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class CustomBorder3 {
  21.  
  22.     public static final String DEST = "results/tables/custom_border_3.pdf";
  23.    
  24.     interface LineDash {
  25.         public void applyLineDash(PdfContentByte canvas);
  26.     }
  27.    
  28.     class SolidLine implements LineDash {
  29.         public void applyLineDash(PdfContentByte canvas) { }
  30.     }
  31.    
  32.     class DottedLine implements LineDash {
  33.         public void applyLineDash(PdfContentByte canvas) {
  34.             canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
  35.             canvas.setLineDash(0, 4, 2);
  36.         }
  37.     }
  38.    
  39.     class DashedLine implements LineDash {
  40.         public void applyLineDash(PdfContentByte canvas) {
  41.             canvas.setLineDash(3, 3);
  42.         }
  43.     }
  44.    
  45.     class CustomBorder implements PdfPCellEvent {
  46.         protected LineDash left;
  47.         protected LineDash right;
  48.         protected LineDash top;
  49.         protected LineDash bottom;
  50.         public CustomBorder(LineDash left, LineDash right,
  51.                 LineDash top, LineDash bottom) {
  52.             this.left = left;
  53.             this.right = right;
  54.             this.top = top;
  55.             this.bottom = bottom;
  56.         }
  57.         public void cellLayout(PdfPCell cell, Rectangle position,
  58.             PdfContentByte[] canvases) {
  59.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  60.             if (top != null) {
  61.                 canvas.saveState();
  62.                 top.applyLineDash(canvas);
  63.                 canvas.moveTo(position.getRight(), position.getTop());
  64.                 canvas.lineTo(position.getLeft(), position.getTop());
  65.                 canvas.stroke();
  66.                 canvas.restoreState();
  67.             }
  68.             if (bottom != null) {
  69.                 canvas.saveState();
  70.                 bottom.applyLineDash(canvas);
  71.                 canvas.moveTo(position.getRight(), position.getBottom());
  72.                 canvas.lineTo(position.getLeft(), position.getBottom());
  73.                 canvas.stroke();
  74.                 canvas.restoreState();
  75.             }
  76.             if (right != null) {
  77.                 canvas.saveState();
  78.                 right.applyLineDash(canvas);
  79.                 canvas.moveTo(position.getRight(), position.getTop());
  80.                 canvas.lineTo(position.getRight(), position.getBottom());
  81.                 canvas.stroke();
  82.                 canvas.restoreState();
  83.             }
  84.             if (left != null) {
  85.                 canvas.saveState();
  86.                 left.applyLineDash(canvas);
  87.                 canvas.moveTo(position.getLeft(), position.getTop());
  88.                 canvas.lineTo(position.getLeft(), position.getBottom());
  89.                 canvas.stroke();
  90.                 canvas.restoreState();
  91.             }
  92.         }
  93.     }
  94.    
  95.     public static void main(String[] args) throws IOException, DocumentException {
  96.         File file = new File(DEST);
  97.         file.getParentFile().mkdirs();
  98.         new CustomBorder3().createPdf(DEST);
  99.     }
  100.    
  101.     public void createPdf(String dest) throws IOException, DocumentException {
  102.         Document document = new Document();
  103.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  104.         document.open();
  105.        
  106.         PdfPTable table;
  107.         PdfPCell cell;
  108.         LineDash solid = new SolidLine();
  109.         LineDash dotted = new DottedLine();
  110.         LineDash dashed = new DashedLine();
  111.        
  112.         table = new PdfPTable(4);
  113.         table.setSpacingAfter(30);
  114.         cell = new PdfPCell(new Phrase("dotted left border"));
  115.         cell.setBorder(PdfPCell.NO_BORDER);
  116.         cell.setCellEvent(new CustomBorder(dotted, null, null, null));
  117.         table.addCell(cell);
  118.         cell = new PdfPCell(new Phrase("solid right border"));
  119.         cell.setBorder(PdfPCell.NO_BORDER);
  120.         cell.setCellEvent(new CustomBorder(null, solid, null, null));
  121.         table.addCell(cell);
  122.         cell = new PdfPCell(new Phrase("dashed top border"));
  123.         cell.setBorder(PdfPCell.NO_BORDER);
  124.         cell.setCellEvent(new CustomBorder(null, null, dashed, null));
  125.         table.addCell(cell);
  126.         cell = new PdfPCell(new Phrase("bottom border"));
  127.         cell.setBorder(PdfPCell.NO_BORDER);
  128.         cell.setCellEvent(new CustomBorder(null, null, null, solid));
  129.         table.addCell(cell);
  130.         document.add(table);
  131.        
  132.         table = new PdfPTable(4);
  133.         table.setSpacingAfter(30);
  134.         cell = new PdfPCell(new Phrase("dotted left and solid top border"));
  135.         cell.setBorder(PdfPCell.NO_BORDER);
  136.         cell.setCellEvent(new CustomBorder(dotted, null, solid, null));
  137.         table.addCell(cell);
  138.         cell = new PdfPCell(new Phrase("dashed right and dashed bottom border"));
  139.         cell.setBorder(PdfPCell.NO_BORDER);
  140.         cell.setCellEvent(new CustomBorder(null, dashed, null, dashed));
  141.         table.addCell(cell);
  142.         cell = new PdfPCell(new Phrase("no border"));
  143.         cell.setBorder(PdfPCell.NO_BORDER);
  144.         table.addCell(cell);
  145.         cell = new PdfPCell(new Phrase("full solid border"));
  146.         cell.setBorder(PdfPCell.NO_BORDER);
  147.         cell.setCellEvent(new CustomBorder(solid, solid, solid, solid));
  148.         table.addCell(cell);
  149.         document.add(table);
  150.         document.close();
  151.     }
  152. }
CustomBorder4.java
  1. package sandbox.tables;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Phrase;
  6. import com.itextpdf.text.Rectangle;
  7. import com.itextpdf.text.pdf.PdfContentByte;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPCellEvent;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class CustomBorder4 {
  21.  
  22.     public static final String DEST = "results/tables/custom_border_4.pdf";
  23.    
  24.     abstract class CustomBorder implements PdfPCellEvent {
  25.         private int border = 0;
  26.         public CustomBorder(int border) {
  27.             this.border = border;
  28.         }
  29.         public void cellLayout(PdfPCell cell, Rectangle position,
  30.             PdfContentByte[] canvases) {
  31.             PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
  32.             canvas.saveState();
  33.             setLineDash(canvas);
  34.             if ((border & PdfPCell.TOP) == PdfPCell.TOP) {
  35.                 canvas.moveTo(position.getRight(), position.getTop());
  36.                 canvas.lineTo(position.getLeft(), position.getTop());
  37.             }
  38.             if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) {
  39.                 canvas.moveTo(position.getRight(), position.getBottom());
  40.                 canvas.lineTo(position.getLeft(), position.getBottom());
  41.             }
  42.             if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) {
  43.                 canvas.moveTo(position.getRight(), position.getTop());
  44.                 canvas.lineTo(position.getRight(), position.getBottom());
  45.             }
  46.             if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) {
  47.                 canvas.moveTo(position.getLeft(), position.getTop());
  48.                 canvas.lineTo(position.getLeft(), position.getBottom());
  49.             }
  50.             canvas.stroke();
  51.             canvas.restoreState();
  52.         }
  53.        
  54.         public abstract void setLineDash(PdfContentByte canvas);
  55.     }
  56.    
  57.     class SolidBorder extends CustomBorder {
  58.         public SolidBorder(int border) { super(border); }
  59.         public void setLineDash(PdfContentByte canvas) {}
  60.     }
  61.     class DottedBorder extends CustomBorder {
  62.         public DottedBorder(int border) { super(border); }
  63.         public void setLineDash(PdfContentByte canvas) {
  64.             canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND);
  65.             canvas.setLineDash(0, 4, 2);
  66.         }
  67.     }
  68.     class DashedBorder extends CustomBorder {
  69.         public DashedBorder(int border) { super(border); }
  70.         public void setLineDash(PdfContentByte canvas) {
  71.             canvas.setLineDash(3, 3);
  72.         }
  73.     }
  74.    
  75.     public static void main(String[] args) throws IOException, DocumentException {
  76.         File file = new File(DEST);
  77.         file.getParentFile().mkdirs();
  78.         new CustomBorder4().createPdf(DEST);
  79.     }
  80.    
  81.     public void createPdf(String dest) throws IOException, DocumentException {
  82.         Document document = new Document();
  83.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  84.         document.open();
  85.        
  86.         PdfPTable table;
  87.         PdfPCell cell;
  88.        
  89.         table = new PdfPTable(4);
  90.         table.setSpacingAfter(30);
  91.         cell = new PdfPCell(new Phrase("dotted left border"));
  92.         cell.setBorder(PdfPCell.NO_BORDER);
  93.         cell.setCellEvent(new DottedBorder(PdfPCell.LEFT));
  94.         table.addCell(cell);
  95.         cell = new PdfPCell(new Phrase("solid right border"));
  96.         cell.setBorder(PdfPCell.NO_BORDER);
  97.         cell.setCellEvent(new SolidBorder(PdfPCell.RIGHT));
  98.         table.addCell(cell);
  99.         cell = new PdfPCell(new Phrase("solid top border"));
  100.         cell.setBorder(PdfPCell.NO_BORDER);
  101.         cell.setCellEvent(new SolidBorder(PdfPCell.TOP));
  102.         table.addCell(cell);
  103.         cell = new PdfPCell(new Phrase("dashed bottom border"));
  104.         cell.setBorder(PdfPCell.NO_BORDER);
  105.         cell.setCellEvent(new DashedBorder(PdfPCell.BOTTOM));
  106.         table.addCell(cell);
  107.         document.add(table);
  108.        
  109.         table = new PdfPTable(4);
  110.         table.setSpacingAfter(30);
  111.         cell = new PdfPCell(new Phrase("dotted left and dashed top border"));
  112.         cell.setBorder(PdfPCell.NO_BORDER);
  113.         cell.setCellEvent(new DottedBorder(PdfPCell.LEFT));
  114.         cell.setCellEvent(new DashedBorder(PdfPCell.TOP));
  115.         table.addCell(cell);
  116.         cell = new PdfPCell(new Phrase("solid right and dotted bottom border"));
  117.         cell.setBorder(PdfPCell.NO_BORDER);
  118.         cell.setCellEvent(new DottedBorder(PdfPCell.BOTTOM));
  119.         cell.setCellEvent(new SolidBorder(PdfPCell.RIGHT));
  120.         table.addCell(cell);
  121.         cell = new PdfPCell(new Phrase("no border"));
  122.         cell.setBorder(PdfPCell.NO_BORDER);
  123.         table.addCell(cell);
  124.         cell = new PdfPCell(new Phrase("full border"));
  125.         cell.setBorder(PdfPCell.NO_BORDER);
  126.         cell.setCellEvent(new DottedBorder(PdfPCell.LEFT | PdfPCell.RIGHT));
  127.         cell.setCellEvent(new SolidBorder(PdfPCell.TOP));
  128.         cell.setCellEvent(new DashedBorder(PdfPCell.BOTTOM));
  129.         table.addCell(cell);
  130.         document.add(table);
  131.         document.close();
  132.     }
  133. }
TableBorder.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/35340003
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfPTableEvent;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class TableBorder {
  25.    
  26.     public static final String DEST = "results/tables/table_border_outer_only.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 TableBorder().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         PdfPTable table = new PdfPTable(4);
  40.         table.setTableEvent(new BorderEvent());
  41.         table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  42.         for(int aw = 0; aw < 16; aw++){
  43.             table.addCell("hi");
  44.         }
  45.         document.add(table);
  46.         document.close();
  47.     }
  48.    
  49.     public class BorderEvent implements PdfPTableEvent {
  50.         public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
  51.             float width[] = widths[0];
  52.             float x1 = width[0];
  53.             float x2 = width[width.length - 1];
  54.             float y1 = heights[0];
  55.             float y2 = heights[heights.length - 1];
  56.             PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
  57.             cb.rectangle(x1, y1, x2 - x1, y2 - y1);
  58.             cb.stroke();
  59.             cb.resetRGBColorStroke();
  60.         }
  61.     }
  62. }
CellTitle.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/35746651
  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 CellTitle {
  30.  
  31.     public static final String DEST = "results/tables/cell_title.pdf";
  32.    
  33.     class Title implements PdfPCellEvent {
  34.         protected String title;
  35.        
  36.         public Title(String title) {
  37.             this.title = title;
  38.         }
  39.        
  40.         public void cellLayout(PdfPCell cell, Rectangle position,
  41.             PdfContentByte[] canvases) {
  42.             Chunk c = new Chunk(title);
  43.             c.setBackground(BaseColor.LIGHT_GRAY);
  44.             PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  45.             ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
  46.                 new Phrase(c), position.getLeft(5), position.getTop(5), 0);
  47.         }
  48.     }
  49.    
  50.     public static void main(String[] args) throws IOException, DocumentException {
  51.         File file = new File(DEST);
  52.         file.getParentFile().mkdirs();
  53.         new CellTitle().createPdf(DEST);
  54.     }
  55.    
  56.     public void createPdf(String dest) throws IOException, DocumentException {
  57.         Document document = new Document();
  58.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  59.         document.open();
  60.         PdfPTable table = new PdfPTable(1);
  61.         PdfPCell cell = getCell("The title of this cell is title 1", "title 1");
  62.         table.addCell(cell);
  63.         cell = getCell("The title of this cell is title 2", "title 2");
  64.         table.addCell(cell);
  65.         cell = getCell("The title of this cell is title 3", "title 3");
  66.         table.addCell(cell);
  67.         document.add(table);
  68.         document.close();
  69.     }
  70.    
  71.     public PdfPCell getCell(String content, String title) {
  72.         PdfPCell cell = new PdfPCell(new Phrase(content));
  73.         cell.setCellEvent(new Title(title));
  74.         cell.setPadding(5);
  75.         return cell;
  76.     }
  77.    
  78. }
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