List of usage examples for com.lowagie.text.pdf PdfWriter setPageEvent
public void setPageEvent(PdfPageEvent event)
PdfPageEvent
for this document. 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; Document document = null;//from www . j a v a2s .c o m 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; Document document = null;//from w w w.jav a 2 s .c o m 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.krawler.spring.hrms.exportreport.exportAppraisalReportPDFDAOImpl.java
License:Open Source License
private ByteArrayOutputStream AppraisalDetail(HttpServletRequest request, Session session, boolean isEmm, String str, String goalstr, String quesansstr) throws JSONException, SessionExpiredException, DocumentException, ServiceException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); String[] colHeader = {/*from ww w .jav a 2s.c o m*/ messageSource.getMessage("hrms.common.employee.name", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.cycle.name", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.start.date", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.end.date", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.total.appraisals", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.common.Updates.NoofAppraisalsSubmitted", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.overall.self.comments", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.overall.appraiser.comments", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.overall.competency.score", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.competencies", null, RequestContextUtils.getLocale(request)) }; String[] colHeader1 = { messageSource.getMessage("hrms.common.employee.name", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.cycle.name", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.start.date", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.end.date", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.total.appraisals", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.common.Updates.NoofAppraisalsSubmitted", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.overall.appraiser.comments", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.overall.competency.score", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.competencies", null, RequestContextUtils.getLocale(request)) }; String[] colHeader2 = { messageSource.getMessage("hrms.common.employee.name", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.cycle.name", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.start.date", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraisal.end.date", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.total.appraisals", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.common.Updates.NoofAppraisalsSubmitted", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.overall.competency.score", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.competencies", null, RequestContextUtils.getLocale(request)) }; String[] dataIndex = { "empname", "appcylename", "appcylestdate", "appcylendate", "totalappraisal", "appraisalsubmitted", "empcomment", "mancom", "manavgwght", "" }; String[] dataIndex1 = { "empname", "appcylename", "appcylestdate", "appcylendate", "totalappraisal", "appraisalsubmitted", "mancom", "manavgwght", "" }; String[] dataIndex2 = { "empname", "appcylename", "appcylestdate", "appcylendate", "totalappraisal", "appraisalsubmitted", "manavgwght", "" }; String[] compHeader = { messageSource.getMessage("hrms.common.name", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.description", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.appraisal.score", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.appraisal.comment", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.competency.score", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.comments", null, RequestContextUtils.getLocale(request)) }; String[] compDataIndex = { "comptename", "comptdesc", "selfcompscore", "selfcomment", "compmanwght" }; String[] compGoalHeader = { messageSource.getMessage("hrms.performance.goals", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.common.assigned.by", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.rating", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.comment", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.rating", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.comments", null, RequestContextUtils.getLocale(request)) }; String[] compGoalDataIndex = { "gname", "assignedby", "gmanrat", "mangoalcomment", "gemprat", "empgoalcomment" }; String[] quesAnsHeader1 = { messageSource.getMessage("hrms.performance.Questions", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.response", null, RequestContextUtils.getLocale(request)) }; String[] quesAnsHeader = { messageSource.getMessage("hrms.performance.Questions", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.response", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.response", null, RequestContextUtils.getLocale(request)) }; String[] quesAnsDataIndex = { hrmsAnonymousAppraisalConstants.question, hrmsAnonymousAppraisalConstants.answer, hrmsAnonymousAppraisalConstants.employeeanswer }; String[] quesAnsDataIndex1 = { hrmsAnonymousAppraisalConstants.question, hrmsAnonymousAppraisalConstants.answer }; String managerComments = ""; String scoreAvg = ""; String companyid = null; PdfWriter writer = null; try { String usID = request.getParameter("userid"); String self = request.getParameter("self"); Boolean removecolumn = false; if (!StringUtil.isNullOrEmpty(self) && self.equals("false")) { quesAnsHeader = quesAnsHeader1; quesAnsDataIndex = quesAnsDataIndex1; colHeader = colHeader1; dataIndex = dataIndex1; removecolumn = true; } if (StringUtil.isNullOrEmpty(usID)) { usID = sessionHandlerImplObj.getUserid(request); } if (StringUtil.isNullOrEmpty(request.getParameter("pdfEmail"))) companyid = sessionHandlerImplObj.getCompanyid(request); else { User u = (User) hibernateTemplate.get(User.class, usID); companyid = u.getCompany().getCompanyID(); } HashMap<String, Object> requestParams = new HashMap<String, Object>(); // companyid = sessionHandlerImplObj.getCompanyid(request); requestParams.put("companyid", companyid); requestParams.put("checklink", "appraisal"); if (!hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { quesAnsHeader = quesAnsHeader1; quesAnsDataIndex = quesAnsDataIndex1; colHeader = colHeader1; removecolumn = true; } requestParams.clear(); requestParams.put("companyid", companyid); requestParams.put("checklink", "overallcomments"); if (!hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { colHeader = colHeader2; dataIndex = dataIndex2; } requestParams.clear(); Document document = new Document(PageSize.A4.rotate(), 25, 25, 25, 25); writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new EndPage()); document.open(); java.awt.Color tColor = new Color(9, 9, 9); //fontSmallBold.setColor(tColor); Paragraph p = new Paragraph(); PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); table.setWidths(new float[] { 55, 75 }); PdfPTable mainTable = new PdfPTable(1); mainTable.setTotalWidth(90); mainTable.setWidthPercentage(100); mainTable.setSpacingBefore(20); PdfPCell headcell = null; headcell = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.performance.appraisal.details", null, RequestContextUtils.getLocale(request)), FontContext.REGULAR_BOLD_HELVETICA, tColor))); headcell.setBackgroundColor(new Color(0xEEEEEE)); headcell.setPadding(5); mainTable.addCell(headcell); document.add(mainTable); // String str=hrmsManager.getAppraisalReport(session, request); JSONObject jobjTemplate = new JSONObject(str); com.krawler.utils.json.base.JSONArray jarr = jobjTemplate.getJSONArray("data"); com.krawler.utils.json.base.JSONArray jarr2 = jarr.getJSONObject(0).getJSONArray("data"); JSONObject jobjAppraisal = new JSONObject(jarr2.getString(0)); jarr = jobjAppraisal.getJSONArray("competencies"); int headlen = colHeader.length; if (jarr.length() < 1) { headlen = headlen - 2; } for (int i = 0; i < headlen; i++) { PdfPCell pcell = new PdfPCell( new Paragraph(fontFamilySelector.process(colHeader[i], FontContext.MEDIUM_BOLD_HELVETICA))); pcell.setBorder(0); if (i == 0) pcell.setPaddingTop(10); pcell.setPaddingLeft(15); pcell.setPaddingBottom(4); pcell.setBorderColor(new Color(0xF2F2F2)); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); table.addCell(pcell); if (!dataIndex[i].equals("mancom")) { pcell = new PdfPCell(new Paragraph(fontFamilySelector.process(!dataIndex[i].equals("") ? !jobjAppraisal.isNull(dataIndex[i]) ? jobjAppraisal.getString(dataIndex[i]) : "" : "", FontContext.SMALL_NORMAL_HELVETICA))); if (i == 0) pcell.setPaddingTop(10); pcell.setBorder(0); pcell.setPaddingLeft(10); pcell.setPaddingBottom(4); pcell.setBorderColor(new Color(0xF2F2F2)); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); table.addCell(pcell); } else { if (!jobjAppraisal.isNull(dataIndex[i])) { JSONArray spl = new JSONArray(jobjAppraisal.getString(dataIndex[i])); String strData = ""; for (int counter = 0; counter < spl.length(); counter++) { strData += spl.getString(counter) + "\n"; } pcell = new PdfPCell(new Paragraph( fontFamilySelector.process(strData, FontContext.SMALL_NORMAL_HELVETICA))); if (i == 0) pcell.setPaddingTop(10); pcell.setBorder(0); pcell.setPaddingLeft(10); pcell.setPaddingBottom(4); pcell.setBorderColor(new Color(0xF2F2F2)); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); table.addCell(pcell); } } } document.add(table); PdfPTable quesansTable = new PdfPTable(quesAnsHeader.length); quesansTable.setWidthPercentage(100); if (removecolumn) { quesansTable.setWidths(new float[] { 40, 40 }); } else { quesansTable.setWidths(new float[] { 40, 40, 40 }); } quesansTable.setSpacingBefore(20); quesansTable.setHeaderRows(1); for (int i = 0; i < quesAnsHeader.length; i++) { PdfPCell pgcell = new PdfPCell(new Paragraph( fontFamilySelector.process(quesAnsHeader[i], FontContext.MEDIUM_BOLD_HELVETICA))); pgcell.setBorder(0); pgcell.setBorder(PdfPCell.BOX); pgcell.setPadding(4); pgcell.setBorderColor(Color.GRAY); pgcell.setHorizontalAlignment(Element.ALIGN_CENTER); quesansTable.addCell(pgcell); } JSONObject quesansjobj = new JSONObject(quesansstr); JSONArray quesansjarr = quesansjobj.getJSONArray("quesans"); JSONObject qajobj = new JSONObject(); for (int i = 0; i < quesansjarr.length(); i++) { qajobj = quesansjarr.getJSONObject(i); for (int k = 0; k < quesAnsHeader.length; k++) { String qatext = !qajobj.isNull(quesAnsDataIndex[k]) ? qajobj.getString(quesAnsDataIndex[k]) : ""; qatext = qatext.replaceAll("~", "\n\n"); qatext = qatext.replaceAll("\n", "<br/>"); StyleSheet st = new StyleSheet(); st.loadTagStyle("body", "face", "HELVETICA"); st.loadTagStyle("body", "size", "1"); st.loadTagStyle("body", "leading", "8,0"); StringReader stringReader = new StringReader(qatext); PdfPCell pcell = new PdfPCell(); ArrayList listStr = HTMLWorker.parseToList(stringReader, st); pcell.setPadding(4); for (int htmlCount = 0; htmlCount < listStr.size(); ++htmlCount) { if (!listStr.get(htmlCount).toString().equals("[]")) pcell.addElement((Element) listStr.get(htmlCount)); } pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_CENTER); quesansTable.addCell(pcell); } } document.add(quesansTable); PdfPTable compTable = new PdfPTable(6); compTable.setWidthPercentage(100); compTable.setWidths(new float[] { 50, 60, 30, 50, 42, 70 }); compTable.setSpacingBefore(20); compTable.setHeaderRows(1); for (int i = 0; i < compHeader.length; i++) { PdfPCell pcell = new PdfPCell(new Paragraph( fontFamilySelector.process(compHeader[i], FontContext.MEDIUM_BOLD_HELVETICA))); pcell.setBorder(0); pcell.setBorder(PdfPCell.BOX); pcell.setPadding(4); pcell.setBorderColor(Color.GRAY); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); compTable.addCell(pcell); } for (int i = 0; i < jarr.length(); i++) { jobjAppraisal = jarr.getJSONObject(i); for (int k = 0; k < compHeader.length; k++) { if (k != compHeader.length - 1) { scoreAvg = ""; if (!jobjAppraisal.isNull(compDataIndex[k]) && compDataIndex[k].equals("compmanwght")) { scoreAvg = jobjAppraisal.getString("nominalRat"); Chunk chunk1 = fontFamilySelector.processChunk( jobjAppraisal.getString(compDataIndex[k]) + "\n\n", FontContext.SMALL_NORMAL_HELVETICA); Chunk chunk2 = null; requestParams.clear(); if (!StringUtil.isNullOrEmpty(request.getParameter("pdfEmail"))) { // User user=(User)session.get(User.class,request.getParameter("userid")); // companyid=user.getCompany().getCompanyID(); requestParams.put("companyid", companyid); requestParams.put("checklink", "modaverage"); if (hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { chunk2 = fontFamilySelector.processChunk("[" + messageSource.getMessage("hrms.performance.mod.avg", null, RequestContextUtils.getLocale(request)) + ": " + scoreAvg + " ]", FontContext.SMALL_NORMAL_HELVETICA); } else { chunk2 = fontFamilySelector.processChunk("[" + messageSource.getMessage("hrms.performance.avg", null, RequestContextUtils.getLocale(request)) + ": " + scoreAvg + " ]", FontContext.SMALL_NORMAL_HELVETICA); } } else { requestParams.put("companyid", companyid); requestParams.put("checklink", "modaverage"); if (hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { chunk2 = fontFamilySelector.processChunk("[" + messageSource.getMessage("hrms.performance.mod.avg", null, RequestContextUtils.getLocale(request)) + ": " + scoreAvg + " ]", FontContext.SMALL_NORMAL_HELVETICA); } else { chunk2 = fontFamilySelector.processChunk("[" + messageSource.getMessage("hrms.performance.avg", null, RequestContextUtils.getLocale(request)) + ": " + scoreAvg + " ]", FontContext.SMALL_NORMAL_HELVETICA); } } Phrase phrase1 = new Phrase(); phrase1.add(chunk1); phrase1.add(chunk2); p = new Paragraph(); p.add(phrase1); } PdfPCell pcell = new PdfPCell(); if (!scoreAvg.equals("")) { pcell = new PdfPCell(new Paragraph(p)); pcell.setPadding(0); pcell.setPaddingTop(2); pcell.setPaddingBottom(4); } else { if (!jobjAppraisal.isNull(compDataIndex[k])) { String htmlStr = jobjAppraisal.getString(compDataIndex[k]); htmlStr = htmlStr.replaceAll("\n", "<br>"); StyleSheet st = new StyleSheet(); st.loadTagStyle("body", "face", "HELVETICA"); st.loadTagStyle("body", "size", "1"); st.loadTagStyle("body", "leading", "8,0"); HTMLWorker worker = new HTMLWorker(document); StringReader stringReader = new StringReader(htmlStr); ArrayList<Element> listStr = HTMLWorker.parseToList(stringReader, st); pcell.setPadding(4); for (int htmlCount = 0; htmlCount < listStr.size(); ++htmlCount) { if (!listStr.get(htmlCount).getChunks().isEmpty()) { pcell.addElement(fontFamilySelector.processElement( listStr.get(htmlCount).getChunks(), FontContext.SMALL_NORMAL_HELVETICA)); } } } else pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( !jobjAppraisal.isNull(compDataIndex[k]) ? StringUtil .serverHTMLStripper(jobjAppraisal.getString(compDataIndex[k])) : "", FontContext.SMALL_NORMAL_HELVETICA))); } pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setHorizontalAlignment( compDataIndex[k].equals("comptename") || compDataIndex[k].equals("comptdesc") || compDataIndex[k].equals("selfcomment") ? Element.ALIGN_LEFT : Element.ALIGN_CENTER); pcell.setVerticalAlignment( compDataIndex[k].equals("comptdesc") || compDataIndex[k].equals("comptdesc") || compDataIndex[k].equals("selfcomment") ? Element.ALIGN_LEFT : Element.ALIGN_CENTER); compTable.addCell(pcell); } else { jarr2 = jobjAppraisal.getJSONArray("comments"); managerComments = ""; int commentCount = 1; for (int j = jarr2.length() - 1; j >= 0; j--) { jobjTemplate = jarr2.getJSONObject(j); managerComments += commentCount + ") " + jobjTemplate.getString("managercomment") + "\n\n"; commentCount++; } PdfPCell pcell = new PdfPCell(new Paragraph( fontFamilySelector.process(managerComments, FontContext.SMALL_NORMAL_HELVETICA))); pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setPadding(4); pcell.setBorderColor(Color.GRAY); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); compTable.addCell(pcell); } } } document.add(compTable); PdfPCell pcell; if (jarr.length() > 0) { PdfPTable helpTable = new PdfPTable(1); helpTable.setTotalWidth(90); helpTable.setWidthPercentage(100); helpTable.setSpacingBefore(20); pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.performance.mod.avg", null, RequestContextUtils.getLocale(request)) + ". : " + messageSource.getMessage("hrms.performance.mode.average.ratings", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_HELVETICA))); pcell.setBorder(0); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); helpTable.addCell(pcell); requestParams.clear(); requestParams.put("companyid", companyid); requestParams.put("checklink", "modaverage"); if (!StringUtil.isNullOrEmpty(request.getParameter("pdfEmail"))) { if (hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { document.add(helpTable); } } else { if (hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { document.add(helpTable); } } } PdfPTable compgTable = new PdfPTable(6); compgTable.setWidthPercentage(100); compgTable.setWidths(new float[] { 50, 60, 30, 50, 42, 70 }); compgTable.setSpacingBefore(20); compgTable.setHeaderRows(1); for (int i = 0; i < compGoalHeader.length; i++) { PdfPCell pgcell = new PdfPCell(new Paragraph( fontFamilySelector.process(compGoalHeader[i], FontContext.MEDIUM_BOLD_HELVETICA))); pgcell.setBorder(0); pgcell.setBorder(PdfPCell.BOX); pgcell.setPadding(4); pgcell.setBorderColor(Color.GRAY); pgcell.setHorizontalAlignment(Element.ALIGN_CENTER); compgTable.addCell(pgcell); } JSONObject jobjTemplates = new JSONObject(goalstr); com.krawler.utils.json.base.JSONArray jarr11 = jobjTemplates.getJSONArray("data"); JSONObject jobjl = new JSONObject(); for (int i = 0; i < jarr11.length(); i++) { jobjl = jarr11.getJSONObject(i); for (int k = 0; k < compHeader.length; k++) { pcell = new PdfPCell( new Paragraph( fontFamilySelector.process( !jobjl.isNull(compGoalDataIndex[k]) ? StringUtil .serverHTMLStripper(jobjl.getString(compGoalDataIndex[k])) : "", FontContext.SMALL_NORMAL_HELVETICA))); pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); pcell.setVerticalAlignment(Element.ALIGN_CENTER); compgTable.addCell(pcell); } } document.add(compgTable); document.newPage(); document.close(); } catch (DocumentException ex) { ex.printStackTrace(); throw ServiceException.FAILURE("AppraisalDetails.AppraisalDetail", ex); } catch (JSONException e) { e.printStackTrace(); throw ServiceException.FAILURE("AppraisalDetails.AppraisalDetail", e); } catch (Exception ex) { ex.printStackTrace(); throw ServiceException.FAILURE(ex.getMessage(), ex); } finally { writer.close(); } return baos; }
From source file:com.krawler.spring.hrms.payroll.payslip.ExportPayslipController.java
License:Open Source License
public ModelAndView exportPDF(HttpServletRequest request, HttpServletResponse response) { JSONObject jsonResp = new JSONObject(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = null; try {/*from w ww .java2 s . co m*/ Company company = (Company) kwlCommonTablesDAOObj.getObject("com.krawler.common.admin.Company", sessionHandlerImplObj.getCompanyid(request)); int i = 0; DecimalFormat decfm = new DecimalFormat("#,##0.00"); Date d2 = null; Date d3 = null; int days = 0; boolean showborder = true; if (!StringUtil.isNullOrEmpty(request.getParameter("showborder"))) { showborder = Boolean.parseBoolean(request.getParameter("showborder")); } SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); String stDate = request.getParameter("stdate").replace("/", "-"); String endDate = request.getParameter("enddate").replace("/", "-"); d2 = sdf.parse(stDate); d3 = sdf.parse(endDate); days = getWorkingDays(request.getParameter("stdate").replace("/", "-"), request.getParameter("enddate").replace("/", "-"), company.getCompanyID()); String historyid = ""; List<Payhistory> lst = exportPayslipDAO.getPayhistory(request.getParameter("empid"), d2, d3); ; if (lst != null && lst.size() == 0) { throw new Exception("Exception occured"); } else if (lst != null && lst.size() > 0) { String currSymbol = getCurrencySymbol(request, false); masterDB.Payhistory payhistory = (masterDB.Payhistory) lst.get(0); historyid = payhistory.getHistoryid(); double unpaidleaves = payhistory.getUnpaidleaves(); Document document = new Document(PageSize.A4, 15, 15, 15, 15); writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new EndPage(request)); document.open(); String userid = request.getParameter("empid"); User userinfo = payhistory.getUserID(); Useraccount ua = (Useraccount) kwlCommonTablesDAOObj .getObject("com.krawler.common.admin.Useraccount", userinfo.getUserID()); String uname = userinfo.getLastName() != null ? userinfo.getLastName() : ""; String empid = getUserCode(ua, company.getCompanyID()); String department = payhistory.getDepartment(); String designation = getUserDesignation(ua); String bankacc = ua.getAccno() != null ? ua.getAccno() : ""; Empprofile empprof = null; String epf = ""; String dateofjoin = ""; try { empprof = (Empprofile) kwlCommonTablesDAOObj.getObject("com.krawler.hrms.ess.Empprofile", userid); epf = StringUtil.isNullOrEmpty(empprof.getPfno()) ? "" : empprof.getPfno(); dateofjoin = empprof.getJoindate() != null ? empprof.getJoindate().toString() : ""; } catch (Exception ex) { epf = ""; } PdfPTable mainTable = new PdfPTable(1); mainTable.setWidthPercentage(100); PdfPTable table1 = new PdfPTable(3); table1.setWidthPercentage(100); table1.setWidths(new float[] { 40, 40, 20 }); PdfPTable userTable1 = new PdfPTable(1); String cmpid = AuthHandler.getCompanyid(request); userTable1.setHorizontalAlignment(Element.ALIGN_MIDDLE); userTable1.setWidthPercentage(10); PdfPCell cell11 = null; PdfPCell cell1x = new PdfPCell( new Paragraph(fontFamilySelector.process(" ", FontContext.REGULAR_BOLD_TIMES_NEW_ROMAN))); cell1x.setBorder(0); userTable1.addCell(cell1x); try { String imgPath = StorageHandler.GetProfileImgStorePath() + cmpid + ".png"; if (StringUtil.isStandAlone()) { imgPath = URLUtil.getPageURL(request, "").concat(ProfileImageServlet.defaultCompanyImgPath); } Image img = Image.getInstance(imgPath); cell11 = new PdfPCell(img); cell11.setPaddingLeft(5); } catch (Exception e) { cell11 = new PdfPCell(new Paragraph(fontFamilySelector .process(AuthHandler.getCompanyName(request), FontContext.TABLE_BOLD_TIMES_NEW_ROMAN))); cell11.setHorizontalAlignment(Element.ALIGN_CENTER); } if (cell11 != null) { cell11.setBorder(0); userTable1.addCell(cell11); } PdfPCell cell1 = new PdfPCell( new Paragraph(fontFamilySelector.process("", FontContext.REGULAR_BOLD_TIMES_NEW_ROMAN))); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setBorder(0); userTable1.addCell(cell1); PdfPTable userTable2 = new PdfPTable(1); userTable2.setWidthPercentage(100); PdfPCell cell = new PdfPCell(new Paragraph(fontFamilySelector.process(company.getCompanyName(), FontContext.REGULAR_BOLD_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(0); cell.setPaddingTop(15); userTable2.addCell(cell); cell = new PdfPCell(new Paragraph( fontFamilySelector.process(company.getCity(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(0); userTable2.addCell(cell); cell = new PdfPCell(new Paragraph(fontFamilySelector.process(company.getAddress(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(0); userTable2.addCell(cell); cell = new PdfPCell(new Paragraph( fontFamilySelector.process(company.getState(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(0); userTable2.addCell(cell); cell = new PdfPCell(new Paragraph(fontFamilySelector.process(company.getZipCode(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(0); userTable2.addCell(cell); cell = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.payslip.period.params", new Object[] { request.getParameter("stdate"), request.getParameter("enddate") }, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setBorder(0); userTable2.addCell(cell); PdfPTable userTable3 = new PdfPTable(1); userTable3.setWidthPercentage(100); PdfPCell cell2 = new PdfPCell( new Paragraph(fontFamilySelector.process("", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); cell2.setHorizontalAlignment(Element.ALIGN_LEFT); cell2.setBorder(0); userTable1.addCell(cell2); PdfPCell mainCell11 = new PdfPCell(userTable1); if (!showborder) { mainCell11.setBorder(0); } else { mainCell11.setBorder(Rectangle.LEFT); } table1.addCell(mainCell11); PdfPCell mainCell13 = new PdfPCell(userTable2); mainCell13.setBorder(0); table1.addCell(mainCell13); PdfPCell mainCell15 = new PdfPCell(userTable3); if (!showborder) { mainCell15.setBorder(0); } else { mainCell15.setBorder(Rectangle.RIGHT); } table1.addCell(mainCell15); PdfPCell mainCell12 = new PdfPCell(new Paragraph("")); if (!showborder) { mainCell12.setBorder(0); } else { mainCell12.setBorder(Rectangle.LEFT); } table1.addCell(mainCell12); PdfPCell mainCell14 = new PdfPCell(new Paragraph("")); mainCell14.setBorder(0); table1.addCell(mainCell14); PdfPCell mainCell16 = new PdfPCell(new Paragraph("")); if (!showborder) { mainCell16.setBorder(0); } else { mainCell16.setBorder(Rectangle.RIGHT); } table1.addCell(mainCell16); PdfPCell mainCell1 = new PdfPCell(table1); if (!showborder) { mainCell1.setBorder(0); } else { mainCell1.setBorder(1); } mainTable.addCell(mainCell1); // __________________________________________________________________________ PdfPTable table2 = new PdfPTable(4); table2.setWidthPercentage(100); table2.setWidths(new float[] { 20, 30, 20, 30 }); PdfPCell h11 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.employee.code", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h11.setHorizontalAlignment(Element.ALIGN_LEFT); h11.setPadding(5); if (!showborder) { h11.setBorder(0); } table2.addCell(h11); PdfPCell h12 = new PdfPCell(new Paragraph( fontFamilySelector.process("" + empid, FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h12.setHorizontalAlignment(Element.ALIGN_LEFT); h12.setPadding(5); if (!showborder) { h12.setBorder(0); } table2.addCell(h12); PdfPCell h13 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.employee.name", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h13.setHorizontalAlignment(Element.ALIGN_LEFT); h13.setPadding(5); if (!showborder) { h13.setBorder(0); } table2.addCell(h13); PdfPCell h14 = new PdfPCell(new Paragraph(fontFamilySelector .process(userinfo.getFirstName() + " " + uname, FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h14.setHorizontalAlignment(Element.ALIGN_LEFT); h14.setPadding(5); if (!showborder) { h14.setBorder(0); } table2.addCell(h14); PdfPCell h21 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.department", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h21.setHorizontalAlignment(Element.ALIGN_LEFT); h21.setPadding(5); if (!showborder) { h21.setBorder(0); } table2.addCell(h21); PdfPCell h22 = new PdfPCell(new Paragraph( fontFamilySelector.process(department, FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h22.setHorizontalAlignment(Element.ALIGN_LEFT); h22.setPadding(5); if (!showborder) { h22.setBorder(0); } table2.addCell(h22); PdfPCell h33 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.designation", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h33.setHorizontalAlignment(Element.ALIGN_LEFT); h33.setPadding(5); if (!showborder) { h33.setBorder(0); } table2.addCell(h33); PdfPCell h34 = new PdfPCell(new Paragraph( fontFamilySelector.process(designation, FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h34.setHorizontalAlignment(Element.ALIGN_LEFT); h34.setPadding(5); if (!showborder) { h34.setBorder(0); } table2.addCell(h34); PdfPCell h43 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.bank.ac.no", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h43.setHorizontalAlignment(Element.ALIGN_LEFT); h43.setPadding(5); if (!showborder) { h43.setBorder(0); } table2.addCell(h43); PdfPCell h44 = new PdfPCell(new Paragraph( fontFamilySelector.process(bankacc, FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h44.setHorizontalAlignment(Element.ALIGN_LEFT); h44.setPadding(5); if (!showborder) { h44.setBorder(0); } table2.addCell(h44); PdfPCell h61 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.working.days", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h61.setHorizontalAlignment(Element.ALIGN_LEFT); h61.setPadding(5); if (!showborder) { h61.setBorder(0); } table2.addCell(h61); PdfPCell h62 = new PdfPCell(new Paragraph(fontFamilySelector.process(String.valueOf(days), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h62.setHorizontalAlignment(Element.ALIGN_LEFT); h62.setPadding(5); if (!showborder) { h62.setBorder(0); } table2.addCell(h62); PdfPCell h63 = new PdfPCell(new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.e.p.f", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h63.setHorizontalAlignment(Element.ALIGN_LEFT); h63.setPadding(5); if (!showborder) { h63.setBorder(0); } table2.addCell(h63); PdfPCell h64 = new PdfPCell( new Paragraph(fontFamilySelector.process(epf, FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h64.setHorizontalAlignment(Element.ALIGN_LEFT); h64.setPadding(5); if (!showborder) { h64.setBorder(0); } table2.addCell(h64); PdfPCell h65 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.common.DateofJoining", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h65.setHorizontalAlignment(Element.ALIGN_LEFT); h65.setPadding(5); if (!showborder) { h65.setBorder(0); } table2.addCell(h65); PdfPCell h66 = new PdfPCell(new Paragraph( fontFamilySelector.process(dateofjoin, FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); h66.setHorizontalAlignment(Element.ALIGN_LEFT); h66.setPadding(5); if (!showborder) { h66.setBorder(0); } table2.addCell(h66); for (int y = 0; y < 2; y++) { PdfPCell h71 = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h71.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { h71.setBorder(0); } else { h71.setBorder(Rectangle.LEFT); } table2.addCell(h71); for (i = 0; i < 2; i++) { h71 = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h71.setHorizontalAlignment(Element.ALIGN_LEFT); h71.setBorder(0); table2.addCell(h71); } h71 = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); h71.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { h71.setBorder(0); } else { h71.setBorder(Rectangle.RIGHT); } table2.addCell(h71); } PdfPCell mainCell2 = new PdfPCell(table2); if (!showborder) { mainCell2.setBorder(0); } else { mainCell2.setBorder(1); } mainTable.addCell(mainCell2); PdfPTable table3main = new PdfPTable(1); table3main.setWidthPercentage(100); PdfPTable table7main = new PdfPTable(1); table7main.setWidthPercentage(100); PdfPTable table31 = new PdfPTable(4); table31.setWidthPercentage(100); table31.setWidths(new float[] { 30, 20, 30, 20 }); PdfPTable table41 = new PdfPTable(2); table41.setWidthPercentage(100); table41.setWidths(new float[] { 50, 50 }); //*************************************************************************************************************** PdfPCell s11 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.Earnings", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); s11.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s11.setBorder(0); } else { s11.setBorder(Rectangle.LEFT + Rectangle.BOTTOM); } s11.setPadding(5); table31.addCell(s11); PdfPCell s12 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.amount.params", new Object[] { currSymbol }, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); s12.setHorizontalAlignment(Element.ALIGN_RIGHT); s12.setPadding(5); if (!showborder) { s12.setBorder(0); } else { s12.setBorder(Rectangle.LEFT + Rectangle.BOTTOM); } table31.addCell(s12); PdfPCell s14 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.Deduction", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); s14.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s14.setBorder(0); } else { s14.setBorder(Rectangle.LEFT + Rectangle.BOTTOM); } s14.setPadding(5); table31.addCell(s14); PdfPCell s15 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.amount.params", new Object[] { currSymbol }, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); s15.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { s15.setBorder(0); } else { s15.setBorder(Rectangle.LEFT + Rectangle.RIGHT + Rectangle.BOTTOM); } s15.setPadding(5); table31.addCell(s15); int Wcount = 0; float taxtotal = 0; List<Historydetail> lst1 = exportPayslipDAO.getHistorydetail(historyid, "Wages", "Basic"); List<Historydetail> list = exportPayslipDAO.getHistorydetailNotType(historyid, "Wages", "Basic"); if (list != null) { lst1.addAll(list); } List<Historydetail> lst2 = exportPayslipDAO.getHistorydetail(historyid, "Taxes"); CompanyPreferences cp = hrmsCommonDAOObj.getCompanyPreferences(company.getCompanyID()); int financialMonth = cp.getFinancialmonth(); Calendar c1 = Calendar.getInstance(); c1.setTime(d3); c1.set(Calendar.MONTH, financialMonth); c1.set(Calendar.DATE, 1); Date d = c1.getTime(); Date DOJ = new java.util.Date(empprof.getJoindate().getTime()); if (d3.before(d)) { int currentYear = c1.get(Calendar.YEAR) - 1; c1.set(Calendar.YEAR, currentYear); d = c1.getTime(); } if (DOJ.after(d)) { c1.setTime(DOJ); c1.set(Calendar.DATE, 1); d = c1.getTime(); } List<Historydetail> lst3 = exportPayslipDAO.getHistorydetail(historyid, "Deduction"); int size = lst1.size() > (lst2.size() + lst3.size()) ? lst1.size() : lst2.size() + lst3.size(); float wagetotal = 0; double totalYTDEarning = 0, totalYTDDeduction = 0; for (i = 0; i < size; i++) { masterDB.Historydetail hd = null; if (i < lst1.size()) { hd = (masterDB.Historydetail) lst1.get(i); PdfPCell s21 = new PdfPCell(new Paragraph(fontFamilySelector.process(hd.getType(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s21.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s21.setBorder(0); } else { s21.setBorder(Rectangle.LEFT); } s21.setPadding(5); table31.addCell(s21); BigDecimal bd = new BigDecimal(hd.getAmount()); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); double newamount = bd.doubleValue(); PdfPCell s22 = new PdfPCell( new Paragraph(fontFamilySelector.process(String.valueOf(decfm.format(newamount)), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s22.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { s22.setBorder(0); } else { s22.setBorder(Rectangle.LEFT); } s22.setPadding(5); table31.addCell(s22); List<Historydetail> lstw = exportPayslipDAO.getHistorydetail(request.getParameter("empid"), d, d3, hd.getType()); double wageAmt = 0; masterDB.Historydetail hd1 = null; for (int y = 0; y < lstw.size(); y++) { hd1 = (masterDB.Historydetail) lstw.get(y); wageAmt += Double.parseDouble(hd1.getAmount()); } wagetotal = wagetotal + Float.parseFloat(hd.getAmount()); totalYTDEarning = totalYTDEarning + wageAmt; Wcount++; } else { for (int j = 0; j < 2; j++) { PdfPCell s21 = new PdfPCell(new Paragraph( fontFamilySelector.process("", FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s21.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s21.setBorder(0); } else { s21.setBorder(Rectangle.LEFT); } s21.setPadding(5); table31.addCell(s21); } } if (i < lst2.size()) { hd = (masterDB.Historydetail) lst2.get(i); PdfPCell s24 = new PdfPCell(new Paragraph(fontFamilySelector.process(hd.getType(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s24.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s24.setBorder(0); } else { s24.setBorder(Rectangle.LEFT); } s24.setPadding(5); table31.addCell(s24); BigDecimal bd = new BigDecimal(hd.getAmount()); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); double newamount = bd.doubleValue(); PdfPCell s25 = new PdfPCell(new Paragraph(fontFamilySelector .process(decfm.format(newamount), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s25.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { s25.setBorder(0); } else { s25.setBorder(Rectangle.RIGHT + Rectangle.LEFT); } s25.setPadding(5); table31.addCell(s25); List<Historydetail> lstw1 = exportPayslipDAO.getHistorydetail(request.getParameter("empid"), d, d3, hd.getType()); double taxAmt = 0; masterDB.Historydetail hd2 = null; for (int y = 0; y < lstw1.size(); y++) { hd2 = (masterDB.Historydetail) lstw1.get(y); taxAmt += Double.parseDouble(hd2.getAmount()); } taxtotal = taxtotal + Float.parseFloat(hd.getAmount()); totalYTDDeduction = totalYTDDeduction + taxAmt; } else { if (i - lst2.size() < lst3.size()) { hd = (masterDB.Historydetail) lst3.get(i - lst2.size()); PdfPCell s24 = new PdfPCell(new Paragraph(fontFamilySelector.process(hd.getType(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); if (hd.getType().equals("Unpaid_leaves")) { s24 = new PdfPCell(new Paragraph( fontFamilySelector.process(hd.getType() + "(" + unpaidleaves + ")", FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); } s24.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s24.setBorder(0); } else { s24.setBorder(Rectangle.LEFT); } s24.setPadding(5); table31.addCell(s24); BigDecimal bd = new BigDecimal(hd.getAmount()); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); double newamount = bd.doubleValue(); PdfPCell s25 = new PdfPCell(new Paragraph(fontFamilySelector .process(decfm.format(newamount), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s25.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { s25.setBorder(0); } else { s25.setBorder(Rectangle.RIGHT + Rectangle.LEFT); ; } s25.setPadding(5); table31.addCell(s25); List<Historydetail> lstw2 = exportPayslipDAO .getHistorydetail(request.getParameter("empid"), d, d3, hd.getType()); masterDB.Historydetail hd3 = null; double deductAmt = 0; for (int y = 0; y < lstw2.size(); y++) { hd3 = (masterDB.Historydetail) lstw2.get(y); deductAmt += Double.parseDouble(hd3.getAmount()); } taxtotal = taxtotal + Float.parseFloat(hd.getAmount()); totalYTDDeduction = totalYTDDeduction + deductAmt; } else { for (int j = 0; j < 2; j++) { PdfPCell s21 = new PdfPCell(new Paragraph( fontFamilySelector.process("", FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s21.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s21.setBorder(0); } else { s21.setBorder(Rectangle.LEFT + Rectangle.RIGHT); } s21.setPadding(5); table31.addCell(s21); } } } } PdfPCell s91 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.total.earnings", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); s91.setHorizontalAlignment(Element.ALIGN_BASELINE); s91.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s91.setBorder(0); } else { s91.setBorder(Rectangle.LEFT + Rectangle.TOP + Rectangle.BOTTOM); } s91.setPadding(5); table31.addCell(s91); BigDecimal bd = new BigDecimal(String.valueOf(wagetotal)); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); double newamount = bd.doubleValue(); PdfPCell s92 = new PdfPCell(new Paragraph(fontFamilySelector.process(decfm.format(newamount), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s92.setHorizontalAlignment(Element.ALIGN_BASELINE); s92.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { s92.setBorder(0); } else { s92.setBorder(Rectangle.LEFT + Rectangle.TOP + Rectangle.BOTTOM); } s92.setPadding(5); table31.addCell(s92); PdfPCell s94 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.total.deductions", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); s94.setHorizontalAlignment(Element.ALIGN_BASELINE); s94.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s94.setBorder(0); } else { s94.setBorder(Rectangle.LEFT + Rectangle.TOP + Rectangle.BOTTOM); } s94.setPadding(5); table31.addCell(s94); bd = new BigDecimal(String.valueOf(taxtotal)); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); newamount = bd.doubleValue(); PdfPCell s95 = new PdfPCell(new Paragraph(fontFamilySelector.process(decfm.format(newamount), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s95.setHorizontalAlignment(Element.ALIGN_BASELINE); s95.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { s95.setBorder(0); } else { s95.setBorder(Rectangle.RIGHT + Rectangle.LEFT + Rectangle.TOP + Rectangle.BOTTOM); } s95.setPadding(5); table31.addCell(s95); for (int y = 0; y < 2; y++) { PdfPCell he71 = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); he71.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { he71.setBorder(0); } else { he71.setBorder(Rectangle.LEFT); } table31.addCell(he71); for (i = 0; i < 2; i++) { he71 = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); he71.setHorizontalAlignment(Element.ALIGN_LEFT); he71.setBorder(0); table31.addCell(he71); } he71 = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); he71.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { he71.setBorder(0); } else { he71.setBorder(Rectangle.RIGHT); } table31.addCell(he71); } List<Historydetail> lst4 = exportPayslipDAO.getHistorydetail(historyid, "Employer Contribution"); double ectotal = 0; PdfPCell ec11 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.EmployerContribution", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); ec11.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { ec11.setBorder(0); } else { ec11.setBorder(Rectangle.LEFT + Rectangle.BOTTOM); } ec11.setPadding(5); table41.addCell(ec11); PdfPCell ec12 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.amount.params", new Object[] { currSymbol }, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); ec12.setHorizontalAlignment(Element.ALIGN_RIGHT); ec12.setPadding(5); if (!showborder) { ec12.setBorder(0); } else { ec12.setBorder(Rectangle.LEFT + Rectangle.BOTTOM + Rectangle.RIGHT); } table41.addCell(ec12); masterDB.Historydetail hd1 = null; for (i = 0; i < lst4.size(); i++) { hd1 = (masterDB.Historydetail) lst4.get(i); PdfPCell s24 = new PdfPCell(new Paragraph( fontFamilySelector.process(hd1.getType(), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s24.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { s24.setBorder(0); } else { s24.setBorder(Rectangle.LEFT); } s24.setPadding(5); table41.addCell(s24); bd = new BigDecimal(hd1.getAmount()); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); newamount = bd.doubleValue(); ectotal = ectotal + newamount; PdfPCell s25 = new PdfPCell(new Paragraph(fontFamilySelector.process(decfm.format(newamount), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); s25.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { s25.setBorder(0); } else { s25.setBorder(Rectangle.RIGHT + Rectangle.LEFT); } s25.setPadding(5); table41.addCell(s25); } PdfPCell se91 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.total.contribution", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); se91.setHorizontalAlignment(Element.ALIGN_BASELINE); se91.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { se91.setBorder(0); } else { se91.setBorder(Rectangle.LEFT + Rectangle.TOP + Rectangle.BOTTOM); } se91.setPadding(5); table41.addCell(se91); bd = new BigDecimal(String.valueOf(ectotal)); bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); newamount = bd.doubleValue(); PdfPCell se92 = new PdfPCell(new Paragraph(fontFamilySelector.process(decfm.format(newamount), FontContext.SMALL_NORMAL_TIMES_NEW_ROMAN))); se92.setHorizontalAlignment(Element.ALIGN_BASELINE); se92.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { se92.setBorder(0); } else { se92.setBorder(Rectangle.LEFT + Rectangle.TOP + Rectangle.BOTTOM + Rectangle.RIGHT); } se92.setPadding(5); table41.addCell(se92); PdfPCell Cell31 = new PdfPCell(table31); if (!showborder) { Cell31.setBorder(0); } else { Cell31.setBorder(1); } table3main.addCell(Cell31); PdfPCell mainCell4 = new PdfPCell(table3main); if (!showborder) { mainCell4.setBorder(0); } else { mainCell4.setBorder(1); } mainTable.addCell(mainCell4); PdfPCell Cell41 = new PdfPCell(table41); if (!showborder) { Cell41.setBorder(0); } else { Cell41.setBorder(1); } table7main.addCell(Cell41); PdfPCell mainCell44 = new PdfPCell(table7main); if (!showborder) { mainCell44.setBorder(0); } else { mainCell44.setBorder(1); } mainTable.addCell(mainCell44); //*************************************************************************************************************** PdfPTable table4 = new PdfPTable(2); table4.setWidthPercentage(100); for (i = 0; i < 4; i++) { PdfPCell i33 = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); i33.setHorizontalAlignment(Element.ALIGN_CENTER); if (!showborder) { i33.setBorder(0); } else { i33.setBorder(Rectangle.LEFT + Rectangle.RIGHT); } table4.addCell(i33); } PdfPCell i31 = new PdfPCell(new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.netpay", null, RequestContextUtils.getLocale(request)) + " ", FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); i31.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { i31.setBorder(0); } else { i31.setBorder(Rectangle.LEFT + Rectangle.TOP); } i31.setPadding(5); table4.addCell(i31); double netpay = 0; netpay = wagetotal - taxtotal; netpay = Math.round(netpay); String amount = numberFormatter((double) netpay, currSymbol); PdfPCell i33 = new PdfPCell( new Paragraph(fontFamilySelector.process(amount, FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); i33.setHorizontalAlignment(Element.ALIGN_RIGHT); if (!showborder) { i33.setBorder(0); } else { i31.setBorder(Rectangle.RIGHT); } i33.setPadding(5); table4.addCell(i33); PdfPCell i32 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.in.words", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); i32.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { i32.setBorder(0); } else { i31.setBorder(Rectangle.LEFT); } i32.setPadding(5); table4.addCell(i32); EnglishNumberToWords enw = new EnglishNumberToWords(request, messageSource); String netinword = enw.convert(netpay); PdfPCell i34 = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.only", new Object[] { getCurrencyName(request), netinword }, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); i34.setHorizontalAlignment(Element.ALIGN_LEFT); if (!showborder) { i34.setBorder(0); } else { i31.setBorder(Rectangle.RIGHT); } i34.setPadding(5); table4.addCell(i34); PdfPCell mainCell5 = new PdfPCell(table4); if (!showborder) { mainCell5.setBorder(0); } else { mainCell5.setBorder(1); } mainTable.addCell(mainCell5); document.add(new Paragraph("\n\n")); PdfPTable table5 = new PdfPTable(2); table5.setWidthPercentage(80); cell = new PdfPCell(new Paragraph("___________________________")); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); table5.addCell(cell); cell = new PdfPCell(new Paragraph("___________________________")); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); table5.addCell(cell); cell = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.employee.signature", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); table5.addCell(cell); cell = new PdfPCell( new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.payroll.manager.signature", null, RequestContextUtils.getLocale(request)), FontContext.SMALL_BOLD_TIMES_NEW_ROMAN))); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); table5.addCell(cell); document.add(mainTable); document.close(); } String filename = "Employee_Payslip.pdf"; if (!StringUtil.isNullOrEmpty(request.getParameter("reportname"))) { String temp = request.getParameter("reportname"); filename = temp.lastIndexOf(".pdf") >= 0 ? temp : temp + ".pdf"; } response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.setContentType("application/octet-stream"); response.setContentLength(baos.size()); response.getOutputStream().write(baos.toByteArray()); jsonResp.put("valid", true); jsonResp.put("data", ""); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (response.getOutputStream() != null) { response.getOutputStream().flush(); response.getOutputStream().close(); } } catch (IOException e) { e.printStackTrace(); } writer.close(); } return new ModelAndView("jsonView", "model", jsonResp.toString()); }
From source file:com.krawler.spring.hrms.payroll.salaryslip.ExportSalarySlipService.java
License:Open Source License
public void exportSalarySlip(String userid, Date startdate, Date enddate, HttpServletRequest request, HttpServletResponse response, String historyid) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(PageSize.A4); PdfWriter writer = null; try {/*from w ww . ja va2s . c o m*/ HrmsCommonPayroll hrmsCommonPayroll = new HrmsCommonPayroll(); DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); User user = exportSalarySlipDAO.getUser(userid); Empprofile empprofile = exportSalarySlipDAO.getEmpprofile(userid); Useraccount useraccount = exportSalarySlipDAO.getUserAccount(userid); Company company = user.getCompany(); PayrollHistory payrollHistory = exportSalarySlipDAO.getPayrollHistory(historyid); CompanyPreferences preferences = (CompanyPreferences) kwlCommonTablesDAO .getObject("com.krawler.common.admin.CompanyPreferences", company.getCompanyID()); Date financialStartDate = hrmsCommonPayroll.getFinanacialYearStartDate(enddate, preferences.getFinancialmonth()); Date financialEndDate = hrmsCommonPayroll.getFinanacialYearEndDate(enddate, preferences.getFinancialmonth() + 11); IncomeTax incomeTax = getIncomTaxObj(request); List<PayrollHistory> list = null; Map<String, Double> mapEPF = null; if (incomeTax != null) { list = incomeTax.getPayrollHistories(userid, HrmsPayrollConstants.PAYROLL_HISTORY_STATUS_PROCESSED_FINAL, financialStartDate, financialEndDate); mapEPF = incomeTax.getEPF(list, payrollHistory); } writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new EndPage(RequestContextUtils.getLocale(request))); document.open(); PdfPTable mainTable = new PdfPTable(1); mainTable.setWidthPercentage(100); mainTable.addCell(getEmployeeDetails(user, empprofile, useraccount, company, payrollHistory, sdf, Rectangle.NO_BORDER, request)); mainTable.addCell(getSalaryDetails(user, empprofile, useraccount, company, payrollHistory, sdf, Rectangle.NO_BORDER, request)); mainTable.addCell(getEmployeeAndEmployerDetails(user, empprofile, useraccount, company, payrollHistory, sdf, Rectangle.CELL, list, mapEPF, request)); document.newPage(); document.add(mainTable); document.close(); response.setHeader("Content-Disposition", "attachment; filename=\"" + user.getFirstName() + "_" + sdf.format(startdate) + ".pdf\""); response.setContentType("application/octet-stream"); response.setContentLength(baos.size()); response.getOutputStream().write(baos.toByteArray()); } catch (Exception e) { e.printStackTrace(); } finally { writer.close(); } }
From source file:com.krawler.spring.hrms.printreport.PrintAppraisalReportDAOImp.java
License:Open Source License
public void createPDFPriview(HttpServletRequest request, HttpServletResponse response, JSONArray obj, Map<String, String> personalData) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(PageSize.A4.rotate(), 25, 25, 25, 25); PdfWriter writer = null; try {//from ww w . ja v a2s. c o m HashMap<String, Object> requestParams = new HashMap<String, Object>(); boolean isQuestion = request.getParameter("question").equals("true") ? true : false; boolean promotion = request.getParameter("promotion").equals("false") ? false : true; Boolean isMyAppraisal = Boolean.parseBoolean(personalData.get("isMyAppraisal")); CompanyPreferences companyPreferences = (CompanyPreferences) hibernateTemplate .get(CompanyPreferences.class, sessionHandlerImplObj.getCompanyid(request)); writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new EndPage()); document.open(); java.awt.Color tColor = new Color(9, 9, 9); //fontSmallBold.setColor(tColor); //heading PdfPTable headTable = new PdfPTable(1); headTable.setTotalWidth(90); headTable.setWidthPercentage(100); headTable.setSpacingBefore(20); PdfPCell headcell = new PdfPCell( new Paragraph(fontFamilySelector.process(messageSource.getMessage("hrms.common.AppraisalForm", null, RequestContextUtils.getLocale(request)), FontContext.REGULAR_BOLD_HELVETICA))); headcell.setBackgroundColor(new Color(0xEEEEEE)); headcell.setPadding(5); headTable.addCell(headcell); document.add(headTable); //heading //Details int flag = 0; String appDetailsParam[] = { messageSource.getMessage("hrms.performance.appraisal.cycle.name", null, RequestContextUtils.getLocale(request)) + ": ", messageSource.getMessage("hrms.common.employee.name", null, RequestContextUtils.getLocale(request)) + ": ", messageSource.getMessage("hrms.performance.appraisal.cycle.period", null, RequestContextUtils.getLocale(request)) + ": ", messageSource.getMessage("hrms.Masters.Designation", null, RequestContextUtils.getLocale(request)) + ": ", messageSource.getMessage("hrms.performance.performance.rating", null, RequestContextUtils.getLocale(request)) + "*: " }; String appDetailsValue[] = { personalData.get("appcycname"), personalData.get("empname"), messageSource.getMessage("hrms.common.form", null, RequestContextUtils.getLocale(request)) + ": " + personalData.get("startdate") + " " + messageSource.getMessage("hrms.common.to", null, RequestContextUtils.getLocale(request)) + ": " + personalData.get("enddate"), personalData.get("desgname"), personalData.get("performance") }; requestParams.clear(); requestParams.put("companyid", sessionHandlerImplObj.getCompanyid(request)); requestParams.put("checklink", "promotion"); if (!hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { flag = 1; } PdfPTable appDetailsTable = new PdfPTable(2); appDetailsTable.setWidthPercentage(100); appDetailsTable.setWidths(new float[] { 55, 75 }); if (isMyAppraisal) { flag = 1; } for (int i = 0; i < (appDetailsParam.length - flag); i++) { PdfPCell pcell = new PdfPCell(new Paragraph( fontFamilySelector.process(appDetailsParam[i], FontContext.MEDIUM_BOLD_HELVETICA))); pcell.setBorder(0); pcell.setPaddingTop(10); pcell.setPaddingLeft(15); pcell.setPaddingBottom(4); pcell.setBorderColor(new Color(0xF2F2F2)); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell); PdfPCell pcell1 = new PdfPCell(new Paragraph( fontFamilySelector.process(appDetailsValue[i], FontContext.SMALL_NORMAL_HELVETICA))); pcell1.setBorder(0); pcell1.setPaddingTop(10); pcell1.setPaddingLeft(15); pcell1.setPaddingBottom(4); pcell1.setBorderColor(new Color(0xF2F2F2)); pcell1.setHorizontalAlignment(Element.ALIGN_LEFT); pcell1.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell1); } requestParams.clear(); requestParams.put("companyid", sessionHandlerImplObj.getCompanyid(request)); requestParams.put("checklink", "overallcomments"); if (hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { PdfPCell pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( (isMyAppraisal ? messageSource.getMessage("hrms.performance.self.overall.comment", null, RequestContextUtils.getLocale(request)) + ": " : messageSource.getMessage("hrms.performance.overall.appraiser.comments", null, RequestContextUtils.getLocale(request)) + ": "), FontContext.MEDIUM_BOLD_HELVETICA))); pcell.setBorder(0); pcell.setPaddingTop(10); pcell.setPaddingLeft(15); pcell.setPaddingBottom(4); pcell.setBorderColor(new Color(0xF2F2F2)); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell); PdfPCell pcell1 = new PdfPCell(new Paragraph(fontFamilySelector.process( (isMyAppraisal ? personalData.get("selfcomment") : personalData.get("empcomment")), FontContext.SMALL_NORMAL_HELVETICA))); pcell1.setBorder(0); pcell1.setPaddingTop(10); pcell1.setPaddingLeft(15); pcell1.setPaddingBottom(4); pcell1.setBorderColor(new Color(0xF2F2F2)); pcell1.setHorizontalAlignment(Element.ALIGN_LEFT); pcell1.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell1); } requestParams.clear(); requestParams.put("companyid", sessionHandlerImplObj.getCompanyid(request)); requestParams.put("checklink", "promotion"); if (!isMyAppraisal && hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { PdfPCell pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.performance.promotion.recommendation", null, RequestContextUtils.getLocale(request)) + ": ", FontContext.MEDIUM_BOLD_HELVETICA))); pcell.setBorder(0); pcell.setPaddingTop(10); pcell.setPaddingLeft(15); pcell.setPaddingBottom(4); pcell.setBorderColor(new Color(0xF2F2F2)); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell); PdfPCell pcell1 = new PdfPCell(new Paragraph(fontFamilySelector.process( (promotion ? messageSource.getMessage("hrms.MasterData.Yes", null, RequestContextUtils.getLocale(request)) : messageSource.getMessage("hrms.MasterData.No", null, RequestContextUtils.getLocale(request))), FontContext.SMALL_NORMAL_HELVETICA))); pcell1.setBorder(0); pcell1.setPaddingTop(10); pcell1.setPaddingLeft(15); pcell1.setPaddingBottom(4); pcell1.setBorderColor(new Color(0xF2F2F2)); pcell1.setHorizontalAlignment(Element.ALIGN_LEFT); pcell1.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell1); } if (promotion && !isMyAppraisal && hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { String[] appDetaisPromotionParam = { messageSource.getMessage("hrms.performance.new.department", null, RequestContextUtils.getLocale(request)) + ": ", messageSource.getMessage("hrms.performance.new.designation", null, RequestContextUtils.getLocale(request)) + ": ", messageSource.getMessage("hrms.performance.salary.increment", null, RequestContextUtils.getLocale(request)) + "(%): " }; String[] appDetaisPromotionValue = { personalData.get("newDept") != null ? personalData.get("newDept") : "", personalData.get("newDesig") != null ? personalData.get("newDesig") : "", personalData.get("salInc") != null ? personalData.get("salInc") : "" }; for (int i = 0; i < appDetaisPromotionParam.length; i++) { PdfPCell pcell = new PdfPCell(new Paragraph(fontFamilySelector .process(appDetaisPromotionParam[i], FontContext.MEDIUM_BOLD_HELVETICA))); pcell.setBorder(0); pcell.setPaddingTop(10); pcell.setPaddingLeft(15); pcell.setPaddingBottom(4); pcell.setBorderColor(new Color(0xF2F2F2)); pcell.setHorizontalAlignment(Element.ALIGN_LEFT); pcell.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell); PdfPCell pcell1 = new PdfPCell(new Paragraph(fontFamilySelector .process(appDetaisPromotionValue[i], FontContext.SMALL_NORMAL_HELVETICA))); pcell1.setBorder(0); pcell1.setPaddingTop(10); pcell1.setPaddingLeft(15); pcell1.setPaddingBottom(4); pcell1.setBorderColor(new Color(0xF2F2F2)); pcell1.setHorizontalAlignment(Element.ALIGN_LEFT); pcell1.setVerticalAlignment(Element.ALIGN_LEFT); appDetailsTable.addCell(pcell1); } } document.add(appDetailsTable); //Details //Comp requestParams.clear(); requestParams.put("companyid", sessionHandlerImplObj.getCompanyid(request)); requestParams.put("checklink", "competency"); if (hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { try { if (!isQuestion) { JSONArray jsonComp = obj.getJSONArray(0); if (!isMyAppraisal) { String[] compParam = { messageSource.getMessage("hrms.performance.competency", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.competency.description", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.weightage", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.rating", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.comment", null, RequestContextUtils.getLocale(request)) }; String[] colParam = { "cmptname", "cmptdesc", "cmptwt", "manrat", "mancompcomment" }; PdfPTable questionTable = null; if (companyPreferences.isWeightage()) { questionTable = new PdfPTable(5); questionTable.setWidths(new float[] { 50, 60, 30, 30, 42 }); } else { questionTable = new PdfPTable(4); questionTable.setWidths(new float[] { 50, 60, 30, 42 }); } questionTable.setWidthPercentage(100); questionTable.setSpacingBefore(20); questionTable.setHeaderRows(1); for (int i = 0; i < compParam.length; i++) { if (!(!companyPreferences.isWeightage() && i == 2)) { PdfPCell pgcell = new PdfPCell(new Paragraph(fontFamilySelector .process(compParam[i], FontContext.MEDIUM_BOLD_HELVETICA))); pgcell.setBorder(0); pgcell.setBorder(PdfPCell.BOX); pgcell.setPadding(4); pgcell.setBorderColor(Color.GRAY); pgcell.setHorizontalAlignment(Element.ALIGN_CENTER); questionTable.addCell(pgcell); } } for (int i = 0; i < jsonComp.length(); i++) { for (int j = 0; j < colParam.length; j++) { if (!(!companyPreferences.isWeightage() && j == 2)) { PdfPCell pcell = null; if (j != 3) pcell = new PdfPCell( new Paragraph(fontFamilySelector.process( jsonComp.getJSONObject(i).has(colParam[j]) ? jsonComp .getJSONObject(i).getString(colParam[j]) : "", FontContext.SMALL_NORMAL_HELVETICA))); else { pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( jsonComp.getJSONObject(i).has(colParam[j]) ? (jsonComp.getJSONObject(i).getString(colParam[j]) .equals("0") ? "" : jsonComp.getJSONObject(i) .getString(colParam[j])) : "", FontContext.SMALL_NORMAL_HELVETICA))); } pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); pcell.setVerticalAlignment(Element.ALIGN_CENTER); questionTable.addCell(pcell); } } } document.add(questionTable); } else { String[] compParam = { messageSource.getMessage("hrms.performance.competency", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.competency.description", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.weightage", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.rating", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.comments", null, RequestContextUtils.getLocale(request)) }; String[] colParam = { "cmptname", "cmptdesc", "cmptwt", "emprat", "empcompcomment" }; PdfPTable questionTable = null; if (companyPreferences.isWeightage()) { questionTable = new PdfPTable(5); questionTable.setWidths(new float[] { 50, 60, 30, 30, 42 }); } else { questionTable = new PdfPTable(4); questionTable.setWidths(new float[] { 50, 60, 30, 42 }); } questionTable.setWidthPercentage(100); questionTable.setSpacingBefore(20); questionTable.setHeaderRows(1); for (int i = 0; i < compParam.length; i++) { if (!(!companyPreferences.isWeightage() && i == 2)) { PdfPCell pgcell = new PdfPCell(new Paragraph(fontFamilySelector .process(compParam[i], FontContext.MEDIUM_BOLD_HELVETICA))); pgcell.setBorder(0); pgcell.setBorder(PdfPCell.BOX); pgcell.setPadding(4); pgcell.setBorderColor(Color.GRAY); pgcell.setHorizontalAlignment(Element.ALIGN_CENTER); questionTable.addCell(pgcell); } } for (int i = 0; i < jsonComp.length(); i++) { for (int j = 0; j < colParam.length; j++) { if (!(!companyPreferences.isWeightage() && j == 2)) { PdfPCell pcell; if (j != 3) pcell = new PdfPCell( new Paragraph(fontFamilySelector.process( jsonComp.getJSONObject(i).has(colParam[j]) ? jsonComp .getJSONObject(i).getString(colParam[j]) : "", FontContext.SMALL_NORMAL_HELVETICA))); else pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( jsonComp.getJSONObject(i).has(colParam[j]) ? (jsonComp.getJSONObject(i).getString(colParam[j]) .equals("0") ? "" : jsonComp.getJSONObject(i) .getString(colParam[j])) : "", FontContext.SMALL_NORMAL_HELVETICA))); pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); pcell.setVerticalAlignment(Element.ALIGN_CENTER); questionTable.addCell(pcell); } } } document.add(questionTable); } } else {//Questions try { JSONArray jsonQues = obj.getJSONArray(1); JSONArray jsonAnsTemp = obj.getJSONArray(2); JSONArray jsonAns = new JSONArray(); PdfPTable quesMainTable = new PdfPTable(1); quesMainTable.setTotalWidth(90); quesMainTable.setWidthPercentage(100); quesMainTable.setSpacingBefore(20); quesMainTable.setSkipFirstHeader(true); PdfPCell quesHeadcell = new PdfPCell(new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.performance.qualitative.appraisal", null, RequestContextUtils.getLocale(request)), FontContext.MEDIUM_BOLD_HELVETICA))); quesHeadcell.setPadding(5); quesMainTable.addCell(quesHeadcell); document.add(quesMainTable); PdfPTable[] quesTable = new PdfPTable[jsonQues.length()]; try { for (int i = 0; i < jsonQues.length(); i++) { String qid = (String) jsonQues.getJSONObject(i).get("qdescription"); for (int j = 0; j < jsonAnsTemp.length(); j++) { try { if (qid.equals((String) jsonAnsTemp.getJSONObject(j).get("question"))) { jsonAns.put(jsonAnsTemp.getJSONObject(j)); break; } } catch (JSONException e) { jsonAns.put(""); } } } } catch (JSONException e) { e.printStackTrace(); } for (int i = 0; i < jsonQues.length(); i++) { quesTable[i] = new PdfPTable(2); quesTable[i].setWidthPercentage(90); quesTable[i].setWidths(new float[] { 10, 80 }); quesTable[i].setSpacingBefore(20); quesTable[i].setHeaderRows(1); PdfPCell pgcell1 = new PdfPCell(new Paragraph(fontFamilySelector.process( messageSource.getMessage("hrms.performance.que", null, RequestContextUtils.getLocale(request)) + ".", FontContext.SMALL_NORMAL_HELVETICA))); pgcell1.setBorder(0); pgcell1.setBorder(PdfPCell.BOX); pgcell1.setPadding(4); pgcell1.setHorizontalAlignment(Element.ALIGN_CENTER); quesTable[i].addCell(pgcell1); PdfPCell pgcell2 = new PdfPCell( new Paragraph( fontFamilySelector .process( jsonQues.getJSONObject(i).has("qdesc") ? jsonQues .getJSONObject(i).getString("qdesc") : "", FontContext.SMALL_NORMAL_HELVETICA))); pgcell2.setBorder(0); pgcell2.setBorder(PdfPCell.BOX); pgcell2.setPadding(4); pgcell2.setHorizontalAlignment(Element.ALIGN_LEFT); quesTable[i].addCell(pgcell2); int cnt = 0; try { cnt = jsonQues.getJSONObject(i).has("qans") ? Integer.parseInt(jsonQues.getJSONObject(i).getString("qans")) : 0; } catch (NumberFormatException e) { e.printStackTrace(); } if (jsonAns.length() > 0) { JSONArray arr = jsonAns.getJSONObject(i).getJSONArray("answer"); for (int j = 0; j < arr.length(); j++) { PdfPCell pcell1 = new PdfPCell(new Paragraph(fontFamilySelector .process((j + 1) + "", FontContext.SMALL_NORMAL_HELVETICA))); pcell1.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell1.setBorderColor(Color.GRAY); pcell1.setPadding(4); pcell1.setHorizontalAlignment(Element.ALIGN_CENTER); pcell1.setVerticalAlignment(Element.ALIGN_CENTER); quesTable[i].addCell(pcell1); PdfPCell pcell2 = new PdfPCell(new Paragraph(fontFamilySelector.process( arr.getJSONObject(j).getString("" + j + ""), FontContext.SMALL_NORMAL_HELVETICA))); pcell2.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell2.setBorderColor(Color.GRAY); pcell2.setPadding(4); pcell2.setHorizontalAlignment(Element.ALIGN_LEFT); pcell2.setVerticalAlignment(Element.ALIGN_CENTER); quesTable[i].addCell(pcell2); } } else { for (int j = 0; j < cnt; j++) { PdfPCell pcell1 = new PdfPCell(new Paragraph(fontFamilySelector .process((j + 1) + "", FontContext.SMALL_NORMAL_HELVETICA))); pcell1.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell1.setBorderColor(Color.GRAY); pcell1.setPadding(4); pcell1.setHorizontalAlignment(Element.ALIGN_CENTER); pcell1.setVerticalAlignment(Element.ALIGN_CENTER); quesTable[i].addCell(pcell1); PdfPCell pcell = new PdfPCell(new Paragraph(fontFamilySelector.process("", FontContext.SMALL_NORMAL_HELVETICA))); pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); pcell.setVerticalAlignment(Element.ALIGN_CENTER); quesTable[i].addCell(pcell); } } PdfPCell pcell = new PdfPCell(new Paragraph( fontFamilySelector.process(" ", FontContext.SMALL_NORMAL_HELVETICA))); pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); pcell.setVerticalAlignment(Element.ALIGN_CENTER); //questionTable.addCell(pcell); document.add(quesTable[i]); } //document.add(questionTable); } catch (JSONException e) { e.printStackTrace(); } } } catch (JSONException e) { e.printStackTrace(); } } //Comp //Goals requestParams.clear(); requestParams.put("companyid", sessionHandlerImplObj.getCompanyid(request)); requestParams.put("checklink", "goal"); if (hrmsCommonDAOObj.checkModule(requestParams).isSuccessFlag()) { try { JSONArray jsonGoals = obj.getJSONArray(3); if (!isMyAppraisal) { String goalParam[] = { messageSource.getMessage("hrms.performance.goals", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.percent.completed", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.goal.weightage", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.rating", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.appraiser.comment", null, RequestContextUtils.getLocale(request)) }; String colHeader[] = { "gname", "gcomplete", "gwth", "gmanrat", "mangoalcomment" }; PdfPTable goalTable = null; if (companyPreferences.isWeightage()) { goalTable = new PdfPTable(5); goalTable.setWidths(new float[] { 50, 60, 30, 30, 42 }); } else { goalTable = new PdfPTable(4); goalTable.setWidths(new float[] { 50, 60, 30, 42 }); } goalTable.setWidthPercentage(100); goalTable.setSpacingBefore(20); goalTable.setHeaderRows(1); for (int i = 0; i < goalParam.length; i++) { if (!(!companyPreferences.isWeightage() && i == 2)) { PdfPCell pgcell = new PdfPCell(new Paragraph(fontFamilySelector .process(goalParam[i], FontContext.MEDIUM_BOLD_HELVETICA))); pgcell.setBorder(0); pgcell.setBorder(PdfPCell.BOX); pgcell.setPadding(4); pgcell.setBorderColor(Color.GRAY); pgcell.setHorizontalAlignment(Element.ALIGN_CENTER); goalTable.addCell(pgcell); } } for (int i = 0; i < jsonGoals.length(); i++) { for (int j = 0; j < colHeader.length; j++) { if (!(!companyPreferences.isWeightage() && j == 2)) { PdfPCell pcell; if (j != 3) pcell = new PdfPCell( new Paragraph( fontFamilySelector .process( jsonGoals.getJSONObject(i).has(colHeader[j]) ? jsonGoals.getJSONObject(i) .getString(colHeader[j]) : "", FontContext.SMALL_NORMAL_HELVETICA))); else pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( jsonGoals.getJSONObject(i).has(colHeader[j]) ? (jsonGoals.getJSONObject(i).getString(colHeader[j]) .equals("0") ? "" : jsonGoals.getJSONObject(i) .getString(colHeader[j])) : "", FontContext.SMALL_NORMAL_HELVETICA))); pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); pcell.setVerticalAlignment(Element.ALIGN_CENTER); goalTable.addCell(pcell); } } } document.add(goalTable); } else { String goalParam[] = { messageSource.getMessage("hrms.performance.goals", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.percent.completed", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.goal.weightage", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.rating", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.performance.self.comments", null, RequestContextUtils.getLocale(request)), messageSource.getMessage("hrms.common.assigned.by", null, RequestContextUtils.getLocale(request)) }; String colHeader[] = { "gname", "gcomplete", "gwth", "gemprat", "empgoalcomment", "assignedby" }; PdfPTable goalTable = null; if (companyPreferences.isWeightage()) { goalTable = new PdfPTable(6); goalTable.setWidths(new float[] { 50, 60, 30, 50, 42, 60 }); } else { goalTable = new PdfPTable(5); goalTable.setWidths(new float[] { 50, 60, 50, 42, 60 }); } goalTable.setWidthPercentage(100); goalTable.setSpacingBefore(20); goalTable.setHeaderRows(1); for (int i = 0; i < goalParam.length; i++) { if (!(!companyPreferences.isWeightage() && i == 2)) { PdfPCell pgcell = new PdfPCell(new Paragraph(fontFamilySelector .process(goalParam[i], FontContext.MEDIUM_BOLD_HELVETICA))); pgcell.setBorder(0); pgcell.setBorder(PdfPCell.BOX); pgcell.setPadding(4); pgcell.setBorderColor(Color.GRAY); pgcell.setHorizontalAlignment(Element.ALIGN_CENTER); goalTable.addCell(pgcell); } } for (int i = 0; i < jsonGoals.length(); i++) { for (int j = 0; j < colHeader.length; j++) { if (!(!companyPreferences.isWeightage() && j == 2)) { PdfPCell pcell; if (j != 3) pcell = new PdfPCell( new Paragraph( fontFamilySelector .process( jsonGoals.getJSONObject(i).has(colHeader[j]) ? jsonGoals.getJSONObject(i) .getString(colHeader[j]) : "", FontContext.SMALL_NORMAL_HELVETICA))); else pcell = new PdfPCell(new Paragraph(fontFamilySelector.process( jsonGoals.getJSONObject(i).has(colHeader[j]) ? (jsonGoals.getJSONObject(i).getString(colHeader[j]) .equals("0") ? "" : jsonGoals.getJSONObject(i) .getString(colHeader[j])) : "", FontContext.SMALL_NORMAL_HELVETICA))); pcell.setBorder(PdfPCell.BOTTOM | PdfPCell.LEFT | PdfPCell.RIGHT); pcell.setBorderColor(Color.GRAY); pcell.setPadding(4); pcell.setHorizontalAlignment(Element.ALIGN_CENTER); pcell.setVerticalAlignment(Element.ALIGN_CENTER); goalTable.addCell(pcell); } } } document.add(goalTable); } } catch (JSONException e) { e.printStackTrace(); } } //Goals document.newPage(); document.close(); response.setHeader("Content-Disposition", "attachment; filename=\"" + "App_Form" + ".pdf\""); response.setContentType("application/octet-stream"); response.setContentLength(baos.size()); response.getOutputStream().write(baos.toByteArray()); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { writer.close(); } }
From source file:com.preparatic.archivos.PdfGenerator.java
License:Apache License
/** * Genera un pdf para las preguntas y otro para las soluciones. * /* ww w. j ava 2s. co m*/ * @throws Exception */ public String guardarPDF() throws Exception { // Abrir documentos docpregs = new Document(); docsols = new Document(); String pregsFilename = FactoriaArchivo.pdfTestFilename(test.getTipoTest(), test.getIdBloqueTematicaAnho(), test.getIdTest()); PdfWriter writerPregs = PdfWriter.getInstance(docpregs, FactoriaArchivo.pdfTest(test.getTipoTest(), test.getIdBloqueTematicaAnho(), test.getIdTest())); TestFooter footerPregs = new TestFooter(test.getTitulo()); //mod_AZ_2018-02-04 writerPregs.setPageEvent(footerPregs); PdfWriter.getInstance(docsols, FactoriaArchivo.pdfSol(test.getTipoTest(), test.getIdBloqueTematicaAnho(), test.getIdTest())); docpregs.open(); docsols.open(); docpregs.setMargins(80, 80, 72, 36); docsols.setMargins(80, 80, 36, 36); // Aadir logo Image gif = Image.getInstance(ConfigProperties.getProperty("files.rootDir") + "/images/logo-alpha.png"); //mod_AZE_2018-02-04 (logo con fondo blanco) gif.setAlignment(Image.LEFT); gif.scaleAbsolute(148, 46); //mod_AZE_2018-02-04 (para que entre bien el logo nuevo) docpregs.add(gif); docsols.add(gif); ponerTitulo(docpregs, docsols); // Adimos las preguntas al documento de preguntas docpregs.add(listaPreguntas); /* * Aadimos las respuestas, en varias columnas al documento de * respuestas. */ MultiColumnText multiColumnTextSoluciones = new MultiColumnText(); multiColumnTextSoluciones.addRegularColumns(docsols.left(), docsols.right(), 20f, Integer.parseInt(ConfigProperties.getProperty("tests.solucion.columnas"))); multiColumnTextSoluciones.addElement(listaSoluciones); docsols.add(multiColumnTextSoluciones); // Cerramos los documentos docpregs.close(); docsols.close(); return pregsFilename; }
From source file:com.prime.location.billing.InvoicedBillingManager.java
/** * Print invoice// ww w .ja v a 2s. c om */ public void printInvoice() { try { //catch better your exceptions, this is just an example FacesContext context = FacesContext.getCurrentInstance(); Document pdf = new Document(PageSize.A4, 5f, 5f, 75f, 45f); String fileName = "PDFFile"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(pdf, baos); TableHeader event = new TableHeader(); event.setHeader("Header Here"); writer.setPageEvent(event); if (!pdf.isOpen()) { pdf.open(); } PdfPCell cell; PdfPTable header = new PdfPTable(new float[] { 1, 2, 1 }); cell = new PdfPCell(new Paragraph("INVOICE", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC, Color.BLACK))); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(3); cell.setBorder(Rectangle.NO_BORDER); header.addCell(cell); header.setTotalWidth(527); header.setLockedWidth(true); cell = new PdfPCell(new Phrase("Agency: " + selectedAgency.getDescription())); cell.setColspan(2); cell.setBorder(Rectangle.NO_BORDER); header.addCell(cell); cell = new PdfPCell(new Phrase("Invoice#: " + selectedInvoice.getId())); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); header.addCell(cell); cell = new PdfPCell( new Phrase("Date: " + DateUtility.dateTimeFormat(selectedInvoice.getInvoiceDate()))); cell.setColspan(3); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); header.addCell(cell); pdf.add(header); PdfPTable table = new PdfPTable(new float[] { 1, 3, 2, 1 }); table.setSpacingBefore(15f); table.setTotalWidth(527); table.setLockedWidth(true); //table.setWidths(new int[]{3, 1, 1}); table.getDefaultCell().setBackgroundColor(Color.LIGHT_GRAY); table.addCell("Date"); table.addCell("Name"); table.addCell("Service"); table.addCell("Rate"); table.getDefaultCell().setBackgroundColor(null); cell = new PdfPCell(new Phrase("Total(US$): ")); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setColspan(3); cell.setBackgroundColor(Color.LIGHT_GRAY); table.addCell(cell); cell = new PdfPCell(new Phrase(new DecimalFormat("###,###.###").format(selectedInvoice.getAmount()))); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBackgroundColor(Color.LIGHT_GRAY); table.addCell(cell); // There are three special rows table.setHeaderRows(2); // One of them is a footer table.setFooterRows(1); Font f = FontFactory.getFont(FontFactory.HELVETICA, 10); //add remaining for (AgencyBilling billing : selectedInvoice.getBillings()) { table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); table.getDefaultCell().setIndent(2); table.getDefaultCell().setFixedHeight(20); table.addCell(new Phrase(DateUtility.dateFormat(billing.getBillingDate()), f)); table.addCell(new Phrase(billing.getPerson().getName(), f)); table.addCell(new Phrase(billing.getTariff().getTariffType().getDescription(), f)); cell = new PdfPCell(new Phrase(new DecimalFormat("###,###.###").format(billing.getRate()), f)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); table.addCell(cell); } pdf.add(table); writer.getAcroForm().setNeedAppearances(true); //document.add(new Phrase(TEXT)); //Keep modifying your pdf file (add pages and more) pdf.close(); writePDFToResponse(context.getExternalContext(), baos, fileName); context.responseComplete(); } catch (Exception e) { //e.printStackTrace(); } }
From source file:com.qcadoo.plugins.qcadooExport.internal.ExportToPDFController.java
License:Open Source License
@Monitorable(threshold = 500) @ResponseBody/* w w w. ja va2 s . co m*/ @RequestMapping(value = { CONTROLLER_PATH }, method = RequestMethod.POST) public Object generatePdf(@PathVariable(PLUGIN_IDENTIFIER_VARIABLE) final String pluginIdentifier, @PathVariable(VIEW_NAME_VARIABLE) final String viewName, @RequestBody final JSONObject body, final Locale locale) { try { changeMaxResults(body); ViewDefinitionState state = crudService.invokeEvent(pluginIdentifier, viewName, body, locale); GridComponent grid = (GridComponent) state.getComponentByReference("grid"); Document document = new Document(PageSize.A4.rotate()); String date = DateFormat.getDateInstance().format(new Date()); File file = fileService.createExportFile("export_" + grid.getName() + "_" + date + ".pdf"); FileOutputStream fileOutputStream = new FileOutputStream(file); PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream); writer.setPageEvent(new PdfPageNumbering(footerResolver.resolveFooter(locale))); document.setMargins(40, 40, 60, 60); document.addTitle("export.pdf"); pdfHelper.addMetaData(document); writer.createXmpMetadata(); document.open(); String title = translationService.translate( pluginIdentifier + "." + viewName + ".window.mainTab." + grid.getName() + ".header", locale); Date generationDate = new Date(); pdfHelper.addDocumentHeader(document, "", title, translationService.translate("qcadooReport.commons.generatedBy.label", locale), generationDate); int columns = 0; List<String> exportToPDFTableHeader = new ArrayList<String>(); for (String name : grid.getColumnNames().values()) { exportToPDFTableHeader.add(name); columns++; } PdfPTable table = pdfHelper.createTableWithHeader(columns, exportToPDFTableHeader, false); List<Map<String, String>> rows; if (grid.getSelectedEntitiesIds().isEmpty()) { rows = grid.getColumnValuesOfAllRecords(); } else { rows = grid.getColumnValuesOfSelectedRecords(); } for (Map<String, String> row : rows) { for (String value : row.values()) { table.addCell(new Phrase(value, FontUtils.getDejavuRegular7Dark())); } } document.add(table); document.close(); state.redirectTo(fileService.getUrl(file.getAbsolutePath()) + "?clean", true, false); return crudService.renderView(state); } catch (JSONException e) { throw new IllegalStateException(e.getMessage(), e); } catch (FileNotFoundException e) { throw new IllegalStateException(e.getMessage(), e); } catch (DocumentException e) { throw new IllegalStateException(e.getMessage(), e); } }
From source file:com.qcadoo.report.api.pdf.PdfDocumentService.java
License:Open Source License
private void generate(final Entity entity, final Locale locale, final String filename, final Rectangle pageSize) throws IOException, DocumentException { Document document = new Document(pageSize); try {/*www .ja v a 2 s. c o m*/ FileOutputStream fileOutputStream = new FileOutputStream( fileService.createReportFile(filename + "." + ReportService.ReportType.PDF.getExtension())); PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream); writer.setPageEvent(new PdfPageNumbering(footerResolver.resolveFooter(locale))); document.setMargins(40, 40, 60, 60); buildPdfMetadata(document, locale); writer.createXmpMetadata(); document.open(); buildPdfContent(document, entity, locale); document.close(); } catch (DocumentException e) { LOG.error("Problem with generating document - " + e.getMessage()); document.close(); throw e; } }