List of usage examples for com.lowagie.text.pdf PdfReader getPageSizeWithRotation
public Rectangle getPageSizeWithRotation(PdfDictionary page)
From source file:org.kuali.coeus.common.impl.print.watermark.WatermarkServiceImpl.java
License:Open Source License
/** * /*w w w.ja v a 2s . c o m*/ * This method for Decorating the PDF with watermark. * * @param watermarkPdfStamper - wrapper for pdf content byte and assists in decorating PDF LOg the exception if cannot open/read the file * for decoration */ private void decorateWatermark(PdfStamper watermarkPdfStamper, WatermarkBean watermarkBean) { watermarkPdfStamper.setFormFlattening(true); PdfReader pdfReader = watermarkPdfStamper.getReader(); int pageCount = pdfReader.getNumberOfPages(); int pdfPageNumber = 0; PdfContentByte pdfContents; Rectangle rectangle; while (pdfPageNumber < pageCount) { pdfPageNumber++; pdfContents = watermarkPdfStamper.getOverContent(pdfPageNumber); rectangle = pdfReader.getPageSizeWithRotation(pdfPageNumber); if (watermarkBean.getType().equalsIgnoreCase(WatermarkConstants.WATERMARK_TYPE_IMAGE)) { decoratePdfWatermarkImage(pdfContents, (int) rectangle.getWidth(), (int) rectangle.getHeight(), watermarkBean); } if (watermarkBean.getType().equalsIgnoreCase(WatermarkConstants.WATERMARK_TYPE_TEXT)) { decoratePdfWatermarkText(pdfContents, rectangle, watermarkBean); } watermarkPdfStamper.setFormFlattening(true); } try { watermarkPdfStamper.close(); } catch (IOException decorateWatermark) { LOG.error("Exception occured in WatermarkServiceImpl. decorateWatermark Exception: " + decorateWatermark.getMessage()); } catch (DocumentException documentException) { LOG.error("Exception occured in WatermarkServiceImpl. decorateWatermark Exception: " + documentException.getMessage()); } }
From source file:org.kuali.coeus.s2sgen.impl.print.S2SPrintingServiceImpl.java
License:Educational Community License
/** * @param pdfBytesList List containing the PDF data bytes * @param bookmarksList List of bookmarks corresponding to the PDF bytes. *//*from w ww. j a va 2 s. c o m*/ protected byte[] mergePdfBytes(List<byte[]> pdfBytesList, List<String> bookmarksList, boolean headerFooterRequired) { Document document = null; PdfWriter writer = null; ByteArrayOutputStream mergedPdfReport = new ByteArrayOutputStream(); int totalNumOfPages = 0; PdfReader[] pdfReaderArr = new PdfReader[pdfBytesList.size()]; int pdfReaderCount = 0; for (byte[] fileBytes : pdfBytesList) { LOG.debug("File Size " + fileBytes.length + " For " + bookmarksList.get(pdfReaderCount)); PdfReader reader = null; try { reader = new PdfReader(fileBytes); pdfReaderArr[pdfReaderCount] = reader; pdfReaderCount = pdfReaderCount + 1; totalNumOfPages += reader.getNumberOfPages(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } HeaderFooter footer = null; if (headerFooterRequired) { Calendar calendar = Calendar.getInstance(); String dateString = formateCalendar(calendar); StringBuilder footerPhStr = new StringBuilder(); footerPhStr.append(" of "); footerPhStr.append(totalNumOfPages); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_76)); footerPhStr.append(getWhitespaceString(WHITESPACE_LENGTH_60)); footerPhStr.append(dateString); Font font = FontFactory.getFont(FontFactory.TIMES, 8, Font.NORMAL, Color.BLACK); Phrase beforePhrase = new Phrase("Page ", font); Phrase afterPhrase = new Phrase(footerPhStr.toString(), font); footer = new HeaderFooter(beforePhrase, afterPhrase); footer.setAlignment(Element.ALIGN_BASELINE); footer.setBorderWidth(0f); } for (int count = 0; count < pdfReaderArr.length; count++) { PdfReader reader = pdfReaderArr[count]; int nop; if (reader == null) { LOG.debug("Empty PDF byetes found for " + bookmarksList.get(count)); continue; } else { nop = reader.getNumberOfPages(); } if (count == 0) { document = nop > 0 ? new Document(reader.getPageSizeWithRotation(1)) : new Document(); try { writer = PdfWriter.getInstance(document, mergedPdfReport); } catch (DocumentException e) { LOG.error(e.getMessage(), e); throw new S2SException(e.getMessage(), e); } if (footer != null) { document.setFooter(footer); } document.open(); } PdfContentByte cb = writer.getDirectContent(); int pageCount = 0; while (pageCount < nop) { document.setPageSize(reader.getPageSize(++pageCount)); document.newPage(); if (footer != null) { document.setFooter(footer); } PdfImportedPage page = writer.getImportedPage(reader, pageCount); cb.addTemplate(page, 1, 0, 0, 1, 0, 0); PdfOutline root = cb.getRootOutline(); if (pageCount == 1) { String pageName = bookmarksList.get(count); cb.addOutline(new PdfOutline(root, new PdfDestination(PdfDestination.FITH), pageName), pageName); } } } if (document != null) { try { document.close(); return mergedPdfReport.toByteArray(); } catch (Exception e) { LOG.error("Exception occured because the generated PDF document has no pages", e); } } return null; }
From source file:org.kuali.ext.mm.document.web.struts.CountWorksheetPrintAction.java
License:Educational Community License
private void combineAndFlushReportPDFFiles(List<File> fileList, HttpServletRequest request, HttpServletResponse response) throws Exception { long startTime = System.currentTimeMillis(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ArrayList master = new ArrayList(); int pageOffset = 0; int f = 0;//from ww w . j a v a 2 s .c o m PdfCopy writer = null; com.lowagie.text.Document document = null; for (File file : fileList) { // we create a reader for a certain document String reportName = file.getAbsolutePath(); PdfReader reader = new PdfReader(reportName); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += n; if (f == 0) { // step 1: creation of a document-object document = new com.lowagie.text.Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); f++; } if (!master.isEmpty()) writer.setOutlines(master); // step 5: we close the document document.close(); StringBuffer sbContentDispValue = new StringBuffer(); String useJavascript = request.getParameter("useJavascript"); if (useJavascript == null || useJavascript.equalsIgnoreCase("false")) { sbContentDispValue.append("attachment"); } else { sbContentDispValue.append("inline"); } sbContentDispValue.append("; filename="); sbContentDispValue.append(MMUtil.getFileName()); String contentDisposition = sbContentDispValue.toString(); response.setContentType("application/pdf"); response.setHeader("Content-disposition", contentDisposition); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentLength(baos.size()); // write to output ServletOutputStream sos = response.getOutputStream(); baos.writeTo(sos); sos.flush(); baos.close(); sos.close(); long endTime = System.currentTimeMillis(); loggerAc.debug("Time taken for report Parameter settings in action " + (endTime - startTime)); }
From source file:org.kuali.ext.mm.service.impl.CountWorksheetReportServiceImpl.java
License:Educational Community License
public ByteArrayOutputStream combineAndFlushReportPDFFiles(List<File> fileList) throws Exception { long startTime = System.currentTimeMillis(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ArrayList master = new ArrayList(); int pageOffset = 0; int f = 0;//from www. j a v a 2s . co m PdfCopy writer = null; com.lowagie.text.Document document = null; for (File file : fileList) { // we create a reader for a certain document String reportName = file.getAbsolutePath(); PdfReader reader = new PdfReader(reportName); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += n; if (f == 0) { // step 1: creation of a document-object document = new com.lowagie.text.Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); f++; } if (!master.isEmpty()) writer.setOutlines(master); // step 5: we close the document document.close(); return baos; }
From source file:org.kuali.ext.mm.service.impl.CountWorksheetReportServiceImpl.java
License:Educational Community License
public ByteArrayOutputStream combineAndFlushReportPDFStreams(List<ByteArrayOutputStream> fileList) throws Exception { long startTime = System.currentTimeMillis(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ArrayList master = new ArrayList(); int pageOffset = 0; int f = 0;/* ww w . java 2 s .c o m*/ PdfCopy writer = null; com.lowagie.text.Document document = null; for (ByteArrayOutputStream file : fileList) { // we create a reader for a certain document // String reportName = file.getAbsolutePath(); PdfReader reader = new PdfReader(file.toByteArray()); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += n; if (f == 0) { // step 1: creation of a document-object document = new com.lowagie.text.Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); f++; } if (!master.isEmpty()) writer.setOutlines(master); // step 5: we close the document document.close(); return baos; }
From source file:org.kuali.kfs.gl.web.struts.TrialBalanceReportAction.java
License:Open Source License
/** * Generate pdf for sending response using itext * * @param reportFileFullName/* w ww . ja v a 2 s.c o m*/ * @return * @throws IOException * @throws DocumentException * @throws BadPdfFormatException */ protected ByteArrayOutputStream generatePdfOutStream(String reportFileFullName) throws IOException, DocumentException, BadPdfFormatException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // we create a reader for a certain document PdfReader reader = new PdfReader(reportFileFullName); reader.consolidateNamedDestinations(); // step 1: creation of a document-object Document document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document PdfCopy writer = new PdfCopy(document, baos); // step 3: we open the document document.open(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); // step 5: we close the document document.close(); return baos; }
From source file:org.kuali.kfs.module.ar.document.web.struts.CustomerCreditMemoDocumentAction.java
License:Educational Community License
/** * /*from w w w . j av a2 s. co m*/ * This method... * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward printCreditMemoPDF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String creditMemoDocId = request.getParameter(KFSConstants.PARAMETER_DOC_ID); CustomerCreditMemoDocument customerCreditMemoDocument = (CustomerCreditMemoDocument) SpringContext .getBean(DocumentService.class).getByDocumentHeaderId(creditMemoDocId); AccountsReceivableReportService reportService = SpringContext .getBean(AccountsReceivableReportService.class); File report = reportService.generateCreditMemo(customerCreditMemoDocument); StringBuilder fileName = new StringBuilder(); fileName.append(customerCreditMemoDocument.getFinancialDocumentReferenceInvoiceNumber()); fileName.append("-"); fileName.append(customerCreditMemoDocument.getDocumentNumber()); fileName.append(".pdf"); if (report.length() == 0) { //csForm.setMessage("No Report Generated"); return mapping.findForward(KFSConstants.MAPPING_BASIC); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); String contentDisposition = ""; try { ArrayList master = new ArrayList(); PdfCopy writer = null; // create a reader for the document String reportName = report.getAbsolutePath(); PdfReader reader = new PdfReader(reportName); reader.consolidateNamedDestinations(); // retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) master.addAll(bookmarks); // step 1: create a document-object Document document = new Document(reader.getPageSizeWithRotation(1)); // step 2: create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: open the document document.open(); // step 4: add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); if (!master.isEmpty()) writer.setOutlines(master); // step 5: we close the document document.close(); StringBuffer sbContentDispValue = new StringBuffer(); String useJavascript = request.getParameter("useJavascript"); if (useJavascript == null || useJavascript.equalsIgnoreCase("false")) { sbContentDispValue.append("attachment"); } else { sbContentDispValue.append("inline"); } sbContentDispValue.append("; filename="); sbContentDispValue.append(fileName); contentDisposition = sbContentDispValue.toString(); } catch (Exception e) { e.printStackTrace(); } response.setContentType("application/pdf"); response.setHeader("Content-Disposition", contentDisposition); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentLength(baos.size()); // write to output ServletOutputStream sos; sos = response.getOutputStream(); baos.writeTo(sos); sos.flush(); sos.close(); return null; }
From source file:org.kuali.kfs.module.ar.document.web.struts.CustomerInvoiceDocumentAction.java
License:Educational Community License
/** * // w ww . j a v a 2 s . c o m * This method... * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward printInvoicePDF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String invoiceDocId = request.getParameter(KFSConstants.PARAMETER_DOC_ID); CustomerInvoiceDocument customerInvoiceDocument = (CustomerInvoiceDocument) SpringContext .getBean(DocumentService.class).getByDocumentHeaderId(invoiceDocId); AccountsReceivableReportService reportService = SpringContext .getBean(AccountsReceivableReportService.class); File report = reportService.generateInvoice(customerInvoiceDocument); StringBuilder fileName = new StringBuilder(); fileName.append(customerInvoiceDocument.getOrganizationInvoiceNumber()); fileName.append("-"); fileName.append(customerInvoiceDocument.getDocumentNumber()); fileName.append(".pdf"); if (report.length() == 0) { //csForm.setMessage("No Report Generated"); return mapping.findForward(KFSConstants.MAPPING_BASIC); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); String contentDisposition = ""; try { ArrayList master = new ArrayList(); PdfCopy writer = null; // create a reader for the document String reportName = report.getAbsolutePath(); PdfReader reader = new PdfReader(reportName); reader.consolidateNamedDestinations(); // retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { master.addAll(bookmarks); } // step 1: create a document-object Document document = new Document(reader.getPageSizeWithRotation(1)); // step 2: create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: open the document document.open(); // step 4: add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); if (!master.isEmpty()) { writer.setOutlines(master); } // step 5: we close the document document.close(); StringBuffer sbContentDispValue = new StringBuffer(); String useJavascript = request.getParameter("useJavascript"); if (useJavascript == null || useJavascript.equalsIgnoreCase("false")) { sbContentDispValue.append("attachment"); } else { sbContentDispValue.append("inline"); } sbContentDispValue.append("; filename="); sbContentDispValue.append(fileName); contentDisposition = sbContentDispValue.toString(); } catch (Exception e) { e.printStackTrace(); } response.setContentType("application/pdf"); response.setHeader("Content-Disposition", contentDisposition); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentLength(baos.size()); // write to output ServletOutputStream sos; sos = response.getOutputStream(); baos.writeTo(sos); sos.flush(); sos.close(); return null; }
From source file:org.kuali.kfs.module.ar.service.impl.AccountsReceivablePdfHelperServiceImpl.java
License:Open Source License
@Override public ByteArrayOutputStream buildPdfOutputStream(List<byte[]> contents) throws IOException, DocumentException, BadPdfFormatException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ArrayList<PdfReader> master = new ArrayList<>(); Document document = null;/*ww w .ja va2 s.com*/ PdfCopy writer = null; boolean createDocument = true; for (byte[] content : contents) { // create a reader for the document PdfReader reader = new PdfReader(content); reader.consolidateNamedDestinations(); // retrieve the total number of pages int n = reader.getNumberOfPages(); List<PdfReader> bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { master.addAll(bookmarks); } if (createDocument) { // step 1: create a document-object document = new Document(reader.getPageSizeWithRotation(1)); // step 2: create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: open the document document.open(); } // step 4: add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); createDocument = false; } if (!master.isEmpty()) { writer.setOutlines(master); } // step 5: we close the document document.close(); return baos; }
From source file:org.kuali.kfs.module.ar.web.struts.CustomerInvoiceAction.java
License:Open Source License
/** * * This method.../*w ww . j av a 2s.c o m*/ * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward print(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CustomerInvoiceForm ciForm = (CustomerInvoiceForm) form; String org = ciForm.getOrgCode(); String chart = ciForm.getChartCode(); Date date = ciForm.getRunDate(); StringBuilder fileName = new StringBuilder(); AccountsReceivableReportService reportService = SpringContext .getBean(AccountsReceivableReportService.class); List<File> reports = new ArrayList<File>(); if (ciForm.getOrgType() != null && chart != null && org != null) { if (ciForm.getOrgType().equals("B")) { reports = reportService.generateInvoicesByBillingOrg(chart, org, date); } else if (ciForm.getOrgType().equals("P")) { reports = reportService.generateInvoicesByProcessingOrg(chart, org, date); } fileName.append(chart); fileName.append(org); if (date != null) { fileName.append(date); } } else if (ciForm.getUserId() != null) { reports = reportService.generateInvoicesByInitiator(ciForm.getUserId(), date); fileName.append(ciForm.getUserId()); } if (reports.size() > 0) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int pageOffset = 0; ArrayList master = new ArrayList(); int f = 0; Document document = null; PdfCopy writer = null; for (Iterator<File> itr = reports.iterator(); itr.hasNext();) { // we create a reader for a certain document String reportName = itr.next().getAbsolutePath(); PdfReader reader = new PdfReader(reportName); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += n; if (f == 0) { // step 1: creation of a document-object document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); f++; } if (!master.isEmpty()) { writer.setOutlines(master); // step 5: we close the document } document.close(); fileName.append("-InvoiceBatchPDFs.pdf"); WebUtils.saveMimeOutputStreamAsFile(response, "application/pdf", baos, fileName.toString()); ciForm.setMessage(reports.size() + " Reports Generated"); return null; } ciForm.setMessage("No Reports Generated"); return mapping.findForward(KFSConstants.MAPPING_BASIC); }