Example usage for com.lowagie.text.pdf PdfPCellEvent PdfPCellEvent

List of usage examples for com.lowagie.text.pdf PdfPCellEvent PdfPCellEvent

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPCellEvent PdfPCellEvent.

Prototype

PdfPCellEvent

Source Link

Usage

From source file:org.unitime.timetable.export.PDFPrinter.java

License:Open Source License

@Override
public void printLine(String... fields) {
    PdfPCellEvent setLineDashEvent = new PdfPCellEvent() {
        @Override//  w ww.  j ava  2s. co m
        public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) {
            PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
            cb.setLineDash(new float[] { 2, 2 }, 0);
        }
    };

    for (int idx = 0; idx < fields.length; idx++) {
        if (iHiddenColumns.contains(idx))
            continue;
        String f = fields[idx];
        if (f == null || f.isEmpty() || (iCheckLast
                && f.equals(iLastLine == null || idx >= iLastLine.length ? null : iLastLine[idx])))
            f = "";

        boolean number = sNumber.matcher(f).matches();

        Font font = PdfFont.getFont();
        Phrase p = new Phrase(f, PdfFont.getSmallFont());

        PdfPCell cell = new PdfPCell(p);
        cell.setBorder(iLastLine == null ? Rectangle.TOP : Rectangle.NO_BORDER);
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(number ? Element.ALIGN_RIGHT : Element.ALIGN_LEFT);
        cell.setPaddingBottom(4f);
        cell.setCellEvent(setLineDashEvent);
        if (number)
            cell.setPaddingRight(10f);
        iTable.addCell(cell);

        float width = 0;
        if (f.indexOf('\n') >= 0) {
            for (StringTokenizer s = new StringTokenizer(f, "\n"); s.hasMoreTokens();)
                width = Math.max(width, font.getBaseFont().getWidthPoint(s.nextToken(), font.getSize()));
        } else
            width = Math.max(width, font.getBaseFont().getWidthPoint(f, font.getSize()));
        iMaxWidth[idx] = Math.max(iMaxWidth[idx], width + (number ? 10 : 0));
    }
    iLastLine = fields;
}