Example usage for com.lowagie.text Table Table

List of usage examples for com.lowagie.text Table Table

Introduction

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

Prototype

public Table(int columns, int rows) throws BadElementException 

Source Link

Document

Constructs a Table with a certain number of columns and a certain number of Rows.

Usage

From source file:org.activityinfo.server.report.renderer.itext.ItextTableRenderer.java

License:Open Source License

private void renderTable(Document document, TableData data) throws DocumentException {
    int colDepth = data.getRootColumn().getDepth();
    List<TableColumn> colLeaves = data.getRootColumn().getLeaves();
    int colBreadth = colLeaves.size();

    Table table = new Table(colBreadth, 1);
    table.setUseVariableBorders(true);/*w  w  w.j ava 2s .c  o  m*/
    table.setWidth(100.0f);
    table.setBorderWidth(0);

    // first write the column headers

    for (int depth = 1; depth <= colDepth; ++depth) {
        List<TableColumn> columns = data.getRootColumn().getDescendantsAtDepth(depth);
        for (TableColumn column : columns) {
            Cell cell = ThemeHelper.columnHeaderCell(column.getLabel(), column.isLeaf(), computeHAlign(column));
            cell.setColspan(Math.max(1, column.getChildren().size()));
            cell.setRowspan(colDepth - depth - column.getDepth() + 1);
            table.addCell(cell);
        }
    }
    table.endHeaders();

    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    NumberFormat numberFormat = NumberFormat.getIntegerInstance();
    numberFormat.setGroupingUsed(true);

    for (SiteDTO row : data.getRows()) {
        for (TableColumn column : colLeaves) {

            Object value = row.get(column.getSitePropertyName());

            String label = "";
            if (value instanceof Date) {
                label = dateFormat.format(value);
            } else if (value instanceof Number) {
                label = numberFormat.format(value);
            } else if (value != null) {
                label = value.toString();
            }

            table.addCell(ThemeHelper.bodyCell(label, false, 0, true, computeHAlign(column)));
        }
    }
    document.add(table);
}

From source file:org.kuali.kfs.module.cam.report.DepreciationReport.java

License:Open Source License

/**
 * This method adds the log lines into the report
 * /*from   w  w w . j av  a 2  s. com*/
 * @param reportLog
 */
private void generateReportLogBody(List<String[]> reportLog) {
    try {
        Font font = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL);
        int columnwidths[];
        columnwidths = new int[] { 40, 15 };

        Table aTable = new Table(2, linesPerPage);
        int rowsWritten = 0;
        for (String[] columns : reportLog) {
            if (pageNumber == 0 || line >= linesPerPage) {
                if (pageNumber > 0) {
                    this.document.add(aTable);
                }
                int elementsLeft = reportLog.size() - rowsWritten;
                int rowsNeeded = (elementsLeft >= linesPerPage ? linesPerPage : elementsLeft);
                this.document.newPage();

                this.generateColumnHeaders();

                aTable = new Table(2, rowsNeeded); // 12 columns, 11 rows.

                aTable.setAutoFillEmptyCells(true);
                aTable.setPadding(3);
                aTable.setWidths(columnwidths);
                aTable.setWidth(100);
                aTable.setBorder(Rectangle.NO_BORDER);

                line = 0;
                pageNumber++;
            }
            rowsWritten++;

            Cell cell;
            cell = new Cell(new Phrase(columns[0], font));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            aTable.addCell(cell);

            cell = new Cell(new Phrase(columns[1], font));
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            aTable.addCell(cell);
            line++;
        }
        this.document.add(aTable);
    } catch (DocumentException de) {
        throw new RuntimeException(
                "DepreciationReport.generateReportLogBody(List<String[]> reportLog) - error: "
                        + de.getMessage());
    }
}

From source file:org.kuali.kfs.module.cam.report.DepreciationReport.java

License:Open Source License

/**
 * This method creates a report group for the error message on the report
 * /*from  ww w  .j  a  v a2s. c  o  m*/
 * @throws DocumentException
 */
private void generateErrorColumnHeaders() throws DocumentException {
    try {
        int headerwidths[] = { 60 };

        Table aTable = new Table(1, 1); // 2 columns, 1 rows.

        aTable.setAutoFillEmptyCells(true);
        aTable.setPadding(3);
        aTable.setWidths(headerwidths);
        aTable.setWidth(100);

        Cell cell;

        Font font = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL);

        cell = new Cell(new Phrase("Error(s)", font));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setGrayFill(0.9f);
        aTable.addCell(cell);

        this.document.add(aTable);

    } catch (Exception e) {
        throw new RuntimeException(
                "DepreciationReport.generateErrorColumnHeaders() - Error: " + e.getMessage());
    }
}

From source file:org.kuali.kfs.module.cam.report.DepreciationReport.java

License:Open Source License

/**
 * This method creates the headers for the report statistics
 *//*from ww w .j a  v a2 s . c o m*/
private void generateColumnHeaders() {
    try {
        int headerwidths[] = { 40, 15 };

        Table aTable = new Table(2, 1); // 2 columns, 1 rows.

        aTable.setAutoFillEmptyCells(true);
        aTable.setPadding(3);
        aTable.setWidths(headerwidths);
        aTable.setWidth(100);

        Cell cell;

        Font font = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL);

        cell = new Cell(new Phrase(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
                CamsKeyConstants.Depreciation.MSG_REPORT_DEPRECIATION_HEADING1), font));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setGrayFill(0.9f);
        aTable.addCell(cell);

        cell = new Cell(new Phrase(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
                CamsKeyConstants.Depreciation.MSG_REPORT_DEPRECIATION_HEADING2), font));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setGrayFill(0.9f);
        aTable.addCell(cell);
        this.document.add(aTable);

    } catch (Exception e) {
        throw new RuntimeException("DepreciationReport.generateColumnHeaders() - Error: " + e.getMessage());
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.helper.RTFPrinter.java

License:Open Source License

/**
 * @noinspection IOResourceOpenedButNotSafelyClosed
 *///from w ww .j  av  a2 s  . c o m
public void print(final LogicalPageKey logicalPageKey, final LogicalPageBox logicalPage,
        final TableContentProducer contentProducer, final RTFOutputProcessorMetaData metaData,
        final boolean incremental) throws ContentProcessingException {
    final int startRow = contentProducer.getFinishedRows();
    final int finishRow = contentProducer.getFilledRows();
    if (incremental && startRow == finishRow) {
        return;
    }

    if (document == null) {
        this.cellBackgroundProducer = new CellBackgroundProducer(
                metaData.isFeatureSupported(AbstractTableOutputProcessor.TREAT_ELLIPSE_AS_RECTANGLE),
                metaData.isFeatureSupported(OutputProcessorFeature.UNALIGNED_PAGEBANDS));

        final PhysicalPageBox pageFormat = logicalPage.getPageGrid().getPage(0, 0);
        final float urx = (float) StrictGeomUtility.toExternalValue(pageFormat.getWidth());
        final float ury = (float) StrictGeomUtility.toExternalValue(pageFormat.getHeight());

        final float marginLeft = (float) StrictGeomUtility.toExternalValue(pageFormat.getImageableX());
        final float marginRight = (float) StrictGeomUtility.toExternalValue(
                pageFormat.getWidth() - pageFormat.getImageableWidth() - pageFormat.getImageableX());
        final float marginTop = (float) StrictGeomUtility.toExternalValue(pageFormat.getImageableY());
        final float marginBottom = (float) StrictGeomUtility.toExternalValue(
                pageFormat.getHeight() - pageFormat.getImageableHeight() - pageFormat.getImageableY());
        final Rectangle pageSize = new Rectangle(urx, ury);

        document = new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom);
        imageCache = new RTFImageCache(resourceManager);

        // rtf does not support PageFormats or other meta data...
        final PatchRtfWriter2 instance = PatchRtfWriter2.getInstance(document,
                new NoCloseOutputStream(outputStream));
        instance.getDocumentSettings().setAlwaysUseUnicode(true);

        final String author = config
                .getConfigProperty("org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.Author");
        if (author != null) {
            document.addAuthor(author);
        }

        final String title = config
                .getConfigProperty("org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.Title");
        if (title != null) {
            document.addTitle(title);
        }

        document.addProducer();
        document.addCreator(RTFPrinter.CREATOR);

        try {
            document.addCreationDate();
        } catch (Exception e) {
            RTFPrinter.logger.debug("Unable to add creation date. It will have to work without it.", e);
        }

        document.open();
    }

    // Start a new page.
    try {
        final SheetLayout sheetLayout = contentProducer.getSheetLayout();
        final int columnCount = contentProducer.getColumnCount();
        if (table == null) {
            final int rowCount = contentProducer.getRowCount();
            table = new Table(columnCount, rowCount);
            table.setAutoFillEmptyCells(false);
            table.setWidth(100); // span the full page..
            // and finally the content ..

            final float[] cellWidths = new float[columnCount];
            for (int i = 0; i < columnCount; i++) {
                cellWidths[i] = (float) StrictGeomUtility.toExternalValue(sheetLayout.getCellWidth(i, i + 1));
            }
            table.setWidths(cellWidths);
        }

        // logger.debug ("Processing: " + startRow + " " + finishRow + " " + incremental);

        for (int row = startRow; row < finishRow; row++) {
            for (short col = 0; col < columnCount; col++) {
                final RenderBox content = contentProducer.getContent(row, col);
                final CellMarker.SectionType sectionType = contentProducer.getSectionType(row, col);

                if (content == null) {
                    final RenderBox backgroundBox = contentProducer.getBackground(row, col);
                    final CellBackground background;
                    if (backgroundBox != null) {
                        background = cellBackgroundProducer.getBackgroundForBox(logicalPage, sheetLayout, col,
                                row, 1, 1, true, sectionType, backgroundBox);
                    } else {
                        background = cellBackgroundProducer.getBackgroundAt(logicalPage, sheetLayout, col, row,
                                true, sectionType);
                    }
                    if (background == null) {
                        // An empty cell .. ignore
                        final PatchRtfCell cell = new PatchRtfCell();
                        cell.setBorderWidth(0);
                        cell.setMinimumHeight(
                                (float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row)));
                        table.addCell(cell, row, col);
                        continue;
                    }

                    // A empty cell with a defined background ..
                    final PatchRtfCell cell = new PatchRtfCell();
                    cell.setBorderWidth(0);
                    cell.setMinimumHeight(
                            (float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row)));
                    updateCellStyle(cell, background);
                    table.addCell(cell, row, col);
                    continue;
                }

                if (content.isCommited() == false) {
                    throw new InvalidReportStateException("Uncommited content encountered");
                }

                final long contentOffset = contentProducer.getContentOffset(row, col);
                final long colPos = sheetLayout.getXPosition(col);
                final long rowPos = sheetLayout.getYPosition(row);
                if (content.getX() != colPos || (content.getY() + contentOffset) != rowPos) {
                    // A spanned cell ..
                    continue;
                }

                final int colSpan = sheetLayout.getColSpan(col, content.getX() + content.getWidth());
                final int rowSpan = sheetLayout.getRowSpan(row,
                        content.getY() + content.getHeight() + contentOffset);

                final CellBackground realBackground = cellBackgroundProducer.getBackgroundForBox(logicalPage,
                        sheetLayout, col, row, colSpan, rowSpan, false, sectionType, content);

                final PatchRtfCell cell = new PatchRtfCell();
                cell.setRowspan(rowSpan);
                cell.setColspan(colSpan);
                cell.setBorderWidth(0);
                cell.setMinimumHeight((float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row)));
                if (realBackground != null) {
                    updateCellStyle(cell, realBackground);
                }

                computeCellStyle(content, cell);

                // export the cell and all content ..
                final RTFTextExtractor etx = new RTFTextExtractor(metaData);
                etx.compute(content, cell, imageCache);

                table.addCell(cell, row, col);
                content.setFinishedTable(true);
                // logger.debug("set Finished to cell (" + col + ", " + row + "," + content.getName() + ")");
            }

        }

        if (incremental == false) {
            document.add(table);
            table = null;
        }
    } catch (DocumentException e) {
        throw new ContentProcessingException("Failed to generate RTF-Document", e);
    }
}

From source file:org.sigmah.server.report.renderer.itext.ItextPivotTableRenderer.java

License:Open Source License

public void render(DocWriter writer, Document document, PivotTableElement element) {
    try {//from  w ww. j av a 2  s . c om

        document.add(ThemeHelper.elementTitle(element.getTitle()));
        ItextRendererHelper.addFilterDescription(document, element.getContent().getFilterDescriptions());

        PivotTableData data = element.getContent().getData();

        if (data.isEmpty()) {
            document.add(new Paragraph("Aucune Donnes")); // TODO: i18n

        } else {

            int colDepth = data.getRootColumn().getDepth();
            List<PivotTableData.Axis> colLeaves = data.getRootColumn().getLeaves();
            int colBreadth = colLeaves.size();

            Table table = new Table(colBreadth + 1, 1);
            table.setUseVariableBorders(true);
            table.setWidth(100.0f);
            table.setWidths(calcColumnWidths(document, data, colLeaves));
            table.setBorderWidth(0);

            // first write the column headers

            for (int depth = 1; depth <= colDepth; ++depth) {

                if (depth == 1) {
                    Cell cell = ThemeHelper.cornerCell();
                    cell.setRowspan(colDepth);
                    table.addCell(cell);
                }

                List<PivotTableData.Axis> columns = data.getRootColumn().getDescendantsAtDepth(depth);
                for (PivotTableData.Axis column : columns) {
                    Cell cell = ThemeHelper.columnHeaderCell(column.getLabel(), column.isLeaf());
                    cell.setColspan(Math.max(1, column.getChildCount()));
                    table.addCell(cell);
                }

            }
            table.endHeaders();

            for (PivotTableData.Axis row : data.getRootRow().getChildren()) {
                writeRow(table, row, colLeaves, 0);
            }

            document.add(table);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.sigmah.server.report.renderer.itext.ItextTableRenderer.java

License:Open Source License

private void renderTable(Document document, TableData data) throws DocumentException {
    int colDepth = data.getRootColumn().getDepth();
    List<TableColumn> colLeaves = data.getRootColumn().getLeaves();
    int colBreadth = colLeaves.size();

    Table table = new Table(colBreadth, 1);
    table.setUseVariableBorders(true);/*from www . ja  va 2  s.  c  o m*/
    table.setWidth(100.0f);
    table.setBorderWidth(0);

    // first write the column headers

    for (int depth = 1; depth <= colDepth; ++depth) {
        List<TableColumn> columns = data.getRootColumn().getDescendantsAtDepth(depth);
        for (TableColumn column : columns) {
            Cell cell = ThemeHelper.columnHeaderCell(column.getLabel(), column.isLeaf(), computeHAlign(column));
            cell.setColspan(Math.max(1, column.getChildren().size()));
            cell.setRowspan(colDepth - depth - column.getDepth() + 1);
            table.addCell(cell);
        }
    }
    table.endHeaders();

    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    NumberFormat numberFormat = NumberFormat.getIntegerInstance();
    numberFormat.setGroupingUsed(true);

    for (TableData.Row row : data.getRows()) {
        for (TableColumn column : colLeaves) {

            Object value = row.values[data.getColumnIndex(column)];

            String label = "";
            if (value instanceof Date) {
                label = dateFormat.format(value);
            } else if (value instanceof Number) {
                label = numberFormat.format(value);
            } else if (value != null) {
                label = value.toString();
            }

            table.addCell(ThemeHelper.bodyCell(label, false, 0, true, computeHAlign(column)));
        }
    }
    document.add(table);
}

From source file:sg.edu.nus.util.ReportWriter.java

private static void createTable(Section subCatPart) throws BadElementException {
    Table t = new Table(3, 2);

    t.setBorderColor(Color.GRAY);
    t.setPadding(4);//from  w  w w .java  2s .  co m
    t.setSpacing(4);
    t.setBorderWidth(1);

    Cell c1 = new Cell("Table Header 1");
    c1.setHeader(true);
    t.addCell(c1);
    c1 = new Cell("Table Header 2");
    t.addCell(c1);
    c1 = new Cell("Table Header 3");
    t.addCell(c1);
    t.endHeaders();

    t.addCell("1.0");
    t.addCell("1.1");
    t.addCell("1.2");
    t.addCell("2.1");
    t.addCell("2.2");
    t.addCell("2.3");

    subCatPart.add(t);

}