Cell borders (without cell or table events)

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

29th January 2016
admin-marketing

Switch code for this example

ColoredBorder.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/35073619
  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 ColoredBorder {
  26.     public static final String DEST = "results/tables/colored_border.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 ColoredBorder().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.         table = new PdfPTable(2);
  40.         PdfPCell cell;
  41.         cell = new PdfPCell(new Phrase("Cell 1"));
  42.         cell.setUseVariableBorders(true);
  43.         cell.setBorderColorTop(BaseColor.RED);
  44.         cell.setBorderColorBottom(BaseColor.BLUE);
  45.         table.addCell(cell);
  46.         cell = new PdfPCell(new Phrase("Cell 2"));
  47.         cell.setBorderWidthLeft(5);
  48.         cell.setBorderColorLeft(BaseColor.GREEN);
  49.         cell.setBorderWidthTop(8);
  50.         cell.setBorderColorTop(BaseColor.YELLOW);
  51.         table.addCell(cell);
  52.         cell = new PdfPCell(new Phrase("Cell 3"));
  53.         cell.setUseVariableBorders(true);
  54.         cell.setBorder(Rectangle.LEFT | Rectangle.BOTTOM);
  55.         cell.setBorderColorLeft(BaseColor.RED);
  56.         cell.setBorderColorBottom(BaseColor.BLUE);
  57.         table.addCell(cell);
  58.         cell = new PdfPCell(new Phrase("Cell 4"));
  59.         cell.setBorder(Rectangle.LEFT | Rectangle.TOP);
  60.         cell.setUseBorderPadding(true);
  61.         cell.setBorderWidthLeft(5);
  62.         cell.setBorderColorLeft(BaseColor.GREEN);
  63.         cell.setBorderWidthTop(8);
  64.         cell.setBorderColorTop(BaseColor.YELLOW);
  65.         table.addCell(cell);
  66.         document.add(table);
  67.         document.close();
  68.     }
  69.  
  70. }
RowBackground.java
  1. /*
  2.  * Example written in answer to the SO question:
  3.  * http://stackoverflow.com/questions/39154089
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.sql.SQLException;
  11.  
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.PageSize;
  15. import com.itextpdf.text.Rectangle;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfPTableEvent;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20. import java.io.File;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class RowBackground {
  25.  
  26.     public class RowBackgroundEvent implements PdfPTableEvent {
  27.         // the row number of the row that needs a background
  28.         protected int row;
  29.    
  30.         // creates a background event for a specific row
  31.         public RowBackgroundEvent(int row) {
  32.             this.row = row;
  33.         }
  34.    
  35.         /**
  36.          * Draws the background of a row.
  37.          */
  38.         @Override
  39.         public void tableLayout(PdfPTable table, float[][] widths, float[] heights,
  40.             int headerRows, int rowStart, PdfContentByte[] canvases) {
  41.             float llx = widths[row][0];
  42.             float lly = heights[row];
  43.             float urx = widths[row][widths[row].length - 1];
  44.             float ury = heights[row - 1];
  45.             float h = ury - lly;
  46.             PdfContentByte canvas = canvases[PdfPTable.BASECANVAS];
  47.             canvas.saveState();
  48.             canvas.arc(llx - h / 2, lly, llx + h / 2, ury, 90, 180);
  49.             canvas.lineTo(urx, lly);
  50.             canvas.arc(urx - h / 2, lly, urx + h / 2, ury, 270, 180);
  51.             canvas.lineTo(llx, ury);
  52.             canvas.setColorFill(BaseColor.LIGHT_GRAY);
  53.             canvas.fill();
  54.             canvas.restoreState();
  55.         }
  56.     }
  57.    
  58.     /** The resulting PDF file. */
  59.     public static final String DEST = "results/tables/row_background.pdf";
  60.  
  61.     /**
  62.      * Creates a PDF document.
  63.      * @param filename the path to the new PDF document
  64.      * @throws    DocumentException
  65.      * @throws    IOException
  66.      * @throws    SQLException
  67.      */
  68.     public void createPdf(String filename) throws SQLException, DocumentException, IOException {
  69.         // step 1
  70.         Document document = new Document(PageSize.A4.rotate());
  71.         // step 2
  72.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  73.         // step 3
  74.         document.open();
  75.         // step 4
  76.         PdfPTableEvent event = new RowBackgroundEvent(3);
  77.         PdfPTable table = new PdfPTable(7);
  78.         table.setTableEvent(event);
  79.         table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  80.         for (int i = 0; i < 10; i++) {
  81.             for (int j = 1; j < 8; j++) {
  82.                 table.addCell(String.valueOf(j));
  83.             }
  84.         }
  85.         document.add(table);
  86.         // step 5
  87.         document.close();
  88.     }
  89.  
  90.     /**
  91.      * Main method.
  92.      * @param    args    no arguments needed
  93.      * @throws DocumentException
  94.      * @throws IOException
  95.      * @throws SQLException
  96.      */
  97.     public static void main(String[] args) throws SQLException, DocumentException, IOException {
  98.         File file = new File(DEST);
  99.         file.getParentFile().mkdirs();
  100.         new RowBackground().createPdf(DEST);
  101.     }
  102. }
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