private ByteArrayOutputStream getPdfData( JSONArray data, JSONArray res, HttpServletRequest request) throws ServiceException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { String[] colHeader = getColHeader(); String[] colIndex = getColIndexes(); String[] val = getColValues(colHeader, data); String[] resources = getResourcesColHeader(res, data); String[] mainHeader = {"Dates", "Duration", "Work", "Cost", "Tasks", "Resources"}; Document document = null; if (landscape) { Rectangle recPage = new Rectangle(PageSize.A4.rotate()); recPage.setBackgroundColor(new java.awt.Color(255, 255, 255)); document = new Document(recPage, 15, 15, 30, 30); } else { Rectangle recPage = new Rectangle(PageSize.A4); recPage.setBackgroundColor(new java.awt.Color(255, 255, 255)); document = new Document(recPage, 15, 15, 30, 30); } PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new EndPage()); document.open(); if (showLogo) { getCompanyDetails(request); addComponyLogo(document, request); } addTitleSubtitle(document); addTable(data, resources, colIndex, colHeader, mainHeader, val, document); document.close(); writer.close(); baos.close(); } catch (ConfigurationException ex) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex); } catch (DocumentException ex) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex); } catch (JSONException e) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e); } catch (IOException e) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e); } catch (Exception e) { throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e); } return baos; }
@Override public void onEndPage(PdfWriter writer, Document document) { try { Rectangle page = document.getPageSize(); getHeaderFooter(document); // Add page header header.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); header.writeSelectedRows( 0, -1, document.leftMargin(), page.getHeight() - 10, writer.getDirectContent()); // Add page footer footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); footer.writeSelectedRows( 0, -1, document.leftMargin(), document.bottomMargin() - 5, writer.getDirectContent()); // Add page border int bmargin = 8; // border margin PdfContentByte cb = writer.getDirectContent(); cb.rectangle( bmargin, bmargin, page.getWidth() - bmargin * 2, page.getHeight() - bmargin * 2); cb.setColorStroke(Color.LIGHT_GRAY); cb.stroke(); } catch (JSONException e) { Logger.getLogger(ExportProjectReportServlet.class.getName()).log(Level.SEVERE, null, e); throw new ExceptionConverter(e); } }