Example usage for com.itextpdf.text.pdf PdfPTable setWidthPercentage

List of usage examples for com.itextpdf.text.pdf PdfPTable setWidthPercentage

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfPTable setWidthPercentage.

Prototype

public void setWidthPercentage(final float widthPercentage) 

Source Link

Document

Sets the width percentage that the table will occupy in the page.

Usage

From source file:com.coderbd.pos.pdf.OrderReportPDF.java

public PdfPTable getHeader() throws DocumentException {
    Font innerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 8, Font.NORMAL);
    Font headerFont = FontFactory.getFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 12, Font.BOLD);

    PdfPTable tableHeader = new PdfPTable(1);
    tableHeader.setWidthPercentage(90);

    Paragraph supplierNamePara = new Paragraph(sor.getSupplier().getSupplierName(), headerFont);
    Paragraph addressPara = new Paragraph(sor.getSupplier().getSupplierAddress(), innerFont);
    Paragraph mobilePara = new Paragraph("Mobile: " + sor.getSupplier().getSupplierMobile(), innerFont);
    Paragraph orderPara = new Paragraph("Order: " + sor.getSupplierOrderId() + ", Time: " + sor.getOrderTime(),
            innerFont);//from   www.j a  v  a 2  s. c om

    PdfPCell supplierNameCell = new PdfPCell();
    supplierNameCell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);
    supplierNameCell.setBorder(Rectangle.NO_BORDER);
    supplierNameCell.addElement(supplierNamePara);

    PdfPCell addressCell = new PdfPCell();
    addressCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    addressCell.setBorder(Rectangle.NO_BORDER);
    addressCell.addElement(addressPara);

    PdfPCell mobileCell = new PdfPCell(mobilePara);
    mobileCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
    mobileCell.setBorder(Rectangle.NO_BORDER);
    mobileCell.addElement(mobilePara);

    PdfPCell orderCell = new PdfPCell(orderPara);
    orderCell.setBorder(Rectangle.NO_BORDER);

    PdfPCell bCell = new PdfPCell(new Paragraph(" "));
    bCell.setBorder(Rectangle.NO_BORDER);

    tableHeader.addCell(supplierNameCell);
    tableHeader.addCell(addressCell);
    tableHeader.addCell(mobileCell);
    tableHeader.addCell(orderCell);
    tableHeader.addCell(bCell);

    return tableHeader;
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

private PdfPTable getTableDeItens(List<ItemDeVenda> itens) throws DocumentException {

    PdfPTable table = new PdfPTable(4);
    table.setWidths(new int[] { 5, 1, 1, 1 });
    table.setWidthPercentage(100f);
    table.setSpacingBefore(10f);/*w  ww  .  ja v a 2s .  c  om*/
    table.setSpacingAfter(10f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell("Produto");
    table.addCell("Quantidade");
    table.addCell("Valor UN");
    table.addCell("Total");
    table.getDefaultCell().setBackgroundColor(null);

    for (ItemDeVenda it : itens) {
        table.addCell(it.getDescricaoProduto());

        table.addCell(new DecimalFormat("0.000").format(it.getQuantidade()));
        table.addCell(new DecimalFormat("0.00").format(it.getValorProduto()));
        table.addCell(new DecimalFormat("0.00").format(it.getTotal()));
    }

    return table;

}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

private PdfPTable getTableEstoqueProdutos(boolean valoresNegativos) throws DocumentException {
    double total_requisitado = 381624.18;

    Map<String, Double[]> saida = GeradorRelatorio.getRelatorioEstoqueProdutos(valoresNegativos);

    PdfPTable table = new PdfPTable(4);
    table.setWidths(new int[] { 5, 2, 1, 1 });
    table.setWidthPercentage(100f);
    table.setSpacingBefore(10f);//from   ww  w . j a v  a 2 s .c om
    table.setSpacingAfter(10f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell("Produto");
    table.addCell("Quantidade em Estoque");
    table.addCell("Valor de Compra");
    table.addCell("SubTotal");
    table.getDefaultCell().setBackgroundColor(null);
    double quantidade = 0, total = 0;
    List<String> c = new ArrayList<String>();
    c.addAll(saida.keySet());
    Collections.sort(c);
    for (String produto : c) {

        Double[] val = saida.get(produto);

        double qt, vc, sub;
        qt = val[0];
        vc = val[1];
        sub = val[0] * val[1];

        if (qt > 0 && qt < 10000) {
            quantidade += qt;
            total += (qt * vc);
            table.addCell(produto);
            table.addCell(new DecimalFormat("0.000").format(qt));
            table.addCell(new DecimalFormat("0.00").format(vc));
            table.addCell(new DecimalFormat("0.00").format(qt * vc));
        } else {
            continue;
            //table.addCell(new DecimalFormat("0.00").format(val[1]));
            //table.addCell(new DecimalFormat("0.000").format(0));
            //table.addCell(new DecimalFormat("0.00").format(vc));
            //table.addCell(new DecimalFormat("0.00").format(0));
        }
        if (total_requisitado - 10 <= total && total <= total_requisitado + 10) {
            break;
        }

    }

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));

    PdfPCell cell = new PdfPCell(new Paragraph("Total"));
    cell.setColspan(2);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell(cell);

    //table.addCell(OperacaoStringUtil.formatarStringValorIntegerEPonto(
    //       saida.size()) + " Produtos");

    //table.addCell(OperacaoStringUtil.formatarStringQuantidadeEPonto(quantidade)
    //        + " Itens");

    String tot = OperacaoStringUtil.formatarStringValorMoedaComDescricaoEPonto(381624.18);
    PdfPCell cel2 = new PdfPCell(new Paragraph(tot));
    cel2.setColspan(2);
    cel2.setHorizontalAlignment(Element.ALIGN_CENTER);
    cel2.setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell(cel2);

    //table.addCell(OperacaoStringUtil.formatarStringValorMoedaComDescricaoEPonto(total));
    //System.out.println(total);
    return table;
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

private PdfPTable getTableEstoqueProdutos(boolean valoresNegativos, boolean paraLista,
        List<String> codigos_retirados, double total_requisitado) throws DocumentException {
    //double total_requisitado = 381624.18;

    Map<String, Double[]> saida = GeradorRelatorio.getRelatorioEstoqueProdutos(valoresNegativos,
            codigos_retirados);//from   ww w  . j a  v  a  2s  .  c om

    PdfPTable table = new PdfPTable(4);
    table.setWidths(new int[] { 5, 2, 1, 1 });
    table.setWidthPercentage(100f);
    table.setSpacingBefore(10f);
    table.setSpacingAfter(10f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell("Produto");
    table.addCell("Quantidade em Estoque");
    table.addCell("Valor de Compra");
    table.addCell("SubTotal");
    table.getDefaultCell().setBackgroundColor(null);
    double quantidade = 0, total = 0;
    List<String> c = new ArrayList<String>();
    c.addAll(saida.keySet());
    Collections.sort(c);
    for (String produto : c) {
        if (paraLista && total_requisitado <= total) {
            break;
        }

        Double[] val = saida.get(produto);

        double qt, vc, sub;
        qt = val[0];
        vc = val[1];
        sub = val[0] * val[1];

        if (qt > 0 && qt < 10000) {
            quantidade += qt;
            total += (qt * vc);
            table.addCell(produto);
            table.addCell(new DecimalFormat("0.000").format(qt));
            table.addCell(new DecimalFormat("0.00").format(vc));
            table.addCell(new DecimalFormat("0.00").format(qt * vc));
        } else {
            continue;
            //table.addCell(new DecimalFormat("0.00").format(val[1]));
            //table.addCell(new DecimalFormat("0.000").format(0));
            //table.addCell(new DecimalFormat("0.00").format(vc));
            //table.addCell(new DecimalFormat("0.00").format(0));
        }

    }

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));

    PdfPCell cell = new PdfPCell(new Paragraph("Total"));
    cell.setColspan(2);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell(cell);

    //table.addCell(OperacaoStringUtil.formatarStringValorIntegerEPonto(
    //       saida.size()) + " Produtos");

    //table.addCell(OperacaoStringUtil.formatarStringQuantidadeEPonto(quantidade)
    //        + " Itens");

    String tot = OperacaoStringUtil.formatarStringValorMoedaComDescricaoEPonto(total_requisitado);
    PdfPCell cel2 = new PdfPCell(new Paragraph(tot));
    cel2.setColspan(2);
    cel2.setHorizontalAlignment(Element.ALIGN_CENTER);
    cel2.setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell(cel2);

    //table.addCell(OperacaoStringUtil.formatarStringValorMoedaComDescricaoEPonto(total));
    //System.out.println(total);
    return table;
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

private PdfPTable getTableBalancoProdutos(Date inicio, Date fim) throws DocumentException {
    Map<String, Double[]> saida = GeradorRelatorio.getRelatorioDetalhadoSaidaProduto(inicio, fim);

    PdfPTable table = new PdfPTable(5);
    table.setWidths(new int[] { 5, 1, 1, 1, 1 });
    table.setWidthPercentage(100f);
    table.setSpacingBefore(10f);/*from  www  .j  a va2s . c o  m*/
    table.setSpacingAfter(10f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell("Produto");
    table.addCell("Estoque");
    table.addCell("Compra");
    table.addCell("Venda");
    table.addCell("Lucro");
    table.getDefaultCell().setBackgroundColor(null);
    double compra = 0, venda = 0, lucro = 0, qt = 0;
    List<String> c = new ArrayList<String>();
    c.addAll(saida.keySet());
    Collections.sort(c);
    for (String produto : c) {
        table.addCell(produto);
        Double[] val = saida.get(produto);
        compra += val[0];
        venda += val[1];
        lucro += val[3];
        qt += val[2];
        table.addCell(new DecimalFormat("0.000").format(val[2]));
        table.addCell(new DecimalFormat("0.00").format(val[0]));
        table.addCell(new DecimalFormat("0.00").format(val[1]));
        table.addCell(new DecimalFormat("0.00").format(val[3]));
    }

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));

    PdfPCell cell = new PdfPCell(new Paragraph("Total"));
    cell.setColspan(5);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell(cell);

    table.addCell(saida.size() + " Produtos");

    table.addCell(new DecimalFormat("0.000").format(qt));
    table.addCell(new DecimalFormat("0.00").format(compra));
    table.addCell(new DecimalFormat("0.00").format(venda));
    table.addCell(new DecimalFormat("0.00").format(lucro));

    return table;
}

From source file:com.cs.sis.controller.gerador.GeradorPDF.java

private PdfPTable getTableDebitoClientes(double maiorQue) throws DocumentException {
    List<Object[]> debitos = GeradorRelatorio.getRelatorioClientesComDebitoMaiorQue(maiorQue);

    PdfPTable table = new PdfPTable(2);
    table.setWidths(new int[] { 5, 2 });
    table.setWidthPercentage(100f);
    table.setSpacingBefore(10f);//w w w. j  a v a 2s . c  om
    table.setSpacingAfter(10f);

    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell("Cliente");
    table.addCell("Dbito");
    table.getDefaultCell().setBackgroundColor(null);
    double total = 0;
    for (Object[] d : debitos) {
        table.addCell((String) d[0]);
        double val = (double) d[1];
        table.addCell(OperacaoStringUtil.formatarStringValorMoedaEPonto(val));
        total += val;
    }

    table.getDefaultCell().setBackgroundColor(new BaseColor(60, 171, 198));

    PdfPCell cell = new PdfPCell(new Paragraph("Total"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBackgroundColor(new BaseColor(60, 171, 198));
    table.addCell(cell);

    table.addCell(OperacaoStringUtil.formatarStringValorMoedaComDescricaoEPonto(total));

    table.addCell("Quantidade de Clientes");
    table.addCell(OperacaoStringUtil.formatarStringValorInteger(debitos.size()));

    return table;
}

From source file:com.dandymadeproductions.ajqvue.io.PDFDataTableDumpThread.java

License:Open Source License

public void run() {
    // Class Method Instances
    String title;//from   w ww  . ja v  a  2 s  .c om

    Font titleFont;
    Font rowHeaderFont;
    Font tableDataFont;
    BaseFont rowHeaderBaseFont;

    PdfPTable pdfTable;
    PdfPCell titleCell;
    PdfPCell rowHeaderCell;
    PdfPCell bodyCell;

    Document pdfDocument;
    PdfWriter pdfWriter;
    ByteArrayOutputStream byteArrayOutputStream;

    int columnCount, rowNumber;
    int[] columnWidths;
    int totalWidth;
    Rectangle pageSize;

    ProgressBar dumpProgressBar;
    HashMap<String, String> summaryListTableNameTypes;
    DataExportProperties pdfDataExportOptions;

    String currentTableFieldName;
    String currentType, currentString;

    // Setup
    columnCount = summaryListTable.getColumnCount();
    rowNumber = summaryListTable.getRowCount();
    columnWidths = new int[columnCount];

    pdfTable = new PdfPTable(columnCount);
    pdfTable.setWidthPercentage(100);
    pdfTable.getDefaultCell().setPaddingBottom(4);
    pdfTable.getDefaultCell().setBorderWidth(1);

    summaryListTableNameTypes = new HashMap<String, String>();
    pdfDataExportOptions = DBTablesPanel.getDataExportProperties();

    titleFont = new Font(pdfDataExportOptions.getFont());
    titleFont.setStyle(Font.BOLD);
    titleFont.setSize((float) pdfDataExportOptions.getTitleFontSize());
    titleFont.setColor(new BaseColor(pdfDataExportOptions.getTitleColor().getRGB()));

    rowHeaderFont = new Font(pdfDataExportOptions.getFont());
    rowHeaderFont.setStyle(Font.BOLD);
    rowHeaderFont.setSize((float) pdfDataExportOptions.getHeaderFontSize());
    rowHeaderFont.setColor(new BaseColor(pdfDataExportOptions.getHeaderColor().getRGB()));
    rowHeaderBaseFont = rowHeaderFont.getCalculatedBaseFont(false);

    tableDataFont = pdfDataExportOptions.getFont();

    // Constructing progress bar.
    dumpProgressBar = new ProgressBar(exportedTable + " Dump");
    dumpProgressBar.setTaskLength(rowNumber);
    dumpProgressBar.pack();
    dumpProgressBar.center();
    dumpProgressBar.setVisible(true);

    // Create a Title if Optioned.
    title = pdfDataExportOptions.getTitle();

    if (!title.equals("")) {
        if (title.equals("EXPORTED TABLE"))
            title = exportedTable;

        titleCell = new PdfPCell(new Phrase(title, titleFont));
        titleCell.setBorder(0);
        titleCell.setPadding(10);
        titleCell.setColspan(summaryListTable.getColumnCount());
        titleCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);

        pdfTable.addCell(titleCell);
        pdfTable.setHeaderRows(2);
    } else
        pdfTable.setHeaderRows(1);

    // Create Row Header.
    for (int i = 0; i < columnCount; i++) {
        currentTableFieldName = summaryListTable.getColumnName(i);
        rowHeaderCell = new PdfPCell(new Phrase(currentTableFieldName, rowHeaderFont));
        rowHeaderCell.setBorderWidth(pdfDataExportOptions.getHeaderBorderSize());
        rowHeaderCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        rowHeaderCell.setBorderColor(new BaseColor(pdfDataExportOptions.getHeaderBorderColor().getRGB()));
        pdfTable.addCell(rowHeaderCell);
        columnWidths[i] = Math.min(50000,
                Math.max(columnWidths[i], rowHeaderBaseFont.getWidth(currentTableFieldName + " ")));
        if (tableColumnTypeNameHashMap != null)
            summaryListTableNameTypes.put(Integer.toString(i),
                    tableColumnTypeNameHashMap.get(currentTableFieldName));
        else
            summaryListTableNameTypes.put(Integer.toString(i), "String");
    }

    // Create the Body of Data.
    int i = 0;
    while ((i < rowNumber) && !dumpProgressBar.isCanceled()) {
        dumpProgressBar.setCurrentValue(i);

        // Collecting rows of data & formatting date & timestamps
        // as needed according to the Export Properties.

        if (summaryListTable.getValueAt(i, 0) != null) {
            for (int j = 0; j < summaryListTable.getColumnCount(); j++) {
                currentString = summaryListTable.getValueAt(i, j) + "";
                currentString = currentString.replaceAll("\n", "");
                currentString = currentString.replaceAll("\r", "");
                currentType = summaryListTableNameTypes.get(Integer.toString(j));

                // Format Date & Timestamp Fields as Needed.

                if ((currentType != null) && (currentType.equals("DATE") || currentType.equals("DATETIME")
                        || currentType.indexOf("TIMESTAMP") != -1)) {
                    if (!currentString.toLowerCase(Locale.ENGLISH).equals("null")) {
                        int firstSpace;
                        String time;

                        // Dates fall through DateTime and Timestamps try
                        // to get the time separated before formatting
                        // the date.

                        if (currentString.indexOf(" ") != -1) {
                            firstSpace = currentString.indexOf(" ");
                            time = currentString.substring(firstSpace);
                            currentString = currentString.substring(0, firstSpace);
                        } else
                            time = "";

                        currentString = Utils.convertViewDateString_To_DBDateString(currentString,
                                DBTablesPanel.getGeneralDBProperties().getViewDateFormat());
                        currentString = Utils.convertDBDateString_To_ViewDateString(currentString,
                                pdfDataExportOptions.getPDFDateFormat()) + time;
                    }
                }
                bodyCell = new PdfPCell(new Phrase(currentString, tableDataFont));
                bodyCell.setPaddingBottom(4);

                if (currentType != null) {
                    // Set Numeric Fields Alignment.
                    if (currentType.indexOf("BIT") != -1 || currentType.indexOf("BOOL") != -1
                            || currentType.indexOf("NUM") != -1 || currentType.indexOf("INT") != -1
                            || currentType.indexOf("FLOAT") != -1 || currentType.indexOf("DOUBLE") != -1
                            || currentType.equals("REAL") || currentType.equals("DECIMAL")
                            || currentType.indexOf("COUNTER") != -1 || currentType.equals("BYTE")
                            || currentType.equals("CURRENCY")) {
                        bodyCell.setHorizontalAlignment(pdfDataExportOptions.getNumberAlignment());
                        bodyCell.setPaddingRight(4);
                    }
                    // Set Date/Time Field Alignment.
                    if (currentType.indexOf("DATE") != -1 || currentType.indexOf("TIME") != -1
                            || currentType.indexOf("YEAR") != -1)
                        bodyCell.setHorizontalAlignment(pdfDataExportOptions.getDateAlignment());
                }

                pdfTable.addCell(bodyCell);
                columnWidths[j] = Math.min(50000,
                        Math.max(columnWidths[j], BASE_FONT.getWidth(currentString + " ")));
            }
        }
        i++;
    }
    dumpProgressBar.dispose();

    // Check to see if any data was in the summary
    // table to even be saved.

    if (pdfTable.size() <= pdfTable.getHeaderRows())
        return;

    // Create a document of the PDF formatted data
    // to be saved to the given output file.

    try {
        // Sizing & Layout
        totalWidth = 0;
        for (int width : columnWidths)
            totalWidth += width;

        if (pdfDataExportOptions.getPageLayout() == PDFExportPreferencesPanel.LAYOUT_PORTRAIT)
            pageSize = PageSize.A4;
        else {
            pageSize = PageSize.A4.rotate();
            pageSize.setRight(pageSize.getRight() * Math.max(1f, totalWidth / 53000f));
            pageSize.setTop(pageSize.getTop() * Math.max(1f, totalWidth / 53000f));
        }

        pdfTable.setWidths(columnWidths);

        // Document
        pdfDocument = new Document(pageSize);
        byteArrayOutputStream = new ByteArrayOutputStream();
        pdfWriter = PdfWriter.getInstance(pdfDocument, byteArrayOutputStream);
        pdfDocument.open();
        pdfTemplate = pdfWriter.getDirectContent().createTemplate(100, 100);
        pdfTemplate.setBoundingBox(new com.itextpdf.text.Rectangle(-20, -20, 100, 100));
        pdfWriter.setPageEvent(this);
        pdfDocument.add(pdfTable);
        pdfDocument.close();

        // Outputting
        WriteDataFile.mainWriteDataString(fileName, byteArrayOutputStream.toByteArray(), false);

    } catch (DocumentException de) {
        if (Ajqvue.getDebug()) {
            System.out.println("Failed to Create Document Needed to Output Data. \n" + de.toString());
        }
    }
}

From source file:com.dandymadeproductions.myjsqlview.io.PDFDataTableDumpThread.java

License:Open Source License

public void run() {
    // Class Method Instances
    String title;/*from  w  ww .  j a va  2 s .  c  o m*/
    PdfPTable pdfTable;
    PdfPCell titleCell, rowHeaderCell, bodyCell;
    Document pdfDocument;
    PdfWriter pdfWriter;
    ByteArrayOutputStream byteArrayOutputStream;

    int columnCount, rowNumber;
    int[] columnWidths;
    int totalWidth;
    Rectangle pageSize;

    MyJSQLView_ProgressBar dumpProgressBar;
    HashMap<String, String> summaryListTableNameTypes;
    String currentTableFieldName;
    String currentType, currentString;

    // Setup
    columnCount = summaryListTable.getColumnCount();
    rowNumber = summaryListTable.getRowCount();
    columnWidths = new int[columnCount];

    pdfTable = new PdfPTable(columnCount);
    pdfTable.setWidthPercentage(100);
    pdfTable.getDefaultCell().setPaddingBottom(4);
    pdfTable.getDefaultCell().setBorderWidth(1);

    summaryListTableNameTypes = new HashMap<String, String>();
    pdfDataExportOptions = DBTablesPanel.getDataExportProperties();

    titleFont = new Font(pdfDataExportOptions.getFont());
    titleFont.setStyle(Font.BOLD);
    titleFont.setSize((float) pdfDataExportOptions.getTitleFontSize());
    titleFont.setColor(new BaseColor(pdfDataExportOptions.getTitleColor().getRGB()));

    rowHeaderFont = new Font(pdfDataExportOptions.getFont());
    rowHeaderFont.setStyle(Font.BOLD);
    rowHeaderFont.setSize((float) pdfDataExportOptions.getHeaderFontSize());
    rowHeaderFont.setColor(new BaseColor(pdfDataExportOptions.getHeaderColor().getRGB()));
    rowHeaderBaseFont = rowHeaderFont.getCalculatedBaseFont(false);

    tableDataFont = pdfDataExportOptions.getFont();

    // Constructing progress bar.
    rowNumber = summaryListTable.getRowCount();
    dumpProgressBar = new MyJSQLView_ProgressBar(exportedTable + " Dump");
    dumpProgressBar.setTaskLength(rowNumber);
    dumpProgressBar.pack();
    dumpProgressBar.center();
    dumpProgressBar.setVisible(true);

    // Create a Title if Optioned.
    title = pdfDataExportOptions.getTitle();

    if (!title.equals("")) {
        if (title.equals("EXPORTED TABLE"))
            title = exportedTable;

        titleCell = new PdfPCell(new Phrase(title, titleFont));
        titleCell.setBorder(0);
        titleCell.setPadding(10);
        titleCell.setColspan(summaryListTable.getColumnCount());
        titleCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);

        pdfTable.addCell(titleCell);
        pdfTable.setHeaderRows(2);
    } else
        pdfTable.setHeaderRows(1);

    // Create Row Header.
    for (int i = 0; i < columnCount; i++) {
        currentTableFieldName = summaryListTable.getColumnName(i);
        rowHeaderCell = new PdfPCell(new Phrase(currentTableFieldName, rowHeaderFont));
        rowHeaderCell.setBorderWidth(pdfDataExportOptions.getHeaderBorderSize());
        rowHeaderCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        rowHeaderCell.setBorderColor(new BaseColor(pdfDataExportOptions.getHeaderBorderColor().getRGB()));
        pdfTable.addCell(rowHeaderCell);
        columnWidths[i] = Math.min(50000,
                Math.max(columnWidths[i], rowHeaderBaseFont.getWidth(currentTableFieldName + " ")));
        if (tableColumnTypeHashMap != null)
            summaryListTableNameTypes.put(Integer.toString(i),
                    tableColumnTypeHashMap.get(currentTableFieldName));
        else
            summaryListTableNameTypes.put(Integer.toString(i), "String");
    }

    // Create the Body of Data.
    int i = 0;
    while ((i < rowNumber) && !dumpProgressBar.isCanceled()) {
        dumpProgressBar.setCurrentValue(i);

        // Collecting rows of data & formatting date & timestamps
        // as needed according to the Export Properties.

        if (summaryListTable.getValueAt(i, 0) != null) {
            for (int j = 0; j < summaryListTable.getColumnCount(); j++) {
                currentString = summaryListTable.getValueAt(i, j) + "";
                currentString = currentString.replaceAll("\n", "");
                currentString = currentString.replaceAll("\r", "");
                currentType = summaryListTableNameTypes.get(Integer.toString(j));

                // Format Date & Timestamp Fields as Needed.

                if ((currentType != null) && (currentType.equals("DATE") || currentType.equals("DATETIME")
                        || currentType.indexOf("TIMESTAMP") != -1)) {
                    if (!currentString.toLowerCase().equals("null")) {
                        int firstSpace;
                        String time;

                        // Dates fall through DateTime and Timestamps try
                        // to get the time separated before formatting
                        // the date.

                        if (currentString.indexOf(" ") != -1) {
                            firstSpace = currentString.indexOf(" ");
                            time = currentString.substring(firstSpace);
                            currentString = currentString.substring(0, firstSpace);
                        } else
                            time = "";

                        currentString = MyJSQLView_Utils.convertViewDateString_To_DBDateString(currentString,
                                DBTablesPanel.getGeneralDBProperties().getViewDateFormat());
                        currentString = MyJSQLView_Utils.convertDBDateString_To_ViewDateString(currentString,
                                pdfDataExportOptions.getPDFDateFormat()) + time;
                    }
                }
                bodyCell = new PdfPCell(new Phrase(currentString, tableDataFont));
                bodyCell.setPaddingBottom(4);

                if (currentType != null) {
                    // Set Numeric Fields Alignment.
                    if (currentType.indexOf("BIT") != -1 || currentType.indexOf("BOOL") != -1
                            || currentType.indexOf("NUM") != -1 || currentType.indexOf("INT") != -1
                            || currentType.indexOf("FLOAT") != -1 || currentType.indexOf("DOUBLE") != -1
                            || currentType.equals("REAL") || currentType.equals("DECIMAL")
                            || currentType.indexOf("COUNTER") != -1 || currentType.equals("BYTE")
                            || currentType.equals("CURRENCY")) {
                        bodyCell.setHorizontalAlignment(pdfDataExportOptions.getNumberAlignment());
                        bodyCell.setPaddingRight(4);
                    }
                    // Set Date/Time Field Alignment.
                    if (currentType.indexOf("DATE") != -1 || currentType.indexOf("TIME") != -1
                            || currentType.indexOf("YEAR") != -1)
                        bodyCell.setHorizontalAlignment(pdfDataExportOptions.getDateAlignment());
                }

                pdfTable.addCell(bodyCell);
                columnWidths[j] = Math.min(50000,
                        Math.max(columnWidths[j], BASE_FONT.getWidth(currentString + " ")));
            }
        }
        i++;
    }
    dumpProgressBar.dispose();

    // Check to see if any data was in the summary
    // table to even be saved.

    if (pdfTable.size() <= pdfTable.getHeaderRows())
        return;

    // Create a document of the PDF formatted data
    // to be saved to the given output file.

    try {
        // Sizing & Layout
        totalWidth = 0;
        for (int width : columnWidths)
            totalWidth += width;

        if (pdfDataExportOptions.getPageLayout() == PDFExportPreferencesPanel.LAYOUT_PORTRAIT)
            pageSize = PageSize.A4;
        else {
            pageSize = PageSize.A4.rotate();
            pageSize.setRight(pageSize.getRight() * Math.max(1f, totalWidth / 53000f));
            pageSize.setTop(pageSize.getTop() * Math.max(1f, totalWidth / 53000f));
        }

        pdfTable.setWidths(columnWidths);

        // Document
        pdfDocument = new Document(pageSize);
        byteArrayOutputStream = new ByteArrayOutputStream();
        pdfWriter = PdfWriter.getInstance(pdfDocument, byteArrayOutputStream);
        pdfDocument.open();
        pdfTemplate = pdfWriter.getDirectContent().createTemplate(100, 100);
        pdfTemplate.setBoundingBox(new com.itextpdf.text.Rectangle(-20, -20, 100, 100));
        pdfWriter.setPageEvent(this);
        pdfDocument.add(pdfTable);
        pdfDocument.close();

        // Outputting
        WriteDataFile.mainWriteDataString(fileName, byteArrayOutputStream.toByteArray(), false);

    } catch (DocumentException de) {
        if (MyJSQLView.getDebug()) {
            System.out.println("Failed to Create Document Needed to Output Data. \n" + de.toString());
        }
    }
}

From source file:com.debashubham.dumpy.ChainageCalcActivity.java

private void createTable(Section subCatPart) throws DocumentException {
    PdfPTable entry_table = new PdfPTable(10);
    entry_table.setWidthPercentage(100);
    //        entry_table.setWidths(new float[]{0.25f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,0.5f,1.0f});
    PdfPCell id = new PdfPCell(new Phrase("Station Point"));
    id.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell left_row = new PdfPCell(new Phrase("CH. Left(m)"));
    left_row.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell center_row = new PdfPCell(new Phrase("CH. Center(m)"));
    center_row.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell right_row = new PdfPCell(new Phrase("CH. Right(m)"));
    right_row.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell bs = new PdfPCell(new Phrase("BS(m)"));
    bs.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell is = new PdfPCell(new Phrase("IS(m)"));
    is.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell fs = new PdfPCell(new Phrase("FS(m)"));
    fs.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell hi = new PdfPCell(new Phrase("HI(m)"));
    hi.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell rl = new PdfPCell(new Phrase("RL(m)"));
    rl.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell remarks = new PdfPCell(new Phrase("REMARKS"));
    remarks.setHorizontalAlignment(Element.ALIGN_CENTER);
    entry_table.addCell(id);/*from   w w  w .  jav a 2  s  .c  o m*/
    entry_table.addCell(left_row);
    entry_table.addCell(center_row);
    entry_table.addCell(right_row);
    entry_table.addCell(bs);
    entry_table.addCell(is);
    entry_table.addCell(fs);
    entry_table.addCell(hi);
    entry_table.addCell(rl);
    entry_table.addCell(remarks);
    entry_table.setHeaderRows(1);
    int largest = rl_list.size();

    Log.e("Total RL:", String.valueOf(largest));
    double total_bs = 0, total_fs = 0, first_rl = 0, last_rl = 0;

    for (int i = 1; i <= largest; i++) {

        entry_table.addCell(String.valueOf(i));

        if (i == 1)
            first_rl = rl_list.get(i);
        if (i == largest)
            last_rl = rl_list.get(i);

        if (left.containsKey(i)) {
            entry_table.addCell(String.valueOf(left.get(i)));
            entry_table.addCell("");
            entry_table.addCell("");
        } else if (center.containsKey(i)) {
            entry_table.addCell("");
            entry_table.addCell(String.valueOf(center.get(i)));
            entry_table.addCell("");
        } else if (right.containsKey(i)) {
            entry_table.addCell("");
            entry_table.addCell("");
            entry_table.addCell(String.valueOf(right.get(i)));
        } else {
            entry_table.addCell("");
            entry_table.addCell("");
            entry_table.addCell("");
        }

        if (bs_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(bs_list.get(i)));
            total_bs += bs_list.get(i);
            Log.e("TABLE:", "BS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "BS not entered");
        }
        if (is_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(is_list.get(i)));
            Log.e("TABLE:", "IS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "IS not entered");
        }
        if (fs_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(fs_list.get(i)));
            total_fs += fs_list.get(i);
            Log.e("TABLE:", "FS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "FS not entered");
        }
        if (hi_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(hi_list.get(i)));
            Log.e("TABLE:", "HI");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "HI not entered");
        }
        if (rl_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(rl_list.get(i)));
            Log.e("TABLE:", "RL");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "RL not entered");
        }
        if (remarks_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(remarks_list.get(i)));
            Log.e("TABLE:", "REMARKS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "REMARKS not entered");
        }
    }
    subCatPart.add(entry_table);
    Log.e("Total BS:", String.valueOf(total_bs));
    Log.e("Total FS:", String.valueOf(total_fs));
    Log.e("First RL:", String.valueOf(first_rl));
    Log.e("Last RL:", String.valueOf(last_rl));
    Paragraph check_para = new Paragraph("Arithmetic Check:", subFont);
    addEmptyLine(check_para, 2);
    check_para.add("Summation(BS) - Summation(FS) = Last RL - First RL");
    addEmptyLine(check_para, 1);
    double bsfs_diff = Math.round((total_bs - total_fs) * 1000.0) / 1000.0;
    double rl_diff = Math.round((last_rl - first_rl) * 1000.0) / 1000.0;
    boolean checktrue = (bsfs_diff) == (rl_diff) ? true : false;
    check_para.add(String.valueOf(bsfs_diff) + " = " + String.valueOf(rl_diff));
    addEmptyLine(check_para, 1);
    if (checktrue) {
        check_para.setSpacingBefore(20);
        check_para.add("OK");
    } else {
        check_para.setSpacingBefore(20);
        check_para.add("Check Fail!");
    }
    addEmptyLine(check_para, 1);
    check_para.add("_______________________________________________________");
    subCatPart.add(check_para);
    Paragraph legend_para = new Paragraph("LEGEND:", subFont);
    addEmptyLine(legend_para, 1);
    legend_para.add("BS : Back Sight");
    addEmptyLine(legend_para, 1);
    legend_para.add("IS : Intermediate Sight");
    addEmptyLine(legend_para, 1);
    legend_para.add("FS : Fore Sight");
    addEmptyLine(legend_para, 1);
    legend_para.add("HI : Height of Instrument");
    addEmptyLine(legend_para, 1);
    legend_para.add("RL : Reduced Level");
    addEmptyLine(legend_para, 1);
    legend_para.add("CH. : Chainage");
    subCatPart.add(legend_para);
}

From source file:com.debashubham.dumpy.SimpleCalcActivity.java

private void createTable(Section subCatPart) throws BadElementException {
    PdfPTable entry_table = new PdfPTable(7);
    entry_table.setWidthPercentage(100);
    PdfPCell id = new PdfPCell(new Phrase("Station Point"));
    id.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell bs = new PdfPCell(new Phrase("BS"));
    bs.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell is = new PdfPCell(new Phrase("IS"));
    is.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell fs = new PdfPCell(new Phrase("FS"));
    fs.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell hi = new PdfPCell(new Phrase("HI"));
    hi.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell rl = new PdfPCell(new Phrase("RL"));
    rl.setHorizontalAlignment(Element.ALIGN_CENTER);
    PdfPCell remarks = new PdfPCell(new Phrase("REMARKS"));
    remarks.setHorizontalAlignment(Element.ALIGN_CENTER);
    entry_table.addCell(id);//  ww  w. j  av a2 s.  c  om
    entry_table.addCell(bs);
    entry_table.addCell(is);
    entry_table.addCell(fs);
    entry_table.addCell(hi);
    entry_table.addCell(rl);
    entry_table.addCell(remarks);
    entry_table.setHeaderRows(1);
    int largest = rl_list.size();

    Log.e("Total RL:", String.valueOf(largest));
    double total_bs = 0, total_fs = 0, first_rl = 0, last_rl = 0;

    for (int i = 1; i <= largest; i++) {

        entry_table.addCell(String.valueOf(i));

        if (i == 1)
            first_rl = rl_list.get(i);
        if (i == largest)
            last_rl = rl_list.get(i);

        if (bs_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(bs_list.get(i)));
            total_bs += bs_list.get(i);
            Log.e("TABLE:", "BS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "BS not entered");
        }
        if (is_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(is_list.get(i)));
            Log.e("TABLE:", "IS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "IS not entered");
        }
        if (fs_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(fs_list.get(i)));
            total_fs += fs_list.get(i);
            Log.e("TABLE:", "FS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "FS not entered");
        }
        if (hi_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(hi_list.get(i)));
            Log.e("TABLE:", "HI");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "HI not entered");
        }
        if (rl_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(rl_list.get(i)));
            Log.e("TABLE:", "RL");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "RL not entered");
        }
        if (remarks_list.containsKey(i)) {
            entry_table.addCell(String.valueOf(remarks_list.get(i)));
            Log.e("TABLE:", "REMARKS");
        } else {
            entry_table.addCell("");
            Log.e("TABLE:", "REMARKS not entered");
        }
    }
    subCatPart.add(entry_table);
    Log.e("Total BS:", String.valueOf(total_bs));
    Log.e("Total FS:", String.valueOf(total_fs));
    Log.e("First RL:", String.valueOf(first_rl));
    Log.e("Last RL:", String.valueOf(last_rl));
    Paragraph check_para = new Paragraph("Arithmetic Check:", subFont);
    addEmptyLine(check_para, 2);
    check_para.add("Summation(BS) - Summation(FS) = Last RL - First RL");
    addEmptyLine(check_para, 1);
    double bsfs_diff = Math.round((total_bs - total_fs) * 1000.0) / 1000.0;
    double rl_diff = Math.round((last_rl - first_rl) * 1000.0) / 1000.0;
    boolean checktrue = (bsfs_diff) == (rl_diff) ? true : false;
    check_para.add(String.valueOf(bsfs_diff) + " = " + String.valueOf(rl_diff));
    addEmptyLine(check_para, 1);
    if (checktrue) {
        check_para.setSpacingBefore(20);
        check_para.add("OK");
    } else {
        check_para.setSpacingBefore(20);
        check_para.add("Check Fail!");
    }
    addEmptyLine(check_para, 1);
    check_para.add("_______________________________________________________");
    subCatPart.add(check_para);
    Paragraph legend_para = new Paragraph("LEGEND:", subFont);
    addEmptyLine(legend_para, 2);
    legend_para.add("BS : Back Sight");
    addEmptyLine(legend_para, 1);
    legend_para.add("IS : Intermediate Sight");
    addEmptyLine(legend_para, 1);
    legend_para.add("FS : Fore Sight");
    addEmptyLine(legend_para, 1);
    legend_para.add("HI : Height of Instrument");
    addEmptyLine(legend_para, 1);
    legend_para.add("RL : Reduced Level");
    subCatPart.add(legend_para);
}