Cell borders (without cell or table events)

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

29th January 2016
admin-marketing

iText 5 is no longer being developed, except for security fixes. Click the following link to see an updated example for iText 7.

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


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