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

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

Introduction

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

Prototype

public void setWidthPercentage(float columnWidth[], Rectangle pageSize) throws DocumentException 

Source Link

Document

Sets the percentage width of the table from the absolute column width.

Usage

From source file:com.krawler.esp.servlets.ExportMPXServlet.java

License:Open Source License

public static PdfPTable createPdfTable(ArrayList header, ArrayList widths, ArrayList tabCol, Document doc)
        throws DocumentException {
    PdfPTable temp = new PdfPTable(header.size());
    float[] wid = new float[widths.size()];
    String[] cols = new String[header.size()];
    for (int i = 0; i < widths.size(); i++) {
        wid[i] = (float) (Integer.parseInt(widths.get(i).toString()));
        cols[i] = header.get(i).toString();
    }/*  ww  w.j a v  a 2s  . co m*/
    temp.setWidthPercentage(wid, doc.getPageSize());
    temp.setTotalWidth(100);
    Font fnt1 = new Font();
    fnt1.setStyle(Font.NORMAL);
    addPdfRowToTable(cols, temp, fnt1, 0);
    temp.setHeaderRows(1);
    tabCol.add(cols);
    return temp;
}

From source file:com.krawler.esp.servlets.ExportProjectReportServlet.java

License:Open Source License

private int addTable(int stcol, int stpcol, int strow, int stprow, JSONArray store, String[] colwidth2,
        String[] colHeader, Document document) throws JSONException, DocumentException {

    java.awt.Color tColor = new Color(Integer.parseInt(config.getString("textColor"), 16));
    fontSmallBold.setColor(tColor);/*w w  w.j a  v  a2s.c om*/

    com.krawler.utils.json.base.JSONObject colWidth = config.getJSONObject("colWidth");
    JSONArray widths = colWidth.getJSONArray("data");
    ArrayList arr = new ArrayList();
    PdfPTable table;
    float[] f = new float[widths.length() + 1];//[(stpcol - stcol) + 1];
    float[] tcol = new float[(stpcol - stcol) + 1];

    if (widths.length() == 0) {
        f[0] = 10;
        for (int k = 1; k < f.length; k++) {
            f[k] = 20;
        }
        tcol[0] = 10;
    } else {
        for (int i = 0; i < widths.length(); i++) {
            JSONObject temp = widths.getJSONObject(i);
            arr.add(temp.getInt("width"));
        }

        f[0] = 30;
        for (int k = 1; k < f.length; k++) {
            if (!config.getBoolean("landscape") && (Integer) arr.get(k - 1) > 550) {
                f[k] = 550;
            } else {
                f[k] = (Integer) arr.get(k - 1);
            }
        }
        //            table = new PdfPTable(f);
        //            table.setTotalWidth(90);
        // table.setWidthPercentage(f,document.getPageSize());
        tcol[0] = 30;
    }
    int i = 1;
    for (int k = stcol; k < stpcol; k++) {
        tcol[i] = f[k + 1];
        i++;
    }

    table = new PdfPTable(tcol);
    table.setTotalWidth(90);
    table.setWidthPercentage(tcol, document.getPageSize());

    table.setSpacingBefore(15);
    Font f1 = FontFactory.getFont("Helvetica", 8, Font.NORMAL, tColor);
    PdfPCell h2 = new PdfPCell(new Paragraph("No.", fontSmallBold));
    if (config.getBoolean("gridBorder")) {
        h2.setBorder(PdfPCell.BOX);
    } else {
        h2.setBorder(0);
    }
    h2.setPadding(4);
    h2.setBorderColor(Color.GRAY);
    h2.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(h2);
    int stpcol1 = 0;
    for (int hcol = stcol; hcol < stpcol; hcol++) {
        PdfPCell h1 = new PdfPCell(new Paragraph(colHeader[hcol], fontSmallBold));
        h1.setHorizontalAlignment(Element.ALIGN_CENTER);
        if (config.getBoolean("gridBorder")) {
            h1.setBorder(PdfPCell.BOX);
        } else {
            h1.setBorder(0);
        }
        h1.setBorderColor(Color.GRAY);
        h1.setPadding(4);
        table.addCell(h1);
    }
    table.setHeaderRows(1);

    for (int row = strow; row < stprow; row++) {
        h2 = new PdfPCell(new Paragraph(String.valueOf(row + 1), f1));
        if (config.getBoolean("gridBorder")) {
            h2.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
        } else {
            h2.setBorder(0);
        }
        h2.setPadding(4);
        h2.setBorderColor(Color.GRAY);
        h2.setHorizontalAlignment(Element.ALIGN_CENTER);
        h2.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(h2);

        for (int col = stcol; col < stpcol; col++) {
            Paragraph para = null;
            if (store.getJSONObject(row).has(colwidth2[col]))
                para = new Paragraph(store.getJSONObject(row).getString(colwidth2[col]), f1);
            else
                para = new Paragraph("", f1);
            //                    Paragraph para = new Paragraph(store.getJSONObject(row).getString(colwidth2[col]), f1);
            PdfPCell h1 = new PdfPCell(para);
            if (config.getBoolean("gridBorder")) {
                h1.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
            } else {
                h1.setBorder(0);
            }
            h1.setPadding(4);
            h1.setBorderColor(Color.GRAY);
            h1.setHorizontalAlignment(Element.ALIGN_CENTER);
            h1.setVerticalAlignment(Element.ALIGN_CENTER);
            table.addCell(h1);
        }

    }
    document.add(table);
    document.newPage();

    /* add instance of OnStartPage*/
    if (widths.length() != 0) {
        if (stpcol != colwidth2.length) {
            float twidth = 0;
            stpcol1 = stpcol;

            int docwidth;
            if (config.getBoolean("landscape"))
                docwidth = 800;
            else
                docwidth = 600;

            while (twidth < docwidth && stpcol1 < f.length) {
                twidth += f[stpcol1];
                stpcol1++;
            }
            stpcol1--;

            addTable(stpcol, stpcol1, strow, stprow, store, colwidth2, colHeader, document);
        }

    } else {
        if (stpcol != colwidth2.length) {
            if ((colwidth2.length - stpcol) > showColumns) { // column limit
                stpcol1 = stpcol + 5;
            } else {
                stpcol1 = (colwidth2.length - stpcol) + stpcol;
            }
            addTable(stpcol, stpcol1, strow, stprow, store, colwidth2, colHeader, document);
        }
    }
    return stpcol;
}

From source file:com.krawler.esp.servlets.ExportProjectSummaryServlet.java

License:Open Source License

private void addTable(JSONArray store, String[] res, String[] colIndex, String[] colHeader, String[] mainHeader,
        String[] val, Document document) throws JSONException, DocumentException {

    java.awt.Color tColor = new Color(0, 0, 0);
    fontSmallBold.setColor(tColor);/*from   w ww . ja  v a 2s  .c  om*/
    Font f1 = FontFactory.getFont("Helvetica", 8, Font.NORMAL, tColor);

    float[] colw = new float[4];
    for (int x = 0; x < 4; x++) {
        colw[x] = 150;
    }
    int col = 0;

    for (int x = 0; x < mainHeader.length; x++) {
        //table start
        PdfPTable table = new PdfPTable(colw);
        table.setTotalWidth(88);
        table.setWidthPercentage(colw, document.getPageSize());
        //table.setSpacingBefore(10);

        PdfPTable mainTable = new PdfPTable(1);
        mainTable.setTotalWidth(90);
        mainTable.setWidthPercentage(100);
        mainTable.setSpacingBefore(20);

        //header cell for mainTable
        PdfPCell headcell = null;
        headcell = new PdfPCell(new Paragraph(mainHeader[x], fontSmallBold));
        headcell.setBackgroundColor(new Color(0xEEEEEE));
        headcell.setPadding(padding);
        mainTable.addCell(headcell);
        document.add(mainTable);

        //header cell added to mainTable
        int row = 3;
        if (x == 0 || x == 4) {
            row = 4;
        } else if (x == 5) {
            row = 0;
        }
        for (; row > 0; row--) {
            for (int y = 1; y < colw.length + 1; y++) {// for each column add the colHeader and value cell
                if (col != colHeader.length) {
                    if (y % 2 != 0) {
                        Paragraph p = new Paragraph(colHeader[col], f1);
                        if (colHeader[col].contains("Variance")) {
                            p = new Paragraph(colHeader[col], fontSmallBold);
                        }
                        PdfPCell pcell = new PdfPCell(p);
                        if (gridBorder) {
                            pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
                        } else {
                            pcell.setBorder(0);
                        }
                        pcell.setPadding(padding);
                        pcell.setBorderColor(new Color(0xF2F2F2));
                        pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pcell.setVerticalAlignment(Element.ALIGN_CENTER);
                        table.addCell(pcell);
                    } else {
                        Paragraph p;
                        p = new Paragraph(val[col], f1);
                        if (colHeader[col].contains("Start Variance")
                                || colHeader[col].contains("End Variance")) {
                            p = new Paragraph(val[col] + " days", fontSmallBold);
                        } else if (colHeader[col].contains("Duration")) {
                            if (colHeader[col].contains("Duration Variance"))
                                p = new Paragraph(val[col] + " days", fontSmallBold);
                            else
                                p = new Paragraph(val[col] + " days", f1);
                        } else if (colHeader[col].contains("Work")) {
                            if (colHeader[col].contains("Work Variance"))
                                p = new Paragraph(val[col] + " hrs", fontSmallBold);
                            else
                                p = new Paragraph(val[col] + " hrs", f1);
                        } else if (colHeader[col].contains("Cost")) {
                            if (colHeader[col].contains("Cost Variance"))
                                p = new Paragraph(currSymbol + " " + val[col], fontSmallBold);
                            else
                                p = new Paragraph(currSymbol + " " + val[col], f1);
                        } else if (colHeader[col].contains("Percent Complete")) {
                            p = new Paragraph(val[col] + " %", f1);
                        }
                        PdfPCell pcell = new PdfPCell(p);
                        if (gridBorder) {
                            pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
                        } else {
                            pcell.setBorder(0);
                        }
                        pcell.setPadding(padding);
                        pcell.setBorderColor(new Color(0xF2F2F2));
                        pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pcell.setVerticalAlignment(Element.ALIGN_CENTER);
                        table.addCell(pcell);
                        col++;
                    }
                }
            }
        }
        if (x == 5) {
            int y = 0;
            row = res.length / 4;
            for (; row > 0; row--) {
                for (int c = 0; c < 2; c++) {
                    Paragraph p = new Paragraph(res[y], f1);
                    PdfPCell pcell = new PdfPCell(p);
                    if (gridBorder) {
                        pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
                    } else {
                        pcell.setBorder(0);
                    }
                    pcell.setPadding(padding);
                    pcell.setBorderColor(new Color(0xF2F2F2));
                    pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    pcell.setVerticalAlignment(Element.ALIGN_CENTER);
                    table.addCell(pcell);
                    p = new Paragraph(res[y + 1], f1);
                    pcell = new PdfPCell(p);
                    if (gridBorder) {
                        pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT | PdfPCell.TOP);
                    } else {
                        pcell.setBorder(0);
                    }
                    pcell.setPadding(padding);
                    pcell.setBorderColor(new Color(0xF2F2F2));
                    pcell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    pcell.setVerticalAlignment(Element.ALIGN_CENTER);
                    table.addCell(pcell);
                    y += 2;
                }
            }
        }
        document.add(table);
    }
}

From source file:com.krawler.esp.servlets.ExportServlet.java

License:Open Source License

private int addTable(int stcol, int stpcol, int strow, int stprow, JSONArray store, String[] colwidth2,
        String[] colHeader, String[] widths, String[] align, Document document, HttpServletRequest request,
        Session session) throws JSONException, DocumentException, SessionExpiredException {

    java.awt.Color tColor = new Color(Integer.parseInt(config.getString("textColor"), 16));
    fontSmallBold.setColor(tColor);//w w  w .  j  a  v a  2 s.c o m
    PdfPTable table;
    float[] tcol;
    tcol = new float[colHeader.length + 1];
    tcol[0] = 40;
    for (int i = 1; i < colHeader.length + 1; i++) {
        tcol[i] = Float.parseFloat(widths[i - 1]);
    }
    table = new PdfPTable(colHeader.length + 1);
    table.setWidthPercentage(tcol, document.getPageSize());
    table.setSpacingBefore(15);
    Font f1 = FontFactory.getFont("Helvetica", 8, Font.NORMAL, tColor);
    PdfPCell h2 = new PdfPCell(new Paragraph("No.", fontSmallBold));
    if (config.getBoolean("gridBorder")) {
        h2.setBorder(PdfPCell.BOX);
    } else {
        h2.setBorder(0);
    }
    h2.setPadding(4);
    h2.setBorderColor(Color.GRAY);
    h2.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(h2);
    PdfPCell h1 = null;
    for (int hcol = stcol; hcol < colwidth2.length; hcol++) {
        if (align[hcol].equals("right") && !colHeader[hcol].equals("")) {
            String currency = currencyRender("", session, request);
            h1 = new PdfPCell(new Paragraph(colHeader[hcol] + "(" + currency + ")", fontSmallBold));
        } else
            h1 = new PdfPCell(new Paragraph(colHeader[hcol], fontSmallBold));
        h1.setHorizontalAlignment(Element.ALIGN_CENTER);
        if (config.getBoolean("gridBorder")) {
            h1.setBorder(PdfPCell.BOX);
        } else {
            h1.setBorder(0);
        }
        h1.setBorderColor(Color.GRAY);
        h1.setPadding(4);
        table.addCell(h1);
    }
    table.setHeaderRows(1);

    for (int row = strow; row < stprow; row++) {
        h2 = new PdfPCell(new Paragraph(String.valueOf(row + 1), f1));
        if (config.getBoolean("gridBorder")) {
            h2.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
        } else {
            h2.setBorder(0);
        }
        h2.setPadding(4);
        h2.setBorderColor(Color.GRAY);
        h2.setHorizontalAlignment(Element.ALIGN_CENTER);
        h2.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(h2);

        JSONObject temp = store.getJSONObject(row);
        for (int col = 0; col < colwidth2.length; col++) {
            Paragraph para = null;
            if (align[col].equals("right") && !temp.getString(colwidth2[col]).equals("")) {
                String currency = currencyRender(temp.getString(colwidth2[col]), session, request);
                para = new Paragraph(currency, f1);
            } else {
                if (colwidth2[col].equals("invoiceno")) {
                    para = new Paragraph(temp.getString("no").toString(), f1);
                } else if (colwidth2[col].equals("invoicedate")) {
                    para = new Paragraph(temp.getString("date").toString(), f1);
                } else if ((temp.isNull(colwidth2[col])) && !(colwidth2[col].equals("invoiceno"))
                        && !(colwidth2[col].equals("invoicedate"))) {
                    para = new Paragraph("", fontMediumRegular);
                } else {
                    para = new Paragraph(temp.getString(colwidth2[col]).toString(), f1);
                }
            }
            h1 = new PdfPCell(para);
            if (config.getBoolean("gridBorder")) {
                h1.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
            } else {
                h1.setBorder(0);
            }
            h1.setPadding(4);
            h1.setBorderColor(Color.GRAY);
            if (!align[col].equals("right") && !align[col].equals("left")) {
                h1.setHorizontalAlignment(Element.ALIGN_CENTER);
                h1.setVerticalAlignment(Element.ALIGN_CENTER);
            } else if (align[col].equals("right")) {
                h1.setHorizontalAlignment(Element.ALIGN_RIGHT);
                h1.setVerticalAlignment(Element.ALIGN_RIGHT);
            } else if (align[col].equals("left")) {
                h1.setHorizontalAlignment(Element.ALIGN_LEFT);
                h1.setVerticalAlignment(Element.ALIGN_LEFT);
            }
            table.addCell(h1);
        }
    }
    document.add(table);
    document.newPage();

    return stpcol;
}

From source file:com.krawler.spring.exportFunctionality.exportDAOImpl.java

License:Open Source License

public int addTable(int stcol, int stpcol, int strow, int stprow, JSONArray store, String[] colwidth2,
        String[] colHeader, String[] widths, String[] align, String[] xtype, Document document)
        throws ServiceException {
    try {/*from   w ww . j  a va 2 s. c o  m*/
        java.awt.Color tColor = new Color(Integer.parseInt(config.getString("textColor"), 16));
        PdfPTable table;
        float[] tcol;
        tcol = new float[colHeader.length + 1];
        tcol[0] = 40;
        for (int i = 1; i < colHeader.length + 1; i++) {
            tcol[i] = Float.parseFloat(widths[i - 1]);
        }
        table = new PdfPTable(colHeader.length + 1);
        table.setWidthPercentage(tcol, document.getPageSize());

        table.setSpacingBefore(15);

        PdfPCell h2 = new PdfPCell(
                new Paragraph(fontFamilySelector.process("No.", FontContext.TABLE_HEADER, tColor)));
        if (config.getBoolean("gridBorder")) {
            h2.setBorder(PdfPCell.BOX);
        } else {
            h2.setBorder(0);
        }
        h2.setPadding(4);
        h2.setBorderColor(Color.GRAY);
        h2.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(h2);
        PdfPCell h1 = null;
        for (int hcol = stcol; hcol < colwidth2.length; hcol++) {
            h1 = new PdfPCell(new Paragraph(
                    fontFamilySelector.process(colHeader[hcol], FontContext.TABLE_HEADER, tColor)));
            h1.setHorizontalAlignment(Element.ALIGN_CENTER);
            if (config.getBoolean("gridBorder")) {
                h1.setBorder(PdfPCell.BOX);
            } else {
                h1.setBorder(0);
            }
            h1.setBorderColor(Color.GRAY);
            h1.setPadding(4);
            table.addCell(h1);
        }
        table.setHeaderRows(1);

        for (int row = strow; row < stprow; row++) {
            h2 = new PdfPCell(new Paragraph(
                    fontFamilySelector.process(String.valueOf(row + 1), FontContext.TABLE_DATA, tColor)));
            if (config.getBoolean("gridBorder")) {
                h2.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
            } else {
                h2.setBorder(0);
            }
            h2.setPadding(4);
            h2.setBorderColor(Color.GRAY);
            h2.setHorizontalAlignment(Element.ALIGN_CENTER);
            h2.setVerticalAlignment(Element.ALIGN_CENTER);
            table.addCell(h2);

            JSONObject temp = store.getJSONObject(row);
            for (int col = 0; col < colwidth2.length; col++) {
                String str = temp.optString((colwidth2[col]), "");
                try {
                    if (xtype.length > 0) {
                        str = formatValue(temp.optString((colwidth2[col]), ""), xtype[col]);
                    }
                } catch (Exception e) {

                }
                Paragraph para = new Paragraph(fontFamilySelector.process(str, FontContext.TABLE_DATA, tColor));
                h1 = new PdfPCell(para);
                if (config.getBoolean("gridBorder")) {
                    h1.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
                } else {
                    h1.setBorder(0);
                }
                h1.setPadding(4);
                h1.setBorderColor(Color.GRAY);

                if (!align[col].equals("right") && !align[col].equals("left")) {
                    h1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    h1.setVerticalAlignment(Element.ALIGN_CENTER);
                } else if (align[col].equals("right")) {
                    h1.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    h1.setVerticalAlignment(Element.ALIGN_RIGHT);
                } else if (align[col].equals("left")) {
                    h1.setHorizontalAlignment(Element.ALIGN_LEFT);
                    h1.setVerticalAlignment(Element.ALIGN_LEFT);
                }
                table.addCell(h1);
            }
        }
        document.add(table);
        document.newPage();
    } catch (Exception e) {
        throw ServiceException.FAILURE("exportDAOImpl.addTable", e);
    }
    return stpcol;
}

From source file:com.krawler.spring.exportFunctionality.exportMPXDAOImpl.java

License:Open Source License

public int addTable(int stcol, int stpcol, int strow, int stprow, JSONArray store, String[] colwidth2,
        String[] colHeader, String[] widths, String[] align, Document document, HttpServletRequest request)
        throws ServiceException {
    try {//from   w  w w  .j a v a  2 s  .c  o m
        DateFormat formatter = authHandlerDAOObj.getUserDateFormatter(
                sessionHandlerImpl.getDateFormatID(request), sessionHandlerImpl.getTimeZoneDifference(request),
                true);
        DateFormat frmt = authHandler.getDateFormatter(request);
        String currencyid = sessionHandlerImpl.getCurrencyID(request);
        int mode = Integer.parseInt(request.getParameter("get"));
        double totalCre = 0;
        double totalDeb = 0;
        java.awt.Color tColor = new Color(Integer.parseInt(config.getString("textColor"), 16));
        //            fontSmallRegular.setColor(tColor);
        PdfPTable table;
        float[] tcol;
        tcol = new float[colHeader.length + 1];
        tcol[0] = 40;
        for (int i = 1; i < colHeader.length + 1; i++) {
            tcol[i] = Float.parseFloat(widths[i - 1]);
        }
        table = new PdfPTable(colHeader.length + 1);
        table.setWidthPercentage(tcol, document.getPageSize());
        table.setSpacingBefore(15);
        PdfPCell h2 = new PdfPCell(new Paragraph(
                (new Phrase(fontFamilySelector.process("No.", FontContext.FOOTER_NOTE, tColor)))));
        if (config.getBoolean("gridBorder")) {
            h2.setBorder(PdfPCell.BOX);
        } else {
            h2.setBorder(0);
        }
        h2.setPadding(4);
        h2.setBorderColor(Color.GRAY);
        h2.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(h2);
        PdfPCell h1 = null;
        for (int hcol = stcol; hcol < colwidth2.length; hcol++) {
            String headerStr = StringUtil.serverHTMLStripper(colHeader[hcol]);
            if (align[hcol].equals("currency") && !colHeader[hcol].equals("")) {
                String currency = currencyRender("", currencyid);
                h1 = new PdfPCell(new Paragraph((new Phrase(fontFamilySelector
                        .process(headerStr + "(" + currency + ")", FontContext.FOOTER_NOTE, tColor)))));
            } else {
                h1 = new PdfPCell(new Paragraph(
                        (new Phrase(fontFamilySelector.process(headerStr, FontContext.FOOTER_NOTE, tColor)))));
            }
            h1.setHorizontalAlignment(Element.ALIGN_CENTER);
            if (config.getBoolean("gridBorder")) {
                h1.setBorder(PdfPCell.BOX);
            } else {
                h1.setBorder(0);
            }
            h1.setBorderColor(Color.GRAY);
            h1.setPadding(4);
            table.addCell(h1);
        }
        table.setHeaderRows(1);

        for (int row = strow; row < stprow; row++) {
            h2 = new PdfPCell(
                    new Paragraph(fontFamilySelector.process(String.valueOf(row + 1), FontContext.TABLE_DATA)));
            if (config.getBoolean("gridBorder")) {
                h2.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
            } else {
                h2.setBorder(0);
            }
            h2.setPadding(4);
            h2.setBorderColor(Color.GRAY);
            h2.setHorizontalAlignment(Element.ALIGN_CENTER);
            h2.setVerticalAlignment(Element.ALIGN_CENTER);
            table.addCell(h2);

            JSONObject temp = store.getJSONObject(row);
            if (mode == 116 || mode == 117) {
                totalCre = totalCre + Double
                        .parseDouble(temp.getString("c_amount") != "" ? temp.getString("c_amount") : "0");
                totalDeb = totalDeb + Double
                        .parseDouble(temp.getString("d_amount") != "" ? temp.getString("d_amount") : "0");
            }
            for (int col = 0; col < colwidth2.length; col++) {
                Paragraph para = null;
                String rowCurrencyId = temp.has("currencyid") ? temp.getString("currencyid") : currencyid;
                if (align[col].equals("currency") && !temp.getString(colwidth2[col]).equals("")) {
                    String currency = currencyRender(temp.getString(colwidth2[col]), currencyid);
                    para = new Paragraph(fontFamilySelector.process(currency, FontContext.TABLE_DATA));
                } else if (align[col].equals("rowcurrency") && !temp.getString(colwidth2[col]).equals("")) {
                    String withCurrency = currencyRender(temp.getString(colwidth2[col]), rowCurrencyId);
                    para = new Paragraph(fontFamilySelector.process(withCurrency, FontContext.TABLE_DATA));
                } else if (align[col].equals("date") && !temp.getString(colwidth2[col]).equals("")) {
                    try {
                        String d1 = formatter.format(frmt.parse(temp.getString(colwidth2[col])));
                        para = new Paragraph(fontFamilySelector.process(d1, FontContext.TABLE_DATA));
                    } catch (Exception ex) {
                        para = new Paragraph(fontFamilySelector.process(temp.getString(colwidth2[col]),
                                FontContext.TABLE_DATA));
                    }
                } else if (colwidth2[col].equals("taxrate")
                        || colwidth2[col].equals("permargin") && !colHeader[col].equals("")) {
                    para = new Paragraph(fontFamilySelector.process(
                            htmlPercentageRender(temp.getString(colwidth2[col]), true),
                            FontContext.TABLE_DATA));
                } else {
                    if (colwidth2[col].equals("invoiceno")) {
                        para = new Paragraph(fontFamilySelector.process(temp.getString("no").toString(),
                                FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("invoicedate")) {
                        para = new Paragraph(fontFamilySelector.process(temp.getString("date").toString(),
                                FontContext.TABLE_DATA));
                    } else if ((temp.isNull(colwidth2[col])) && !(colwidth2[col].equals("invoiceno"))
                            && !(colwidth2[col].equals("invoicedate"))) {
                        para = new Paragraph(fontFamilySelector.process("", FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("c_date")) {
                        para = new Paragraph(fontFamilySelector.process(formatter.format(
                                frmt.parse(temp.getString("c_date").toString() == "" ? temp.getString("d_date")
                                        : temp.getString("c_date"))),
                                FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("c_accountname")) {
                        para = new Paragraph(
                                fontFamilySelector.process(
                                        temp.getString("c_accountname").toString() == ""
                                                ? temp.getString("d_accountname").toString()
                                                : temp.getString("c_accountname").toString(),
                                        FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("c_entryno")) {
                        para = new Paragraph(
                                fontFamilySelector.process(
                                        temp.getString("c_entryno").toString() == ""
                                                ? temp.getString("d_entryno").toString()
                                                : temp.getString("c_entryno").toString(),
                                        FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("d_date")) {
                        para = new Paragraph(fontFamilySelector.process(formatter.format(
                                frmt.parse(temp.getString("d_date").toString() == "" ? temp.getString("c_date")
                                        : temp.getString("d_date"))),
                                FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("d_accountname")) {
                        para = new Paragraph(
                                fontFamilySelector.process(
                                        temp.getString("d_accountname").toString() == ""
                                                ? temp.getString("c_accountname").toString()
                                                : temp.getString("d_accountname").toString(),
                                        FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("d_entryno")) {
                        para = new Paragraph(
                                fontFamilySelector.process(
                                        temp.getString("d_entryno").toString() == ""
                                                ? temp.getString("c_entryno").toString()
                                                : temp.getString("d_entryno").toString(),
                                        FontContext.TABLE_DATA));
                    } else if (colwidth2[col].equals("perioddepreciation")) {
                        double adj = temp.getDouble("perioddepreciation") - temp.getDouble("firstperiodamt");
                        String currency = currencyRender("" + adj, currencyid);
                        if (adj < 0.0001) {
                            para = new Paragraph(fontFamilySelector.process("", FontContext.TABLE_DATA));
                        } else {
                            para = new Paragraph(fontFamilySelector.process(currency, FontContext.TABLE_DATA));
                        }
                    } else if (colHeader[col].equals("Opening Balance")
                            || colHeader[col].equals("Asset Value")) {
                        String currency = currencyRender("" + Math.abs(temp.getDouble("openbalance")),
                                currencyid);
                        para = new Paragraph(fontFamilySelector.process(currency, FontContext.TABLE_DATA));
                    } else {
                        if (colHeader[col].equals("Opening Balance Type")) {
                            double bal = Double.parseDouble(temp.getString(colwidth2[col]));
                            String str = bal == 0 ? "" : (bal < 0 ? "Credit" : "Debit");
                            if (str.equals(""))
                                str = "N/A";
                            para = new Paragraph(fontFamilySelector.process(str, FontContext.TABLE_DATA));
                        } else {
                            para = new Paragraph(fontFamilySelector.process(
                                    temp.getString(colwidth2[col]).toString(), FontContext.TABLE_DATA));
                        }
                    }
                }
                h1 = new PdfPCell(para);
                if (config.getBoolean("gridBorder")) {
                    h1.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
                } else {
                    h1.setBorder(0);
                }
                h1.setPadding(4);
                h1.setBorderColor(Color.GRAY);
                if (align[col].equals("currency") || align[col].equals("rowcurrency")
                        || colwidth2[col].equals("taxrate") || colwidth2[col].equals("permargin")) {
                    h1.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    h1.setVerticalAlignment(Element.ALIGN_RIGHT);
                } else if (align[col].equals("date")) {
                    h1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    h1.setVerticalAlignment(Element.ALIGN_CENTER);
                } else {
                    h1.setHorizontalAlignment(Element.ALIGN_LEFT);
                    h1.setVerticalAlignment(Element.ALIGN_LEFT);
                }
                table.addCell(h1);
            }
        }
        if (mode == 116 || mode == 117) {
            Paragraph para1 = null;
            PdfPCell h3 = null;
            String totCr = "";
            String totDb = "";
            h3 = new PdfPCell(new Paragraph(fontFamilySelector.process("", FontContext.TABLE_DATA)));
            if (config.getBoolean("gridBorder")) {
                h3.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT);
            } else {
                h3.setBorder(0);
            }
            h3.setPadding(4);
            h3.setBorderColor(Color.GRAY);
            h3.setBackgroundColor(Color.lightGray);
            h3.setHorizontalAlignment(Element.ALIGN_CENTER);
            h3.setVerticalAlignment(Element.ALIGN_CENTER);
            table.addCell(h3);
            para1 = new Paragraph(fontFamilySelector.process("Total", FontContext.REPORT_TITLE));
            h3 = new PdfPCell(para1);
            if (config.getBoolean("gridBorder")) {
                h3.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
            } else {
                h3.setBorder(0);
            }
            h3.setPadding(4);
            h3.setBorderColor(Color.GRAY);
            h3.setBackgroundColor(Color.LIGHT_GRAY);
            h3.setHorizontalAlignment(Element.ALIGN_LEFT);
            h3.setVerticalAlignment(Element.ALIGN_LEFT);
            table.addCell(h3);

            for (int col = 1; col < colwidth2.length; col++) {
                if (colwidth2[col].equals("c_amount")) {
                    totCr = currencyRender(String.valueOf(totalCre), currencyid);
                    para1 = new Paragraph(fontFamilySelector.process(totCr, FontContext.TABLE_DATA));
                } else if (colwidth2[col].equals("d_amount")) {
                    totDb = currencyRender(String.valueOf(totalDeb), currencyid);
                    para1 = new Paragraph(fontFamilySelector.process(totDb, FontContext.TABLE_DATA));
                } else {
                    para1 = new Paragraph(fontFamilySelector.process("", FontContext.TABLE_DATA));
                }

                h3 = new PdfPCell(para1);
                if (config.getBoolean("gridBorder")) {
                    h3.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
                } else {
                    h3.setBorder(0);
                }
                h3.setPadding(4);
                h3.setBorderColor(Color.GRAY);
                h3.setBackgroundColor(Color.LIGHT_GRAY);
                h3.setHorizontalAlignment(Element.ALIGN_RIGHT);
                h3.setVerticalAlignment(Element.ALIGN_RIGHT);
                table.addCell(h3);

            }
        }
        document.add(table);
        document.newPage();
    } catch (Exception e) {
        throw ServiceException.FAILURE("exportDAOImpl.addTable", e);
    }
    return stpcol;
}

From source file:com.krawler.spring.exportFunctionality.exportMPXDAOImpl.java

License:Open Source License

public int addGroupableTable(JSONObject groupingConfig, String groupByField, String groupHeaderText, int stcol,
        int stpcol, int strow, int stprow, JSONArray store, String[] dataIndexArr, String[] colHeader,
        String[] widths, String[] align, Document document, HttpServletRequest request)
        throws ServiceException {
    try {//from  w  ww  .ja v a2 s. c  o  m
        String groupSummaryField = groupingConfig.getString("groupSummaryField");
        String groupSummaryText = groupingConfig.getString("groupSummaryText");
        String reportSummaryField = groupingConfig.getString("reportSummaryField");
        String reportSummaryText = groupingConfig.getString("reportSummaryText");

        DateFormat formatter = authHandlerDAOObj.getUserDateFormatter(
                sessionHandlerImpl.getDateFormatID(request), sessionHandlerImpl.getTimeZoneDifference(request),
                true);
        DateFormat frmt = authHandler.getDateFormatter(request);
        String currencyid = sessionHandlerImpl.getCurrencyID(request);
        java.awt.Color tColor = new Color(Integer.parseInt(config.getString("textColor"), 16));
        java.awt.Color gsBGColor = new Color(Integer.parseInt("E5E5E5", 16));
        java.awt.Color rsBGColor = new Color(Integer.parseInt("808080", 16));
        //            fontRegular.setColor(tColor);
        PdfPTable table;

        float[] tcol;
        tcol = new float[colHeader.length + 1];
        tcol[0] = 40;
        for (int i = 1; i < colHeader.length + 1; i++) {
            tcol[i] = Float.parseFloat(widths[i - 1]);
        }
        table = new PdfPTable(colHeader.length + 1);
        table.setWidthPercentage(tcol, document.getPageSize());
        table.setSpacingBefore(15);
        PdfPCell h2 = new PdfPCell(
                new Paragraph(fontFamilySelector.process("No.", FontContext.TABLE_HEADER, tColor)));
        if (config.getBoolean("gridBorder")) {
            h2.setBorder(PdfPCell.BOX);
        } else {
            h2.setBorder(0);
        }
        h2.setPadding(4);
        h2.setBorderColor(Color.GRAY);
        h2.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(h2);
        PdfPCell h1 = null;
        for (int hcol = stcol; hcol < dataIndexArr.length; hcol++) {
            if (align[hcol].equals("currency") && !colHeader[hcol].equals("")) {
                String currency = currencyRender("", currencyid);
                h1 = new PdfPCell(new Paragraph(fontFamilySelector
                        .process(colHeader[hcol] + "(" + currency + ")", FontContext.TABLE_HEADER, tColor)));
            } else {
                h1 = new PdfPCell(new Paragraph(
                        fontFamilySelector.process(colHeader[hcol], FontContext.TABLE_HEADER, tColor)));
            }
            h1.setHorizontalAlignment(Element.ALIGN_CENTER);
            if (config.getBoolean("gridBorder")) {
                h1.setBorder(PdfPCell.BOX);
            } else {
                h1.setBorder(0);
            }
            h1.setBorderColor(Color.GRAY);
            h1.setPadding(4);
            table.addCell(h1);
        }
        table.setHeaderRows(1);
        String groupName = "", rowCurrency = "";
        Double subTotal = 0.0;
        Double grandTotal = 0.0;
        int rowSpan = 0;
        for (int row = strow; row < stprow; row++) {
            rowSpan++;
            JSONObject rowData = store.getJSONObject(row);
            if (row == 0) {
                groupName = rowData.getString(groupByField);
                rowCurrency = rowData.has("currencyid") ? rowData.getString("currencyid") : currencyid;
                subTotal = 0.0;
                addGroupRow(groupHeaderText + ": " + groupName, currencyid, table, dataIndexArr);
            }
            if (!groupName.equalsIgnoreCase(rowData.getString(groupByField))) {
                addSummaryRow(groupSummaryText + groupName + " ", subTotal, rowCurrency, table, dataIndexArr,
                        false, gsBGColor);
                groupName = rowData.getString(groupByField);
                rowCurrency = rowData.has("currencyid") ? rowData.getString("currencyid") : currencyid;
                addGroupRow(groupHeaderText + ": " + groupName, currencyid, table, dataIndexArr);
                subTotal = 0.0;
                rowSpan = 1;
            }
            subTotal += Double.parseDouble(rowData.getString(groupSummaryField));
            grandTotal += Double.parseDouble(rowData.getString(reportSummaryField));
            rowCurrency = rowData.has("currencyid") ? rowData.getString("currencyid") : currencyid;

            h2 = new PdfPCell(new Paragraph(
                    fontFamilySelector.process(String.valueOf(row + 1), FontContext.TABLE_HEADER, tColor)));
            if (config.getBoolean("gridBorder")) {
                h2.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
            } else {
                h2.setBorder(0);
            }
            h2.setPadding(4);
            h2.setBorderColor(Color.GRAY);
            h2.setHorizontalAlignment(Element.ALIGN_CENTER);
            h2.setVerticalAlignment(Element.ALIGN_CENTER);
            table.addCell(h2);

            for (int col = 0; col < dataIndexArr.length; col++) {
                String cellData = null;
                if (align[col].equals("currency") && !rowData.getString(dataIndexArr[col]).equals("")) {
                    cellData = currencyRender(rowData.getString(dataIndexArr[col]),
                            rowData.getString("currencyid"));
                } else if (align[col].equals("date") && !rowData.getString(dataIndexArr[col]).equals("")) {
                    try {
                        cellData = formatter.format(frmt.parse(rowData.getString(dataIndexArr[col])));
                    } catch (Exception ex) {
                        cellData = rowData.getString(dataIndexArr[col]);
                    }
                } else {
                    cellData = rowData.getString(dataIndexArr[col]);
                }

                Paragraph para = new Paragraph(
                        fontFamilySelector.process(cellData, FontContext.TABLE_HEADER, tColor));
                h1 = new PdfPCell(para);
                if (config.getBoolean("gridBorder")) {
                    h1.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT);
                } else {
                    h1.setBorder(0);
                }
                h1.setPadding(4);
                h1.setBorderColor(Color.GRAY);

                if (!align[col].equals("currency") && !align[col].equals("date")) {
                    h1.setHorizontalAlignment(Element.ALIGN_LEFT);
                    h1.setVerticalAlignment(Element.ALIGN_LEFT);
                } else if (align[col].equals("currency")) {
                    h1.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    h1.setVerticalAlignment(Element.ALIGN_RIGHT);
                } else if (align[col].equals("date")) {
                    h1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    h1.setVerticalAlignment(Element.ALIGN_CENTER);
                }
                table.addCell(h1);
            }
        }
        if (rowSpan > 0) {
            addSummaryRow(groupSummaryText + groupName + " ", subTotal, rowCurrency, table, dataIndexArr, false,
                    gsBGColor);
        }
        addSummaryRow(reportSummaryText, grandTotal, currencyid, table, dataIndexArr, false, rsBGColor);

        document.add(table);
        document.newPage();
    } catch (Exception e) {
        throw ServiceException.FAILURE("exportDAOImpl.addTable", e);
    }
    return stpcol;
}