Example usage for com.lowagie.text Rectangle setBackgroundColor

List of usage examples for com.lowagie.text Rectangle setBackgroundColor

Introduction

In this page you can find the example usage for com.lowagie.text Rectangle setBackgroundColor.

Prototype


public void setBackgroundColor(Color backgroundColor) 

Source Link

Document

Sets the backgroundcolor of the rectangle.

Usage

From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java

License:Open Source License

public static boolean exportToFile(WikiPDFContext context, Connection db) throws Exception {

    LOG.debug("exportToFile-> begin");

    // Context Objects
    Wiki wiki = context.getWiki();/*  w w w  .j  a  v a 2 s.  com*/
    Project project = context.getProject();
    File file = context.getFile();
    WikiExportBean exportBean = context.getExportBean();

    // Determine the content to parse
    String content = wiki.getContent();
    if (content == null) {
        return false;
    }

    // Create a pdf
    Document document = new Document(PageSize.LETTER);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

    // Meta data
    document.addTitle(project.getTitle());
    document.addSubject(wiki.getSubject());
    document.addCreator("Concursive ConcourseConnect");
    document.addAuthor("Wiki Contributor");
    //writer.setPageEvent(new PageNumbersWatermark());

    if (!exportBean.getIncludeTitle()) {
        boolean hasTitle = StringUtils.hasText(wiki.getSubject());
        HeaderFooter pageFooter = new HeaderFooter(
                new Phrase(project.getTitle() + (hasTitle ? ": " + wiki.getSubject() : "") + " - page "),
                new Phrase(""));
        pageFooter.setAlignment(Element.ALIGN_CENTER);
        document.setFooter(pageFooter);
    }

    document.open();

    if (exportBean.getIncludeTitle()) {
        //HeaderFooter pageHeader = new HeaderFooter(new Phrase(project.getTitle()), false);
        //document.setHeader(pageHeader);
        boolean hasTitle = (StringUtils.hasText(wiki.getSubject()));
        HeaderFooter pageFooter = new HeaderFooter(
                new Phrase(project.getTitle() + (hasTitle ? ": " + wiki.getSubject() : "") + " - page "),
                new Phrase(""));
        pageFooter.setAlignment(Element.ALIGN_CENTER);
        document.setFooter(pageFooter);

        // Draw a title page
        Rectangle rectangle = new Rectangle(600, 30);
        rectangle.setBackgroundColor(new Color(100, 100, 100));
        LOG.debug("document.add(rectangle)");
        document.add(rectangle);

        document.add(new Paragraph(project.getTitle(), titleFont));
        if (!"".equals(wiki.getSubject())) {
            document.add(new Paragraph(wiki.getSubject(), titleFont));
        }
        document.add(Chunk.NEWLINE);
        document.add(new Paragraph("Last Modified: " + wiki.getModified(), titleSmallFont));
        document.newPage();
    }

    ArrayList<Integer> wikiListDone = new ArrayList<Integer>();

    appendWiki(context, context.getWiki(), document, db, wikiListDone);
    // Close everything
    document.close();
    writer.close();
    LOG.debug("exportToFile-> finished");
    return true;
}

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

License:Open Source License

private ByteArrayOutputStream getPdfData(JSONArray colHeader, JSONArray fieldList, JSONArray store,
        HttpServletRequest request) throws ServiceException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*  w  ww. j av a 2 s.  co  m*/
        config = new com.krawler.utils.json.base.JSONObject(request.getParameter("config"));
        String colHeaderArrStr[] = new String[colHeader.length()];
        for (int i = 0; i < colHeader.length(); i++)
            colHeaderArrStr[i] = colHeader.get(i).toString();
        String dataIndexArrStr[] = new String[fieldList.length()];
        for (int i = 0; i < fieldList.length(); i++)
            dataIndexArrStr[i] = fieldList.get(i).toString();

        Document document = null;
        if (config.getBoolean("landscape")) {
            Rectangle recPage = new Rectangle(PageSize.A4.rotate());
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
        } else {
            Rectangle recPage = new Rectangle(PageSize.A4);
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
        }

        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new EndPage());
        document.open();
        if (config.getBoolean("showLogo")) {
            getCompanyDetails(request);
            addComponyLogo(document, request);
        }
        //            int tC = Integer.parseInt(config.getString("textColor"), 16);
        //            JSONArray myStore=(JSONArray)config.get("store");

        addTitleSubtitle(document);
        com.krawler.utils.json.base.JSONObject colWidth = config.getJSONObject("colWidth");
        JSONArray widths = colWidth.getJSONArray("data");
        ArrayList arr = new ArrayList();
        float[] f = new float[widths.length()];
        float totalwid = 0;
        int counter = 0;

        if (widths.length() != 0) {
            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);
                }
            }

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

            while (totalwid < docwidth && counter < f.length) {
                totalwid += f[counter];
                counter++;
            }
            if (totalwid > docwidth) {
                counter--;
                counter--;
            }

            showColumns = counter;
        }

        addTable(0, showColumns, 0, store.length(), store, dataIndexArrStr, colHeaderArrStr, document);
        document.close();
        writer.close();
        baos.close();
    } catch (ConfigurationException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
    } catch (DocumentException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
    } catch (JSONException e) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
    }
    return baos;
}

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;/*from   w w w .  j  a v a  2  s . c om*/
    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.ExportProjectSummaryServlet.java

License:Open Source License

private ByteArrayOutputStream getPdfData(JSONArray data, JSONArray res, HttpServletRequest request)
        throws ServiceException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  w  w  w .  ja  v a  2 s.  c om*/
        String[] colHeader = getColHeader();
        String[] colIndex = getColIndexes();
        String[] val = getColValues(colHeader, data);
        String[] resources = getResourcesColHeader(res, data);
        String[] mainHeader = { "Dates", "Duration", "Work", "Cost", "Tasks", "Resources" };
        Document document = null;
        if (landscape) {
            Rectangle recPage = new Rectangle(PageSize.A4.rotate());
            recPage.setBackgroundColor(new java.awt.Color(255, 255, 255));
            document = new Document(recPage, 15, 15, 30, 30);
        } else {
            Rectangle recPage = new Rectangle(PageSize.A4);
            recPage.setBackgroundColor(new java.awt.Color(255, 255, 255));
            document = new Document(recPage, 15, 15, 30, 30);
        }
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new EndPage());
        document.open();
        if (showLogo) {
            getCompanyDetails(request);
            addComponyLogo(document, request);
        }
        addTitleSubtitle(document);
        addTable(data, resources, colIndex, colHeader, mainHeader, val, document);
        document.close();
        writer.close();
        baos.close();
    } catch (ConfigurationException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
    } catch (DocumentException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
    } catch (JSONException e) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
    } catch (Exception e) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
    }
    return baos;
}

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

License:Open Source License

private ByteArrayOutputStream getPdfData(JSONArray gridmap, HttpServletRequest request, Session session)
        throws ServiceException, SessionExpiredException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = null;/*from  w w w . j ava  2  s .com*/
    try {
        String colHeader = "";
        String colHeaderFinal = "";
        String fieldListFinal = "";
        String fieldList = "";
        String width = "";
        String align = "";
        String alignFinal = "";
        String widthFinal = "";
        String colHeaderArrStr[] = null;
        String dataIndexArrStr[] = null;
        String widthArrStr[] = null;
        String alignArrStr[] = null;
        String htmlCode = "";
        String advStr = "";
        int strLength = 0;
        float totalWidth = 0;

        config = new com.krawler.utils.json.base.JSONObject(request.getParameter("config"));
        String tmpTitle = config.getString("title");
        Document document = null;
        Rectangle rec = null;
        if (config.getBoolean("landscape")) {
            Rectangle recPage = new Rectangle(PageSize.A4.rotate());
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        } else {
            Rectangle recPage = new Rectangle(PageSize.A4);
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        }

        writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new EndPage());
        document.open();
        if (config.getBoolean("showLogo")) {
            addComponyLogo(document, request);
        }

        addTitleSubtitle(document);

        if (gridmap != null) {
            for (int i = 0; i < gridmap.length(); i++) {
                JSONObject temp = gridmap.getJSONObject(i);
                colHeader += temp.getString("title");
                if (colHeader.indexOf("*") != -1)
                    colHeader = colHeader.substring(0, colHeader.indexOf("*") - 1) + ",";
                else
                    colHeader += ",";
                fieldList += temp.getString("header") + ",";
                if (!config.getBoolean("landscape")) {
                    int totalWidth1 = (int) ((totalWidth / gridmap.length()) - 5.00);
                    width += "" + totalWidth1 + ","; //resize according to page view[potrait]
                } else
                    width += temp.getString("width") + ",";
                if (temp.getString("align").equals("")) {
                    align += "none" + ",";
                } else {
                    align += temp.getString("align") + ",";
                }
            }
            strLength = colHeader.length() - 1;
            colHeaderFinal = colHeader.substring(0, strLength);
            strLength = fieldList.length() - 1;
            fieldListFinal = fieldList.substring(0, strLength);
            strLength = width.length() - 1;
            widthFinal = width.substring(0, strLength);
            strLength = align.length() - 1;
            alignFinal = align.substring(0, strLength);
            colHeaderArrStr = colHeaderFinal.split(",");
            dataIndexArrStr = fieldListFinal.split(",");
            widthArrStr = widthFinal.split(",");
            alignArrStr = alignFinal.split(",");
        } else {
            fieldList = request.getParameter("header");
            colHeader = request.getParameter("title");
            width = request.getParameter("width");
            align = request.getParameter("align");
            colHeaderArrStr = colHeader.split(",");
            dataIndexArrStr = fieldList.split(",");
            widthArrStr = width.split(",");
            alignArrStr = align.split(",");
        }

        JSONObject obj = null;
        obj = getReport(request, session);
        JSONArray store = obj.getJSONArray("data");

        addTable(0, colHeaderArrStr.length, 0, store.length(), store, dataIndexArrStr, colHeaderArrStr,
                widthArrStr, alignArrStr, document, request, session);

        document.close();

    } catch (ConfigurationException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
    } catch (DocumentException ex) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
    } catch (JSONException e) {
        throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
    } finally {
        writer.close();
    }
    return baos;
}

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

License:Open Source License

public ByteArrayOutputStream getInvoicePdfData(HttpServletRequest request, JSONObject obj,
        Map<String, Object> DataInfo, Company com, DateFormat formatter, String currencyid,
        JSONArray productDetails, int mode, String letterHead, String preText, String postText)
        throws ServiceException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = null;//from w  w  w . j a v  a  2s  . co m
    Document document = null;
    try {
        config = new com.krawler.utils.json.base.JSONObject(DataInfo.get("config").toString());
        String baseUrl = URLUtil.getRequestPageURL(request, UnprotectedLoginPageFull);
        Rectangle rec = null;
        if (config.getBoolean("landscape")) {
            Rectangle recPage = new Rectangle(PageSize.A4.rotate());
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
        } else {
            Rectangle recPage = new Rectangle(PageSize.A4);
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
        }
        writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new EndPage());
        document.open();
        if (config.getBoolean("showLogo")) {
            addComponyLogo(document, request);
        }

        if (!StringUtil.isNullOrEmpty(letterHead)) {
            PdfPTable letterHeadTable = new PdfPTable(1);
            getHtmlCell(letterHead, letterHeadTable, baseUrl);
            letterHeadTable.setHorizontalAlignment(Element.ALIGN_LEFT);
            document.add(letterHeadTable);
        }

        addTitleSubtitle(document);
        createInvoicePdf(document, mode, DataInfo, com, formatter, currencyid, productDetails, preText,
                postText, baseUrl);
    } catch (DocumentException ex) {
        errorMsg = ex.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getInvoicePdfData", ex);
    } catch (JSONException e) {
        errorMsg = e.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getInvoicePdfData", e);
    } catch (Exception e) {
        e.printStackTrace();
        errorMsg = e.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getInvoicePdfData", e);
    } finally {
        if (document != null) {
            document.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    return baos;
}

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

License:Open Source License

public ByteArrayOutputStream getPdfData(JSONArray gridmap, HttpServletRequest request, JSONObject obj)
        throws ServiceException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = null;/*  w ww . j av  a  2  s . c  o  m*/
    Document document = null;
    try {
        String colHeader = "";
        String colHeaderFinal = "";
        String fieldListFinal = "";
        String fieldList = "";
        String width = "";
        String align = "";
        String xtype = "";
        String alignFinal = "";
        String xtypeFinal = "";
        String widthFinal = "";
        String colHeaderArrStr[] = null;
        String dataIndexArrStr[] = null;
        String widthArrStr[] = null;
        String alignArrStr[] = null;
        String xtypeArrStr[] = null;
        String htmlCode = "";
        String advStr = "";
        int strLength = 0;
        float totalWidth = 0;

        config = new com.krawler.utils.json.base.JSONObject(request.getParameter("config"));
        if (request.getParameter("searchJson") != null && !request.getParameter("searchJson").equals("")) {
            JSONObject json = new JSONObject(request.getParameter("searchJson"));
            JSONArray advSearch = json.getJSONArray("root");
            for (int i = 0; i < advSearch.length(); i++) {
                JSONObject key = advSearch.getJSONObject(i);
                String value = "";
                String name = key.getString("columnheader");
                name = URLDecoder.decode(name);
                name.trim();
                if (name.contains("*")) {
                    name = name.substring(0, name.indexOf("*") - 1);
                }
                if (name.contains("(") && name.charAt(name.indexOf("(") + 1) == '&') {
                    htmlCode = name.substring(name.indexOf("(") + 3, name.length() - 2);
                    char temp = (char) Integer.parseInt(htmlCode, 10);
                    htmlCode = Character.toString(temp);
                    if (htmlCode.equals("$")) {
                        String currencyid = sessionHandlerImpl.getCurrencyID(request);
                        String currency = currencyRender(key.getString("combosearch"), currencyid);
                        name = name.substring(0, name.indexOf("(") - 1);
                        name = name + "(" + htmlCode + ")";
                        value = currency;
                    } else {
                        name = name.substring(0, name.indexOf("(") - 1);
                        value = name + " " + htmlCode;
                    }
                } else {
                    value = key.getString("combosearch");
                }
                advStr += name + " : " + value + ",";
            }
            advStr = advStr.substring(0, advStr.length() - 1);
            config.remove("subtitles");
            config.put("subtitles", "Filtered By: " + advStr);
        }
        if (request.getParameter("frm") != null && !request.getParameter("frm").equals("")) {
            KWLDateFormat dateFormat = (KWLDateFormat) hibernateTemplate.load(KWLDateFormat.class,
                    sessionHandlerImplObj.getDateFormatID(request));
            String prefDate = dateFormat.getJavaForm();
            Date from = new Date(Long.parseLong(request.getParameter("frm")));
            Date to = new Date(Long.parseLong(request.getParameter("to")));
            config.remove("subtitles");
            String timeFormatId = sessionHandlerImplObj.getUserTimeFormat(request);
            String timeZoneDiff = sessionHandlerImplObj.getTimeZoneDifference(request);
            config.put("subtitles", "Filtered By: From : "
                    + authHandler.getPrefDateFormatter(timeFormatId, timeZoneDiff, prefDate).format(from)
                    + " To : "
                    + authHandler.getPrefDateFormatter(timeFormatId, timeZoneDiff, prefDate).format(to));
        }

        Rectangle rec = null;
        if (config.getBoolean("landscape")) {
            Rectangle recPage = new Rectangle(PageSize.A4.rotate());
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        } else {
            Rectangle recPage = new Rectangle(PageSize.A4);
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        }

        writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new EndPage());
        document.open();
        if (config.getBoolean("showLogo")) {
            addComponyLogo(document, request);
        }

        addTitleSubtitle(document);

        if (gridmap != null) {
            for (int i = 0; i < gridmap.length(); i++) {
                JSONObject temp = gridmap.getJSONObject(i);
                colHeader += URLDecoder.decode(temp.getString("title"), "utf-8");
                if (colHeader.indexOf("*") != -1) {
                    colHeader = colHeader.substring(0, colHeader.indexOf("*") - 1) + ",";
                } else {
                    colHeader += ",";
                }
                fieldList += temp.getString("header").replace("$$", "#") + ",";// handled case for custom report. Because dataindex field have "#" symbol and while exporting data URL will break if having # symbol. So replaced # with $$ at JS side and reverted this change at Java side
                if (!config.getBoolean("landscape")) {
                    int totalWidth1 = (int) ((totalWidth / gridmap.length()) - 5.00);
                    width += "" + totalWidth1 + ","; //resize according to page view[potrait]
                } else {
                    width += temp.getString("width") + ",";
                }
                if (temp.optString("align").equals("")) {
                    align += "none" + ",";
                } else {
                    align += temp.getString("align") + ",";
                }
                if (temp.optString("xtype").equals("")) {
                    xtype += "none" + ",";
                } else {
                    xtype += temp.getString("xtype") + ",";
                }
            }
            strLength = colHeader.length() - 1;
            colHeaderFinal = colHeader.substring(0, strLength);
            strLength = fieldList.length() - 1;
            fieldListFinal = fieldList.substring(0, strLength);
            strLength = width.length() - 1;
            widthFinal = width.substring(0, strLength);
            strLength = align.length() - 1;
            alignFinal = align.substring(0, strLength);
            strLength = xtype.length() - 1;
            xtypeFinal = xtype.substring(0, strLength);
            colHeaderArrStr = colHeaderFinal.split(",");
            dataIndexArrStr = fieldListFinal.split(",");
            widthArrStr = widthFinal.split(",");
            alignArrStr = alignFinal.split(",");
            xtypeArrStr = xtypeFinal.split(",");
        } else {
            fieldList = request.getParameter("header");
            colHeader = URLDecoder.decode(request.getParameter("title"));
            width = request.getParameter("width");
            align = request.getParameter("align");
            xtype = request.getParameter("xtype");
            colHeaderArrStr = colHeader.split(",");
            dataIndexArrStr = fieldList.split(",");
            widthArrStr = width.split(",");
            alignArrStr = align.split(",");
            xtypeArrStr = xtype.split(",");
        }

        JSONArray store = null;
        if (obj.isNull("coldata")) {
            store = obj.getJSONArray("data");
        } else {
            store = obj.getJSONArray("coldata");
        }
        addTable(0, colHeaderArrStr.length, 0, store.length(), store, dataIndexArrStr, colHeaderArrStr,
                widthArrStr, alignArrStr, xtypeArrStr, document);

    } catch (DocumentException ex) {
        errorMsg = ex.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getPdfData", ex);
    } catch (JSONException e) {
        errorMsg = e.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getPdfData", e);
    } catch (Exception e) {
        errorMsg = e.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getPdfData", e);
    } finally {
        if (document != null) {
            document.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    return baos;
}

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

License:Open Source License

public ByteArrayOutputStream getPdfData(JSONArray gridmap, String configStr, String titleStr, String headerStr,
        String widthStr, String alignStr, String xtypeStr, JSONObject obj) throws ServiceException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = null;//  www.ja v  a  2s  .com
    Document document = null;
    try {
        String colHeader = "";
        String colHeaderFinal = "";
        String fieldListFinal = "";
        String fieldList = "";
        String width = "";
        String align = "";
        String xtype = "";
        String alignFinal = "";
        String xtypeFinal = "";
        String widthFinal = "";
        String colHeaderArrStr[] = null;
        String dataIndexArrStr[] = null;
        String widthArrStr[] = null;
        String alignArrStr[] = null;
        String xtypeArrStr[] = null;
        String htmlCode = "";
        String advStr = "";
        int strLength = 0;
        float totalWidth = 0;

        config = new com.krawler.utils.json.base.JSONObject(configStr);
        Rectangle rec = null;
        if (config.getBoolean("landscape")) {
            Rectangle recPage = new Rectangle(PageSize.A4.rotate());
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        } else {
            Rectangle recPage = new Rectangle(PageSize.A4);
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        }

        writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new EndPage());
        document.open();
        //            if (config.getBoolean("showLogo")) {
        //                addComponyLogo(document, request);
        //            }

        addTitleSubtitle(document);

        if (gridmap != null) {
            for (int i = 0; i < gridmap.length(); i++) {
                JSONObject temp = gridmap.getJSONObject(i);
                colHeader += URLDecoder.decode(temp.getString("title"), "utf-8");
                if (colHeader.indexOf("*") != -1) {
                    colHeader = colHeader.substring(0, colHeader.indexOf("*") - 1) + ",";
                } else {
                    colHeader += ",";
                }
                fieldList += temp.getString("header") + ",";
                if (!config.getBoolean("landscape")) {
                    int totalWidth1 = (int) ((totalWidth / gridmap.length()) - 5.00);
                    width += "" + totalWidth1 + ","; //resize according to page view[potrait]
                } else {
                    width += temp.getString("width") + ",";
                }
                if (temp.getString("align").equals("")) {
                    align += "none" + ",";
                } else {
                    align += temp.getString("align") + ",";
                }
                if (temp.getString("xtype").equals("")) {
                    xtype += "none" + ",";
                } else {
                    xtype += temp.getString("xtype") + ",";
                }
            }
            strLength = colHeader.length() - 1;
            colHeaderFinal = colHeader.substring(0, strLength);
            strLength = fieldList.length() - 1;
            fieldListFinal = fieldList.substring(0, strLength);
            strLength = width.length() - 1;
            widthFinal = width.substring(0, strLength);
            strLength = align.length() - 1;
            alignFinal = align.substring(0, strLength);
            xtypeFinal = xtype.substring(0, strLength);
            colHeaderArrStr = colHeaderFinal.split(",");
            dataIndexArrStr = fieldListFinal.split(",");
            widthArrStr = widthFinal.split(",");
            alignArrStr = alignFinal.split(",");
            xtypeArrStr = xtypeFinal.split(",");
        } else {
            fieldList = headerStr;
            colHeader = URLDecoder.decode(titleStr);
            width = widthStr;
            align = alignStr;
            xtype = xtypeStr;
            colHeaderArrStr = colHeader.split(",");
            dataIndexArrStr = fieldList.split(",");
            widthArrStr = width.split(",");
            alignArrStr = align.split(",");
            xtypeArrStr = xtype.split(",");
        }

        JSONArray store = null;
        if (obj.isNull("coldata")) {
            store = obj.getJSONArray("data");
        } else {
            store = obj.getJSONArray("coldata");
        }
        addTable(0, colHeaderArrStr.length, 0, store.length(), store, dataIndexArrStr, colHeaderArrStr,
                widthArrStr, alignArrStr, xtypeArrStr, document);

    } catch (DocumentException ex) {
        errorMsg = ex.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getPdfData", ex);
    } catch (JSONException e) {
        errorMsg = e.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getPdfData", e);
    } catch (Exception e) {
        errorMsg = e.getMessage();
        throw ServiceException.FAILURE("exportDAOImpl.getPdfData", e);
    } finally {
        if (document != null) {
            document.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    return baos;
}

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

License:Open Source License

public ByteArrayOutputStream getPdfData(JSONObject grid, HttpServletRequest request, JSONObject obj)
        throws ServiceException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = null;/*ww  w  . j a  v  a  2s .c  o  m*/
    Document document = null;
    try {
        JSONArray gridmap = grid == null ? null : grid.getJSONArray("data");
        String colHeader = "";
        String colHeaderFinal = "";
        String fieldListFinal = "";
        String fieldList = "";
        String width = "";
        String align = "";
        String alignFinal = "";
        String widthFinal = "";
        String colHeaderArrStr[] = null;
        String dataIndexArrStr[] = null;
        String widthArrStr[] = null;
        String alignArrStr[] = null;
        int strLength = 0;
        float totalWidth = 0;

        config = new com.krawler.utils.json.base.JSONObject(request.getParameter("config"));
        document = null;
        Rectangle rec = null;
        if (config.getBoolean("landscape")) {
            Rectangle recPage = new Rectangle(PageSize.A4.rotate());
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        } else {
            Rectangle recPage = new Rectangle(PageSize.A4);
            recPage.setBackgroundColor(new java.awt.Color(Integer.parseInt(config.getString("bgColor"), 16)));
            document = new Document(recPage, 15, 15, 30, 30);
            rec = document.getPageSize();
            totalWidth = rec.getWidth();
        }

        writer = PdfWriter.getInstance(document, baos);
        writer.setPageEvent(new EndPage());
        document.open();
        if (config.getBoolean("showLogo")) {
            addComponyLogo(document, request);
        }

        addTitleSubtitle(document);

        if (gridmap != null) {
            int givenTotalWidth = 0;
            for (int i = 0; i < gridmap.length(); i++) {
                JSONObject temp = gridmap.getJSONObject(i);
                givenTotalWidth += Integer.parseInt(temp.getString("width"));
            }
            double widthRatio = 1;
            if (givenTotalWidth > (totalWidth - 40.00)) {
                widthRatio = (totalWidth - 40.00) / givenTotalWidth; // 40.00 is left + right + table margin [15+15+10] margins of documents
            }
            for (int i = 0; i < gridmap.length(); i++) {
                JSONObject temp = gridmap.getJSONObject(i);
                colHeader += StringUtil.serverHTMLStripper(temp.getString("title"));
                if (colHeader.indexOf("*") != -1) {
                    colHeader = colHeader.substring(0, colHeader.indexOf("*") - 1) + ",";
                } else {
                    colHeader += ",";
                }
                fieldList += temp.getString("header") + ",";
                if (!config.getBoolean("landscape")) {
                    int totalWidth1 = (int) ((totalWidth / gridmap.length()) - 5.00);
                    width += "" + totalWidth1 + ","; //resize according to page view[potrait]
                } else {
                    double adjustedWidth = (Integer.parseInt(temp.getString("width")) * widthRatio);
                    width += ((int) Math.floor(adjustedWidth)) + ",";
                }
                if (temp.getString("align").equals("")) {
                    align += "none" + ",";
                } else {
                    align += temp.getString("align") + ",";
                }
            }
            strLength = colHeader.length() - 1;
            colHeaderFinal = colHeader.substring(0, strLength);
            strLength = fieldList.length() - 1;
            fieldListFinal = fieldList.substring(0, strLength);
            strLength = width.length() - 1;
            widthFinal = width.substring(0, strLength);
            strLength = align.length() - 1;
            alignFinal = align.substring(0, strLength);
            colHeaderArrStr = colHeaderFinal.split(",");
            dataIndexArrStr = fieldListFinal.split(",");
            widthArrStr = widthFinal.split(",");
            alignArrStr = alignFinal.split(",");
        } else {
            fieldList = request.getParameter("header");
            colHeader = request.getParameter("title");
            width = request.getParameter("width");
            align = request.getParameter("align");
            colHeaderArrStr = colHeader.split(",");
            dataIndexArrStr = fieldList.split(",");
            widthArrStr = width.split(",");
            alignArrStr = align.split(",");
        }

        JSONArray store = obj.getJSONArray("data");

        if (grid != null && grid.has("groupdata")) {
            JSONObject groupingConfig = grid.getJSONObject("groupdata");
            addGroupableTable(groupingConfig, 0, colHeaderArrStr.length, 0, store.length(), store,
                    dataIndexArrStr, colHeaderArrStr, widthArrStr, alignArrStr, document, request);
        } else {
            addTable(0, colHeaderArrStr.length, 0, store.length(), store, dataIndexArrStr, colHeaderArrStr,
                    widthArrStr, alignArrStr, document, request);
        }

    } catch (Exception e) {
        throw ServiceException.FAILURE("exportMPXDAOImpl.getPdfData", e);
    } finally {
        if (document != null) {
            document.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
    return baos;
}

From source file:com.pureinfo.srm.patent.action.PatentPrintPdfAction.java

License:Open Source License

/**
 * @see com.pureinfo.ark.interaction.ActionBase#executeAction()
 *///from w ww  .  j av  a 2  s .com
public ActionForward executeAction() throws PureException {
    int nYear = request.getRequiredInt("year", "");

    Rectangle rectPageSize = new Rectangle(PageSize.A4);
    rectPageSize.setBackgroundColor(Color.WHITE);
    rectPageSize.setBorderColor(Color.BLACK);
    rectPageSize = rectPageSize.rotate();
    Document doc = new Document(rectPageSize, 10, 10, 10, 10);
    doc.addTitle(nYear + "");
    doc.addAuthor("PureInfo");
    try {
        BaseFont bfontTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontTitle = new Font(bfontTitle, 18, Font.NORMAL);
        Paragraph paraTitle = new Paragraph(nYear + "",
                fontTitle);
        paraTitle.setAlignment(ElementTags.ALIGN_CENTER);

        BaseFont bfontContent = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontContent = new Font(bfontContent, 10, Font.NORMAL);

        FileFactory fileFactory = FileFactory.getInstance();
        String sPath = fileFactory.lookupPathConfigByFlag(FileFactory.FLAG_DOWNLOADTEMP, true).getLocalPath();

        FileUtil.insurePathExists(sPath);
        PdfWriter.getInstance(doc, new FileOutputStream(new File(sPath, "patent.pdf")));

        doc.open();
        doc.add(paraTitle);
        doc.add(new Paragraph("   "));

        String[] arrTitles = new String[] { "", "", "", "", "", "",
                "", "", "" };
        PdfPTable pTable = new PdfPTable(arrTitles.length);
        pTable.setWidths(new int[] { 5, 10, 9, 9, 20, 15, 12, 10, 10 });

        for (int i = 0; i < arrTitles.length; i++) {
            PdfPCell pCell = new PdfPCell();
            Paragraph para = new Paragraph(arrTitles[i], fontContent);
            para.setAlignment(ElementTags.ALIGN_CENTER);

            pCell.addElement(para);
            pTable.addCell(pCell);
        }

        /**
         * 
         */
        IPatentMgr mgr = (IPatentMgr) ArkContentHelper.getContentMgrOf(Patent.class);
        List patents = mgr.findAllAuthorizedOf(nYear);
        int i = 0;
        for (Iterator iter = patents.iterator(); iter.hasNext(); i++) {
            Patent patent = (Patent) iter.next();
            this.addPatentCell(pTable, String.valueOf(i + 1), fontContent);
            this.addPatentCell(pTable, patent.getPatentSid(), fontContent);
            this.addPatentCell(pTable, ForceConstants.DATE_FORMAT.format(patent.getApplyDate()), fontContent);
            this.addPatentCell(pTable, patent.getWarrantDate() == null ? ""
                    : ForceConstants.DATE_FORMAT.format(patent.getWarrantDate()), fontContent);
            this.addPatentCell(pTable, patent.getName(), fontContent);
            this.addPatentCell(pTable, patent.getAllAuthosName(), fontContent);
            this.addPatentCell(pTable, patent.getRightPerson(), fontContent);
            this.addPatentCell(pTable, getCollegeName(patent), fontContent);
            this.addPatentCell(pTable, patent.getPatentTypeName(), fontContent);
        }

        doc.add(pTable);
        doc.close();

    } catch (DocumentException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace(System.err);
    } catch (IOException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace(System.err);
    }

    List list = new ArrayList();
    list.add(new Pair("/download/patent.pdf", ""));
    request.setAttribute("forward", list);
    return mapping.findForward("success");
}