Example usage for com.lowagie.text Table setWidths

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

Introduction

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

Prototype

public void setWidths(int[] widths) throws DocumentException 

Source Link

Document

Sets the widths of the different columns (percentages).

Usage

From source file:org.openswing.swing.export.java.ExportToRTF14.java

License:Open Source License

private void prepareGenericComponent(Table parentTable, int parentTableCols, Document document,
        ExportOptions exportOptions, ComponentExportOptions opt) throws Throwable {
    if (opt.getCellsContent() == null || opt.getCellsContent().length == 0)
        return;//from  w  w w .  j av a  2 s . com

    int cols = opt.getCellsContent()[0].length;
    Object[] row = null;
    Object obj = null;
    SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat());
    int[] headerwidths = new int[cols];
    for (int i = 0; i < headerwidths.length; i++)
        headerwidths[i] = (int) PageSize.A4.width() / cols;

    Table table = new Table(cols);
    table.setWidths(headerwidths);
    table.setBorderWidth(2);
    table.setBorderColor(Color.black);
    table.setGrayFill(exportOptions.getExportToRTFAdapter().getHeaderGrayFill());
    table.setPadding(3);

    for (int i = 0; i < opt.getCellsContent().length; i++) {
        row = opt.getCellsContent()[i];
        for (int j = 0; j < row.length; j++) {
            obj = row[j];

            if (obj != null) {
                if (obj instanceof Date || obj instanceof java.util.Date || obj instanceof java.sql.Timestamp) {
                    table.addCell(new Phrase(sdatf.format((java.util.Date) obj),
                            (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                } else {
                    table.addCell(new Phrase(obj.toString(),
                            (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                }
            } else {
                table.addCell(new Phrase("",
                        (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, null)));
            }

        }
    }

    if (parentTable != null) {
        Cell c = new Cell(table);
        //      c.setColspan(parentTableCols);
        parentTable.addCell(c);
    } else
        document.add(table);

}

From source file:org.openswing.swing.export.java.ExportToRTF14.java

License:Open Source License

private void prepareGrid(Table parentTable, int parentTableCols, Document document, ExportOptions exportOptions,
        GridExportOptions opt) throws Throwable {
    // prepare vo getters methods...
    String methodName = null;//from w  w  w.j a v a2s.  c  o m
    String attributeName = null;
    Hashtable gettersMethods = new Hashtable();
    Method[] voMethods = opt.getValueObjectType().getMethods();
    for (int i = 0; i < voMethods.length; i++) {
        methodName = voMethods[i].getName();
        if (methodName.startsWith("get")) {
            attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
            if (opt.getExportAttrColumns().contains(attributeName))
                gettersMethods.put(attributeName, voMethods[i]);
        }
    }

    Response response = null;
    int start = 0;
    int rownum = 0;
    Object value = null;
    Object vo = null;
    int type;

    SimpleDateFormat sdf = new SimpleDateFormat(exportOptions.getDateFormat());
    SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat());
    SimpleDateFormat stf = new SimpleDateFormat(exportOptions.getTimeFormat());

    int headerwidths[] = new int[opt.getExportColumns().size()];
    int total = 0;
    for (int i = 0; i < opt.getExportColumns().size(); i++) {
        headerwidths[i] = Math.max(opt.getExportColumns().get(i).toString().length() * 10,
                ((Integer) opt.getColumnsWidth().get(opt.getExportAttrColumns().get(i))).intValue());
        total += headerwidths[i];
    }

    //    Document document = new Document();
    //    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //    RtfWriter2 w = RtfWriter2.getInstance(document,baos);
    //    document.open();

    Paragraph line = null;
    if (opt.getTitle() != null && !opt.getTitle().equals("")) {
        line = new Paragraph(opt.getTitle(), (Font) exportOptions.getExportToRTFAdapter().getFontTitle());
        line.setAlignment(Element.ALIGN_CENTER);
        document.add(line);
        document.add(new Paragraph(""));
    }
    String[] filters = opt.getFilteringConditions();
    if (filters != null) {
        for (int i = 0; i < filters.length; i++) {
            line = new Paragraph(filters[i]);
            document.add(line);
        }
        document.add(new Paragraph(""));
    }

    Table table = new Table(opt.getExportColumns().size());
    table.setWidths(headerwidths);
    table.setBorderWidth(2);
    table.setBorderColor(Color.black);
    table.setGrayFill(exportOptions.getExportToRTFAdapter().getHeaderGrayFill());
    table.setPadding(3);

    Phrase cell = null;
    for (int i = 0; i < opt.getExportColumns().size(); i++) {
        cell = new Phrase(opt.getExportColumns().get(i).toString(), (Font) exportOptions.getExportToRTFAdapter()
                .getHeaderFont(opt.getExportAttrColumns().get(i).toString()));
        table.addCell(cell);
    }
    //    table.setHeaderRows(1);
    //    table.getDefaultCell().setBorderWidth(1);
    //    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
    //    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);

    for (int j = 0; j < opt.getTopRows().size(); j++) {
        // create a row for each top rows...
        vo = opt.getTopRows().get(j);
        appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, j, 0);
    }

    do {
        response = opt.getGridDataLocator().loadData(GridParams.NEXT_BLOCK_ACTION, start,
                opt.getFilteredColumns(), opt.getCurrentSortedColumns(), opt.getCurrentSortedVersusColumns(),
                opt.getValueObjectType(), opt.getOtherGridParams());
        if (response.isError())
            throw new Exception(response.getErrorMessage());

        boolean even = false;

        for (int j = 0; j < ((VOListResponse) response).getRows().size(); j++) {
            if (even) {
                even = false;
            } else {
                even = true;
            }

            vo = ((VOListResponse) response).getRows().get(j);

            appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, rownum, 1);

            rownum++;
        }

        start = start + ((VOListResponse) response).getRows().size();

        if (!((VOListResponse) response).isMoreRows())
            break;
    } while (rownum < opt.getMaxRows());

    for (int j = 0; j < opt.getBottomRows().size(); j++) {
        // create a row for each bottom rows...
        table.setGrayFill(exportOptions.getExportToRTFAdapter().getBottomRowsGrayFill(j));
        vo = opt.getBottomRows().get(j);
        appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, j, 2);
    }

    //    rtfTable.importTable(table,total);
    if (parentTable != null) {
        Cell c = new Cell(table);
        //      c.setColspan(parentTableCols);
        parentTable.addCell(c);
        //      table.setCellsFitPage(true);
        //      parentTable.insertTable(table,new Point(parentTable.getNextRow()+1,0));

    } else
        document.add(table);

    //    document.add(table);
}

From source file:org.openswing.swing.export.java.ExportToRTF15.java

License:Open Source License

private void prepareGenericComponent(Table parentTable, int parentTableCols, Document document,
        ExportOptions exportOptions, ComponentExportOptions opt) throws Throwable {
    if (opt.getCellsContent() == null || opt.getCellsContent().length == 0)
        return;// www .  j  a  va2s  .c o m

    int cols = opt.getCellsContent()[0].length;
    Object[] row = null;
    Object obj = null;
    SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat());
    int[] headerwidths = new int[cols];
    for (int i = 0; i < headerwidths.length; i++)
        headerwidths[i] = (int) PageSize.A4.getWidth() / cols;

    Table table = new Table(cols);
    table.setWidths(headerwidths);
    table.setBorderWidth(2);
    table.setBorderColor(Color.black);
    table.setGrayFill(exportOptions.getExportToRTFAdapter().getHeaderGrayFill());
    table.setPadding(3);

    for (int i = 0; i < opt.getCellsContent().length; i++) {
        row = opt.getCellsContent()[i];
        for (int j = 0; j < row.length; j++) {
            obj = row[j];

            if (obj != null) {
                if (obj instanceof Date || obj instanceof java.util.Date || obj instanceof java.sql.Timestamp) {
                    table.addCell(new Phrase(sdatf.format((java.util.Date) obj), (Font) (Font) exportOptions
                            .getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                } else {
                    table.addCell(new Phrase(obj.toString(),
                            (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, obj)));
                }
            } else {
                table.addCell(new Phrase("",
                        (Font) exportOptions.getExportToRTFAdapter().getGenericComponentFont(i, j, null)));
            }

        }
    }

    if (parentTable != null) {
        Cell c = new Cell(table);
        //      c.setColspan(parentTableCols);
        parentTable.addCell(c);
    } else
        document.add(table);

}

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  ww w .j  a v a 2  s . c  o  m*/

        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:ro.nextreports.engine.exporter.RtfExporter.java

License:Apache License

private Table buildRtfTable(int type) throws DocumentException {
    List<Band> bands = new ArrayList<Band>();
    if (type == 1) {
        bands.add(getReportLayout().getPageHeaderBand());
    } else if (type == 2) {
        bands.add(getReportLayout().getPageFooterBand());
    } else {//from  ww w .j  a v  a2  s .  com
        bands = getReportLayout().getDocumentBands();
    }
    int totalRows = 0;
    int totalColumns = 0;
    for (Band band : bands) {
        totalRows += band.getRowCount();
        int cols = band.getColumnCount();
        if (cols > totalColumns) {
            totalColumns = cols;
        }
    }

    // no page header or no page footer
    if (totalColumns == 0) {
        return null;
    }
    Table datatable = new Table(totalColumns);
    int[] headerwidths = new int[totalColumns]; // %
    int size = 100 / totalColumns;
    int totalWidth = 0;
    for (int i = 0; i < totalColumns; i++) {
        if (bean.getReportLayout().isUseSize()) {
            headerwidths[i] = bean.getReportLayout().getColumnsWidth().get(i);
        } else {
            headerwidths[i] = size;
        }
        totalWidth += headerwidths[i];
    }

    if (bean.getReportLayout().isUseSize()) {
        float pixels = A4_PORTRAIT_PIXELS;
        if (bean.getReportLayout().getOrientation() == LANDSCAPE) {
            pixels = A4_LANDSCAPE_PIXELS;
        }
        float percentage = totalWidth * 100 / pixels;
        // do not allow to go outside an A4 frame
        if (percentage > 100) {
            percentage = 100;
        }
        if (!ReportLayout.CUSTOM.equals(bean.getReportLayout().getPageFormat())) {
            datatable.setWidth(percentage);
        }
        datatable.setWidths(headerwidths);
    } else {
        datatable.setWidth(100);
    }

    datatable.setPadding(2);
    if (type == PRINT_DOCUMENT) {
        writeHeader(datatable);
    }
    return datatable;
}

From source file:se.idega.idegaweb.commune.school.report.business.ReportPDFWriter.java

License:Open Source License

private MemoryFileBuffer getPDFBuffer() throws DocumentException {
    MemoryFileBuffer buffer = new MemoryFileBuffer();
    MemoryOutputStream mos = new MemoryOutputStream(buffer);

    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    PdfWriter writer = PdfWriter.getInstance(document, mos);

    String titleKey = this._reportModel.getReportTitleLocalizationKey();
    String title = localize(titleKey, titleKey);
    this._normalFont = new Font(Font.HELVETICA, 7, Font.NORMAL);
    this._boldFont = new Font(Font.HELVETICA, 7, Font.BOLD);

    document.addTitle(title);/*w  w  w  .ja  v  a  2  s .  co  m*/
    document.addAuthor("Agura IT Reports");
    document.addSubject(title);
    document.open();

    String dateString = new Date(System.currentTimeMillis()).toString();

    document.add(new Phrase(title + " " + dateString + "\n\n", this._boldFont));
    document.add(new Phrase("\n", this._boldFont));

    int cols = this._reportModel.getColumnSize() + 1;
    Table table = new Table(cols);
    this._widths = new int[cols];
    for (int i = 0; i < cols; i++) {
        this._widths[i] = 1;
    }

    table.setSpacing(1.5f);

    buildColumnHeaders(table);
    buildRowHeaders(table);
    buildReportCells(table);

    int totalWidth = 0;
    for (int i = 0; i < cols; i++) {
        this._widths[i] += 1;
        totalWidth += this._widths[i];
    }
    int width = (100 * totalWidth) / 95;
    if (width > 100) {
        width = 100;
    }
    table.setWidth(width);
    table.setWidths(this._widths);
    document.add(table);
    document.close();
    writer.setPdfVersion(PdfWriter.VERSION_1_2);

    return buffer;
}