Skip to main content
Skip table of contents

Cell borders (without cell or table events)


coloredborder

JAVA

JAVA
/**
 * 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

JAVA
/*
 * 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);
    }
}


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.