Cell borders (without cell or table events)

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

31st May 2016
iText PDF

Switch code for this example

ColoredBorder.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/35073619
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.border.SolidBorder;
  19. import com.itextpdf.layout.element.Cell;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import java.io.File;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. @Category(SampleTest.class)
  29. public class ColoredBorder extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/tables/colored_border.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new ColoredBorder().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  41.         Document doc = new Document(pdfDoc);
  42.  
  43.         Table table;
  44.         table = new Table(2);
  45.         Cell cell;
  46.         cell = new Cell().add("Cell 1");
  47.         cell.setBorderTop(new SolidBorder(Color.RED, 1));
  48.         cell.setBorderBottom(new SolidBorder(Color.BLUE, 1));
  49.         table.addCell(cell);
  50.         cell = new Cell().add("Cell 2");
  51.         cell.setBorderLeft(new SolidBorder(Color.GREEN, 5));
  52.         cell.setBorderTop(new SolidBorder(Color.YELLOW, 8));
  53.         table.addCell(cell);
  54.         cell = new Cell().add("Cell 3");
  55.         cell.setBorderLeft(new SolidBorder(Color.RED, 1));
  56.         cell.setBorderBottom(new SolidBorder(Color.BLUE, 1));
  57.         table.addCell(cell);
  58.         cell = new Cell().add("Cell 4");
  59.         cell.setBorderLeft(new SolidBorder(Color.GREEN, 5));
  60.         cell.setBorderTop(new SolidBorder(Color.YELLOW, 8));
  61.         table.addCell(cell);
  62.  
  63.         doc.add(table);
  64.  
  65.         doc.close();
  66.     }
  67. }
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