Example usage for com.lowagie.text Document add

List of usage examples for com.lowagie.text Document add

Introduction

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

Prototype


public boolean add(Element element) throws DocumentException 

Source Link

Document

Adds an Element to the Document.

Usage

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

License:Open Source License

public void createPDFFile(Connection conn, HttpServletRequest request, HttpServletResponse response)
        throws ConfigurationException {
    String projid = request.getParameter("projectid");
    try {/* ww w . j  av a 2  s .  c  o m*/
        String userid = AuthHandler.getUserid(request);
        String companyid = AuthHandler.getCompanyid(request);
        String tasks = projdb.getProjectTasks(conn, projid, userid, companyid, 0, -1, true);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Document document = new Document(PageSize.A4);
        document.setPageSize(PageSize.A4.rotate());
        PdfWriter writer = PdfWriter.getInstance(document, os);
        setHeaderFooter(document, request.getParameter("header"));
        JSONObject jobj = new JSONObject(request.getParameter("options"));
        JSONArray jarr = jobj.getJSONArray("data");
        ArrayList tabCols = new ArrayList();
        ArrayList tabArr = createTables(jarr, tabCols, document);//new ArrayList();
        writer.setPageEvent(new EndPage());

        HashMap taskIdIndex = new HashMap();
        JSONArray taskArr = new com.krawler.utils.json.base.JSONObject(tasks).getJSONArray("data");
        for (int c = 0; c < taskArr.length(); c++) {
            com.krawler.utils.json.base.JSONObject temp = taskArr.getJSONObject(c);
            taskIdIndex.put(temp.getString("taskid"), temp.getInt("taskindex"));
        }
        taskIdIndex.put("0", 0);
        for (int c = 0; c < taskArr.length(); c++) {
            com.krawler.utils.json.base.JSONObject temp = taskArr.getJSONObject(c);
            String pred = "";
            if (!StringUtil.isNullOrEmpty(temp.getString("predecessor"))) {
                String[] p = temp.getString("predecessor").split(",");
                for (int i = 0; i < p.length; i++) {
                    pred += taskIdIndex.get(p[i]).toString() + ",";
                }
                pred = pred.substring(0, (pred.length() - 1));
            }
            String taskResourceNames = projdb.getTaskResourcesNames(conn, temp.getString("taskid"), projid);
            for (int i = 0; i < tabArr.size(); i++) {
                int lvl = 0;
                if (i == 0) {
                    lvl = temp.getInt("level");
                }
                String[] colArr = (String[]) tabCols.get(i);
                String[] values = getPDFCellOfRec(colArr, temp, pred, taskResourceNames);
                Font fnt = new Font();
                if (temp.getBoolean("isparent")) {
                    fnt.setStyle(Font.BOLD);
                } else {
                    fnt.setStyle(Font.NORMAL);
                }
                addPdfRowToTable(values, (PdfPTable) tabArr.get(i), fnt, lvl);
            }
        }
        document.open();
        getCompanyDetails(request);
        addComponyLogo(document);
        for (int i = 0; i < tabArr.size(); i++) {
            PdfPTable temp = (PdfPTable) tabArr.get(i);
            temp.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
            document.add(temp);
            document.newPage();
        }
        document.close();
        os.close();
        String fname = request.getParameter("filename");
        os.close();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fname + ".pdf\"");
        response.setContentType("application/octet-stream");
        response.setContentLength(os.size());
        response.getOutputStream().write(os.toByteArray());
        response.getOutputStream().flush();
        String type = "[PDF]";
        AddToAuditTrail(conn, request, projid, type);
        conn.commit();
    } catch (ServiceException ex) {
        Logger.getLogger(ExportMPXServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ExportMPXServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ExportMPXServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JSONException e) {
        Logger.getLogger(ExportMPXServlet.class.getName()).log(Level.SEVERE, null, e);
    } catch (SessionExpiredException e) {
        Logger.getLogger(ExportMPXServlet.class.getName()).log(Level.SEVERE, null, e);
    }
}

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

License:Open Source License

private int addTable(int stcol, int stpcol, int strow, int stprow, JSONArray store, String[] colwidth2,
        String[] colIndex1, Document document) {
    float[] f = new float[(stpcol - stcol)];
    for (int k = 0; k < f.length; k++)
        f[k] = 20;//from   ww  w .j av  a  2s  . c  om
    PdfPTable table = new PdfPTable(f);
    PdfPCell cell = new PdfPCell(new Paragraph("Agenda Details"));
    cell.setColspan(6);
    table.addCell(cell);
    Font font = FontFactory.getFont("Helvetica", 8, Font.BOLD, Color.BLACK);
    Font font1 = FontFactory.getFont("Helvetica", 8, Font.NORMAL, Color.BLACK);
    Font f1;
    int stpcol1 = 0;
    for (int hcol = stcol; hcol < stpcol; hcol++) {
        PdfPCell h1 = new PdfPCell(new Paragraph(colwidth2[hcol], font)); // new
        // 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);
            }
            f1 = font1;
            for (int col = stcol; col < stpcol; col++) {
                table.addCell(new Paragraph(store.getJSONObject(row).getString(colIndex1[col]), f1));
            }
        }
        document.newPage();
        document.add(table);

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

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

License:Open Source License

private static void addComponyLogo(Document d, HttpServletRequest request)
        throws ConfigurationException, DocumentException {
    PdfPTable table = new PdfPTable(2);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.setWidthPercentage(100);/*  w  ww  .  j a va 2  s.  com*/
    PdfPCell cell = null;
    try {
        imgPath = com.krawler.esp.utils.ConfigReader.getinstance().get("platformURL") + "b/" + companySubDomain
                + "/images/store/?company=true";
        // imgPath="http://192.168.0.141:8080/dp_dev/b/Demo/images/store/?company=true";
        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);
    PdfPCell cell2 = new PdfPCell(new Paragraph(ProjectName.toUpperCase(),
            FontFactory.getFont("Helvetica", 15, Font.BOLD, Color.BLACK)));
    cell2.setBorderWidth(0);
    cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
    table.addCell(cell2);
    d.add(table);
}

From source file:com.krawler.esp.servlets.ExportProjectReportServlet.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 w w.ja  v a2 s. c  om*/
    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], fontRegular));
        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.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.  ja v  a 2 s .c  o m

    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.ExportProjectReportServlet.java

License:Open Source License

private ByteArrayOutputStream exportToPdfTimeline(HttpServletRequest request, String as)
        throws ServiceException {
    JSONObject s = null;//w w  w .j  a va 2  s.  c  o  m
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        config = new com.krawler.utils.json.base.JSONObject(request.getParameter("config"));
        s = new JSONObject(as);
        JSONArray head = s.getJSONArray("columnheader");
        JSONArray store = s.getJSONArray("data");
        String[] colwidth2 = new String[head.length() + 3];
        String[] colnm = { "info", "level", "flag" };
        for (int i = 0; i < colwidth2.length; i++) {
            if (i < 3)
                colwidth2[i] = colnm[i];
            else {
                if (head.getJSONObject(i - 3).has("0"))
                    colwidth2[i] = head.getJSONObject(i - 3).getString("0");
                else
                    colwidth2[i] = head.getJSONObject(i - 3).getString("1");
            }
        }
        int maxlevel = 0;
        for (int k = 0; k < store.length(); k++) {
            for (int j = 0; j < colwidth2.length; j++) {
                if (!store.getJSONObject(k).has(colwidth2[j]))
                    store.getJSONObject(k).put(colwidth2[j], "");
            }
            if (store.getJSONObject(k).getInt("level") > maxlevel)
                maxlevel = store.getJSONObject(k).getInt("level");
        }
        int len = colwidth2.length - 3;
        int p = 0;
        if (len <= 5)
            p = colwidth2.length;
        else
            p = 8;

        Rectangle recPage = new Rectangle(PageSize.A4);
        recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));

        Document document = null;
        if (config.getBoolean("landscape"))
            document = new Document(recPage.rotate(), 15, 15, 30, 30);
        else
            document = new Document(recPage, 15, 15, 30, 30);

        PdfWriter writer = PdfWriter.getInstance(document, os);
        writer.setPageEvent(new EndPage());
        document.open();
        if (config.getBoolean("showLogo")) {
            getCompanyDetails(request);
            addComponyLogo(document, request);
        }
        addTitleSubtitle(document);
        document.add(new Paragraph("\u00a0"));
        addTableTimeLine(3, p, 0, store.length(), store, colwidth2, maxlevel, document);
        document.close();
        writer.close();
        os.close();
    } catch (ConfigurationException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.exportToPdfTimeline", ex);
    } catch (IOException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.exportToPdfTimeline", ex);
    } catch (DocumentException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.exportToPdfTimeline", ex);
    } catch (JSONException e) {
        throw ServiceException.FAILURE("ExportProjectReport.exportToPdfTimeline", e);
    }
    return os;
}

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

License:Open Source License

private int addTableTimeLine(int stcol, int stpcol, int strow, int stprow, JSONArray store, String[] colwidth2,
        int maxlevel, Document document) throws JSONException, DocumentException {
    float[] f = new float[(stpcol - stcol) + 1];
    for (int k = 0; k < f.length; k++) {
        f[k] = 20;/*from w  w w . ja  v  a 2s  . c o  m*/
    }
    f[0] = f[0] + 10 * maxlevel;
    PdfPTable table = new PdfPTable(f);

    java.awt.Color tColor = new Color(Integer.parseInt(config.getString("textColor"), 16));

    Font font = FontFactory.getFont("Helvetica", 8, Font.BOLD, tColor);
    Font font1 = FontFactory.getFont("Helvetica", 8, Font.NORMAL, tColor);
    Font f1;

    table.setWidthPercentage(90);
    PdfPCell h2 = new PdfPCell(new Paragraph("Name", font)); // new
    if (config.getBoolean("gridBorder"))
        h2.setBorder(PdfPCell.BOX);
    else
        h2.setBorder(0);
    h2.setPadding(2);
    //                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);
        if (config.getBoolean("gridBorder"))
            h1.setBorder(PdfPCell.BOX);
        else
            h1.setBorder(0);
        h1.setPadding(2);
        // Paragraph(colwidth2[hcol],font);
        table.addCell(h1);
    }
    table.setHeaderRows(1);

    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);
        //                Color bColor = Color.decode("DDDDDD");

        if (config.getBoolean("gridBorder")) {
            if (store.getJSONObject(row).getBoolean("flag")) {
                h2.setBackgroundColor(new Color(0xEEEEEE));
                h2.setBorder(PdfPCell.LEFT | PdfPCell.BOTTOM);
            } else
                h2.setBorder(PdfPCell.BOX);
        } else {
            h2.setBorder(0);
        }
        h2.setPadding(2);
        h2.setBorderColor(Color.GRAY);
        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 = new Paragraph(store.getJSONObject(row).getString(colwidth2[col]), f1);
            PdfPCell h1 = new PdfPCell(para);
            //                    h1.setBorder(0);
            //                    h1.setPadding(1);

            h1.setMinimumHeight(15);
            h1.setHorizontalAlignment(Element.ALIGN_RIGHT);
            h1.setVerticalAlignment(Element.ALIGN_CENTER);
            if (config.getBoolean("gridBorder")) {
                if (store.getJSONObject(row).getBoolean("flag")) {
                    h1.setBorder(PdfPCell.BOTTOM);
                    h1.setBackgroundColor(new Color(0xEEEEEE));
                    if (col == stpcol - 1)
                        h1.setBorder(PdfPCell.BOTTOM | PdfPCell.RIGHT);
                } else
                    h1.setBorder(PdfPCell.BOX);
            } else {
                h1.setBorder(0);
            }
            h1.setPadding(2);
            h1.setBorderColor(Color.GRAY);
            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;
        addTableTimeLine(stpcol, stpcol1, strow, stprow, store, colwidth2, maxlevel, document);
    }

    return stpcol;
}

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

License:Open Source License

private static void addComponyLogo(Document d, HttpServletRequest request)
        throws ConfigurationException, DocumentException {
    PdfPTable table = new PdfPTable(1);
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    table.setWidthPercentage(50);/*from w w w. ja va2s . c o m*/
    PdfPCell cell = null;
    try {
        imgPath = com.krawler.esp.utils.ConfigReader.getinstance().get("platformURL") + "b/" + companySubDomain
                + "/images/store/?company=true";
        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.ExportProjectSummaryServlet.java

License:Open Source License

private static void addTitleSubtitle(Document d) throws DocumentException, JSONException {
    java.awt.Color tColor = new Color(0, 0, 0);
    fontBold.setColor(tColor);//from   w  ww . j av  a2 s. c o m
    fontRegular.setColor(tColor);
    PdfPTable table = new PdfPTable(1);
    table.setHorizontalAlignment(Element.ALIGN_CENTER);
    java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
    java.util.Date today = new java.util.Date();

    table.setWidthPercentage(100);
    table.setSpacingBefore(6);

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

    //Sub-title(s)
    cell = new PdfPCell(new Paragraph("(on comparison with " + baseName + ")", fontRegular));
    cell.setBorder(0);
    cell.setBorderWidth(0);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);

    //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(table);
    d.add(line);
}

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  .j a va 2  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);
    }
}