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

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

Introduction

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

Prototype

public void addCell(Phrase phrase) 

Source Link

Document

Adds a cell element.

Usage

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  w  w  . j a v a2  s .c o  m*/
    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.ExportReportServlet.java

License:Open Source License

private static void prepare(Document d, HeaderFooter hfFooter) {
    PdfPTable table = new PdfPTable(1);
    try {// w  ww. j av  a2 s .  c  om
        d.setFooter(hfFooter);
        d.open();
        imgPath = StorageHandler.GetProfileImgStorePath() + StorageHandler.GetFileSeparator() + imgPath;
        //                  imgPath = "/home/krawler-user/logo.jpg";                
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.setWidthPercentage(50);
        PdfPCell cell = null;
        try {
            Image img = Image.getInstance(imgPath);
            cell = new PdfPCell(img);
        } catch (Exception e) {
            cell = new PdfPCell(new Paragraph(companyName, fontBig));
        }
        cell.setBorder(0);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.addCell(cell);

        d.add(table);
    } catch (Exception e) {
        Logger.getLogger(ExportProjectReportServlet.class.getName()).log(Level.SEVERE, null, e);
    }
}

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

License:Open Source License

private static void addCenter(Document d) throws DocumentException {

    PdfPTable table = new PdfPTable(1);
    table.setHorizontalAlignment(Element.ALIGN_CENTER);

    table.setWidthPercentage(50);//  w w w  . j av  a  2  s . c o m
    table.setSpacingBefore(10);
    PdfPCell cell = null;
    cell = new PdfPCell(new Paragraph(ProjectName, fontBold));
    cell.setBorder(0);
    cell.setBorderWidth(0);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);

    cell = new PdfPCell(new Paragraph(ReportName, fontRegular));
    cell.setBorder(0);
    cell.setBorderWidth(0);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);
    // For tasks which prompt user for a start and end date
    if (!(StringUtil.isNullOrEmpty(RepStartDate) && StringUtil.isNullOrEmpty(RepEndDate))) {
        cell = new PdfPCell(new Paragraph("From:" + RepStartDate + " To:" + RepEndDate, fontSmallRegular));
        cell.setBorder(0);
        cell.setBorderWidth(0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }
    table.setSpacingAfter(10);
    d.add(table);
}

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

License:Open Source License

private int addTable(int stcol, int stpcol, int strow, int stprow, JSONArray store, String[] colwidth2,
        int maxlevel, Document document) {
    float[] f = new float[(stpcol - stcol) + 1];
    for (int k = 0; k < f.length; k++) {
        f[k] = 20;/*from  w w w  .  j  a  va2s. com*/
    }
    f[0] = f[0] + 10 * maxlevel;
    PdfPTable table = new PdfPTable(f);
    Font font = FontFactory.getFont("Helvetica", 8, Font.BOLD, Color.BLACK);
    Font font1 = FontFactory.getFont("Helvetica", 8, Font.NORMAL, Color.BLACK);
    Font f1;
    PdfPCell h2 = new PdfPCell(new Paragraph("", font)); // new
    h2.setBorder(0);
    h2.setPadding(1);
    //                h2.setHorizontalAlignment(Element.ALIGN_UNDEFINED);                                       
    // Paragraph(colwidth2[hcol],font);
    table.addCell(h2);
    int stpcol1 = 0;
    for (int hcol = stcol; hcol < stpcol; hcol++) {
        PdfPCell h1 = new PdfPCell(new Paragraph(colwidth2[hcol], font)); // new
        h1.setHorizontalAlignment(Element.ALIGN_RIGHT);
        h1.setBorder(0);
        h1.setPadding(1);
        // Paragraph(colwidth2[hcol],font);
        table.addCell(h1);
    }
    table.setHeaderRows(1);
    try {
        for (int row = strow; row < stprow; row++) {
            if (row % 62 == 61) {
                document.add(table);
                table.deleteBodyRows();
                table.setSkipFirstHeader(true);
            }
            if (store.getJSONObject(row).getBoolean("flag"))
                f1 = font;
            else
                f1 = font1;
            if (store.getJSONObject(row).getString("info").compareTo("") != 0) {
                h2 = new PdfPCell(new Paragraph(store.getJSONObject(row).getString("info"), f1));
                h2.setPaddingLeft((Integer.parseInt(store.getJSONObject(row).getString("level")) * 10) + 5);
            } else {
                h2 = new PdfPCell(new Paragraph(store.getJSONObject(row).getString("info"), f1));
                h2.setPaddingTop(9);
                h2.setPaddingLeft((Integer.parseInt(store.getJSONObject(row).getString("level")) * 10) + 5);
            }
            h2.setBorder(0);
            h2.setPadding(1);
            if (store.getJSONObject(row).getBoolean("flag"))
                h2.setHorizontalAlignment(Element.ALIGN_LEFT);
            else
                h2.setHorizontalAlignment(Element.ALIGN_RIGHT);
            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);
                h1.setBorder(0);
                h1.setPadding(1);
                h1.setHorizontalAlignment(Element.ALIGN_RIGHT);
                h1.setVerticalAlignment(Element.ALIGN_CENTER);
                table.addCell(h1);
                //               table.addCell(new Paragraph(store.getJSONObject(row)
                //                     .getString(colwidth2[col]), f1));
            }

        }
        document.add(table);
        document.newPage();
        if (stpcol != colwidth2.length) {
            if ((colwidth2.length - stpcol) > 5) // column limit
                stpcol1 = stpcol + 5;
            else
                stpcol1 = (colwidth2.length - stpcol) + stpcol;
            addTable(stpcol, stpcol1, strow, stprow, store, colwidth2, maxlevel, document);
        }

    } catch (com.krawler.utils.json.base.JSONException j) {
        KrawlerLog.op.warn("Problem During Creating JSON Object :" + j.toString());
    } catch (com.lowagie.text.DocumentException de) {
        KrawlerLog.op.warn("Problem While Creating PDF :" + de.toString());
    }
    return stpcol;
}

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

License:Open Source License

private static void addComponyLogo(Document d, HttpServletRequest req)
        throws ConfigurationException, DocumentException {
    PdfPTable table = new PdfPTable(1);
    imgPath = StorageHandler.GetProfileImgStorePath() + "logo.gif";
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.setWidthPercentage(50);//from  w  w w . ja v a2 s.  c om
    PdfPCell cell = null;
    try {
        if (StringUtil.isStandAlone()) {
            imgPath = URLUtil.getPageURL(req, "").concat(defaultCompanyImgPath);
        }
        Image img = Image.getInstance(imgPath);
        cell = new PdfPCell(img);
    } catch (Exception e) {
        cell = new PdfPCell(new Paragraph(companyName, fontBig));
    }
    cell.setBorder(0);
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.addCell(cell);
    d.add(table);
}

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

License:Open Source License

private static void addTitleSubtitle(Document d) throws DocumentException, JSONException {
    java.awt.Color tColor = new Color(Integer.parseInt(config.getString("textColor"), 16));
    fontBold.setColor(tColor);//from   w ww  . j  av  a 2s . c  o  m
    fontRegular.setColor(tColor);
    PdfPTable table = new PdfPTable(1);
    table.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.setWidthPercentage(100);
    table.setSpacingBefore(6);

    //Report Title
    PdfPCell cell = new PdfPCell(new Paragraph(config.getString("title"), fontBold));
    cell.setBorder(0);
    cell.setBorderWidth(0);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);

    //Report Subtitle(s)
    String[] SubTitles = config.getString("subtitles").split("~");// '~' as separator
    for (int i = 0; i < SubTitles.length; i++) {
        cell = new PdfPCell(new Paragraph(SubTitles[i], fontSmallRegular));
        cell.setBorder(0);
        cell.setBorderWidth(0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }
    table.setSpacingAfter(6);
    d.add(table);

    //Separator line
    PdfPTable line = new PdfPTable(1);
    line.setWidthPercentage(100);
    PdfPCell cell1 = null;
    cell1 = new PdfPCell(new Paragraph(""));
    cell1.setBorder(PdfPCell.BOTTOM);
    line.addCell(cell1);
    d.add(line);
}

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);//from  www  .ja v  a2s . co 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.exportFuctionality.ExportinvController.java

License:Open Source License

private static void addHeaderFooter(Document document, PdfWriter writer)
        throws DocumentException, ServiceException {
    fontSmallRegular.setColor(Color.BLACK);
    java.util.Date dt = new java.util.Date();
    String date = "yyyy-MM-dd";
    java.text.SimpleDateFormat dtf = new java.text.SimpleDateFormat(date);
    String DateStr = dtf.format(dt);
    PdfPTable footer = new PdfPTable(1);
    PdfPCell footerSeparator = new PdfPCell(new Phrase(""));
    footerSeparator.setBorder(PdfPCell.BOX);
    footerSeparator.setPadding(0);//from w  w  w. j ava 2s  . c om
    footerSeparator.setColspan(3);
    footer.addCell(footerSeparator);

    String PageDate = DateStr;
    PdfPCell pagerDateCell = new PdfPCell(new Phrase("Generated Date : " + PageDate, fontSmallRegular));
    pagerDateCell.setBorder(0);
    pagerDateCell.setHorizontalAlignment(PdfCell.ALIGN_CENTER);
    footer.addCell(pagerDateCell);
    // -------- footer end   -----------
    try {
        Rectangle page = document.getPageSize();
        footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        footer.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:com.krawler.spring.exportFuctionality.ExportinvController.java

License:Open Source License

private ByteArrayOutputStream createForm(String currencyid, String[] header1, String[] header2,
        String[] header3, String[] header4, String[] values1, String[] values2, String[] values3,
        String[] values4, String string, Object ob, Company com, String contextpath, String logoPath,
        DateFormat formatter) throws JSONException, DocumentException, ServiceException, IOException {
    ByteArrayOutputStream baos = null;
    Document document = null;//from www. j  a  va  2s  .com
    PdfWriter writer = null;
    try {
        String company[] = new String[3];
        company[0] = com.getCompanyName();
        company[1] = com.getAddress();
        company[2] = com.getEmailID();
        baos = new ByteArrayOutputStream();
        document = new Document(PageSize.A4, 15, 15, 15, 15);
        writer = PdfWriter.getInstance(document, baos);
        document.open();
        addHeaderFooter(document, writer);
        PdfPTable tab1 = null;
        PdfPTable tab2 = null;
        PdfPTable tab3 = null;

        /*-----------------------------Add Company Name in Center ------------------*/
        tab1 = new PdfPTable(1);
        tab1.setHorizontalAlignment(Element.ALIGN_LEFT);
        PdfPCell cell = new PdfPCell(new Paragraph(com.getCompanyName(), fontBig));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(0);
        tab1.addCell(cell);

        tab2 = new PdfPTable(1);
        tab2.setHorizontalAlignment(Element.ALIGN_LEFT);
        imgPath = logoPath;
        PdfPCell imgCell = null;
        try {
            Image img = Image.getInstance(imgPath);
            imgCell = new PdfPCell(img);
        } catch (Exception e) {
            imgCell = new PdfPCell(new Paragraph(com.getCompanyName(), fontBig));
        }
        imgCell.setHorizontalAlignment(Element.ALIGN_LEFT);
        imgCell.setBorder(0);
        tab2.addCell(imgCell);

        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        table.setWidths(new float[] { 40, 60 });
        PdfPCell cellCompimg = new PdfPCell(tab2);
        cellCompimg.setBorder(0);
        table.addCell(cellCompimg);
        PdfPCell cellCompname = new PdfPCell(tab1);
        cellCompname.setBorder(0);
        table.addCell(cellCompname);
        document.add(table);
        document.add(new Paragraph("\n\n\n"));

        /*-----------------------------Add Company information and Invoice Info------------------*/
        PdfPTable table2 = new PdfPTable(3);
        table2.setWidthPercentage(100);
        if (header4.length != 0)
            table2.setWidths(new float[] { 40, 30, 30 });
        else
            table2.setWidths(new float[] { 50, 50, 0 });
        tab1 = getCompanyInfo(company);
        tab2 = createTable(header1, values1);
        PdfPCell cell1 = new PdfPCell(tab1);
        cell1.setBorder(0);
        table2.addCell(cell1);
        PdfPCell cell2 = new PdfPCell(tab2);
        cell2.setBorder(1);
        table2.addCell(cell2);
        PdfPCell cel = new PdfPCell();
        if (header4.length != 0) {
            tab3 = createTable(header4, values4);
            cel = new PdfPCell(tab3);
        } else
            cel = new PdfPCell(new Paragraph("", fontSmallRegular));
        cel.setBorder(1);
        table2.addCell(cel);
        document.add(table2);
        document.add(new Paragraph("\n\n\n"));

        /*-----------------------------Add BillTo Amount Enclosed -------------------------*/
        PdfPTable table3 = new PdfPTable(1);
        table3.setWidthPercentage(100);
        tab1 = createTable(header2, values2);
        PdfPCell cell3 = new PdfPCell(tab1);
        cell3.setBorder(1);
        table3.addCell(cell3);
        document.add(table3);
        document.add(new Paragraph("\n\n\n\n\n\n"));

        /*-----------------------------Add Cutting Line -------------------------*/
        PdfPTable table4 = new PdfPTable(1);
        imgPath = contextpath + "/images/pdf-cut.jpg";
        table4.setHorizontalAlignment(Element.ALIGN_LEFT);
        table4.setWidthPercentage(100);
        PdfPCell cell11 = null;
        try {
            Image img = Image.getInstance(imgPath);
            img.scalePercent(35);
            cell11 = new PdfPCell(img);
        } catch (Exception e) {
        }
        cell11.setBorder(0);
        table4.addCell(cell11);
        document.add(table4);
        document.add(new Paragraph("\n\n"));

        /*-----------------------------Add Product Information ------------------*/
        PdfPTable table5 = new PdfPTable(1);
        table5.setWidthPercentage(100);
        if (string.equals("GoodsReceipt")) {
            GoodsReceipt gr = (GoodsReceipt) ob;
            tab1 = createGoodsReceiptTable(header3, values3, currencyid, gr);
        } else if (string.equals("debit")) {
            DebitNote dn = (DebitNote) ob;
            tab1 = createDebitTable(header3, values3, currencyid, dn);
        } else if (string.equals("billingdebit")) {
            BillingDebitNote dn = (BillingDebitNote) ob;
            tab1 = createDebitTable(header3, values3, currencyid, dn);
        } else if (string.equals("credit")) {
            CreditNote cn = (CreditNote) ob;
            tab1 = createCreditTable(header3, values3, currencyid, cn);
        } else if (string.equals("billingcredit")) {
            BillingCreditNote cn = (BillingCreditNote) ob;
            tab1 = createCreditTable(header3, values3, currencyid, cn);
        } else if (string.equals("PurchaseOrder")) {
            PurchaseOrder po = (PurchaseOrder) ob;
            tab1 = createPurchaseOrderTable(header3, values3, currencyid, po);
        } else if (string.equals("BillingPurchaseOrder")) {
            BillingPurchaseOrder po = (BillingPurchaseOrder) ob;
            tab1 = createPurchaseOrderTable(header3, values3, currencyid, po);
        } else if (string.equals("SalesOrder")) {
            SalesOrder so = (SalesOrder) ob;
            tab1 = createSalesOrderTable(header3, values3, currencyid, so);
        } else if (string.equals("BillingSalesOrder")) {
            BillingSalesOrder so = (BillingSalesOrder) ob;
            tab1 = createSalesOrderTable(header3, values3, currencyid, so);
        } else if (string.equals("Payment")) {
            Payment pay = (Payment) ob;
            tab1 = createPaymentTable(header3, values3, currencyid, pay, formatter);
        } else if (string.equals("Quotation")) {
            Quotation quotation = (Quotation) ob;
            tab1 = createQuotationTable(header3, values3, currencyid, quotation);
        }
        PdfPCell cell5 = new PdfPCell(tab1);
        cell5.setBorder(1);
        table5.addCell(cell5);
        document.add(table5);
        document.add(new Paragraph("\n\n\n"));

        /*-----------------------------Download file ------------------*/
        return baos;
    } catch (Exception ex) {
        return null;
    } finally {
        if (document != null)
            document.close();
        if (writer != null)
            writer.close();
        if (baos != null)
            baos.close();
    }
}

From source file:com.krawler.spring.exportFuctionality.ExportinvController.java

License:Open Source License

private PdfPTable createDebitTable(String header[], String values[], String currencyid, DebitNote deb)
        throws DocumentException, ServiceException, JSONException {
    PdfPTable table = new PdfPTable(header.length);
    table.setWidthPercentage(100);//  ww  w .  j a v a  2s  .c o m
    table.setWidths(new float[] { 30, 30, 40 });
    PdfPCell cell = null;
    for (int i = 0; i < header.length; i++) {
        cell = new PdfPCell(new Paragraph(header[i], fontTblMediumBold));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPadding(3);
        table.addCell(cell);
    }
    Iterator itr = deb.getRows().iterator();
    while (itr.hasNext()) {
        DebitNoteDetail debDet = (DebitNoteDetail) itr.next();
        cell = new PdfPCell(new Paragraph(debDet.getGoodsReceiptRow().getInventory().getProduct().getName(),
                fontSmallRegular));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(Rectangle.LEFT + Rectangle.RIGHT);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(
                Integer.toString(debDet.getQuantity()) + " "
                        + debDet.getGoodsReceiptRow().getInventory().getProduct().getUnitOfMeasure().getName(),
                fontSmallRegular));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorder(Rectangle.LEFT + Rectangle.RIGHT);
        table.addCell(cell);
        cell = calculateDiscount(debDet.getDiscount(), currencyid);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBorder(Rectangle.LEFT + Rectangle.RIGHT);
        table.addCell(cell);
    }
    for (int i = 0; i < 3; i++) {
        PdfPCell cell1 = new PdfPCell(new Paragraph("", fontSmallRegular));
        cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell1.setBorder(Rectangle.TOP);
        table.addCell(cell1);
    }
    return table;
}