List of usage examples for com.lowagie.text.pdf PdfCopy freeReader
public void freeReader(PdfReader reader) throws IOException
From source file:com.sapienter.jbilling.server.invoice.PaperInvoiceBatchBL.java
License:Open Source License
/** * Takes a list of invoices and replaces the individual PDF files for one * single PDF in the destination directory. * @param destination/*from w w w . j av a 2s .c o m*/ * @param prefix * @param entityId * @param invoices * @throws PdfFormatException * @throws IOException */ public void compileInvoiceFiles(String destination, String prefix, Integer entityId, Integer[] invoices) throws DocumentException, IOException { String filePrefix = Util.getSysProp("base_dir") + "invoices/" + entityId + "-"; String outFile = destination + prefix + "-batch.pdf"; int pageOffset = 0; ArrayList master = new ArrayList(); Document document = null; PdfCopy writer = null; for (int f = 0; f < invoices.length; f++) { // we create a reader for a certain document PdfReader reader = new PdfReader(filePrefix + invoices[f] + "-invoice.pdf"); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int numberOfPages = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); master.addAll(bookmarks); } pageOffset += numberOfPages; 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, new FileOutputStream(outFile)); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < numberOfPages;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } PRAcroForm form = reader.getAcroForm(); if (form != null) writer.copyAcroForm(reader); //release and delete writer.freeReader(reader); reader.close(); File file = new File(filePrefix + invoices[f] + "-invoice.pdf"); file.delete(); } if (!master.isEmpty()) writer.setOutlines(master); // step 5: we close the document if (document != null) { document.close(); } else { LOG.warn("document == null"); } LOG.debug("PDF batch file is ready " + outFile); }
From source file:it.pdfsam.console.tools.pdf.PdfAlternateMix.java
License:Open Source License
/** * Execute the mix command. On error an exception is thrown. * @throws AlternateMixException//from www.j av a 2 s.c o m */ public void execute() throws AlternateMixException { try { workingIndeterminate(); out_message = ""; Document pdf_document = null; PdfCopy pdf_writer = null; File tmp_o_file = TmpFileNameGenerator.generateTmpFile(o_file.getParent()); PdfReader pdf_reader1; PdfReader pdf_reader2; pdf_reader1 = new PdfReader(new RandomAccessFileOrArray(input_file1.getAbsolutePath()), null); pdf_reader1.consolidateNamedDestinations(); limits1[1] = pdf_reader1.getNumberOfPages(); pdf_reader2 = new PdfReader(new RandomAccessFileOrArray(input_file2.getAbsolutePath()), null); pdf_reader2.consolidateNamedDestinations(); limits2[1] = pdf_reader2.getNumberOfPages(); pdf_document = new Document(pdf_reader1.getPageSizeWithRotation(1)); pdf_writer = new PdfCopy(pdf_document, new FileOutputStream(tmp_o_file)); if (compressed_boolean) { pdf_writer.setFullCompression(); } out_message += LogFormatter.formatMessage("Temporary file created-\n"); MainConsole.setDocumentCreator(pdf_document); pdf_document.open(); PdfImportedPage page; //importo boolean finished1 = false; boolean finished2 = false; int current1 = (reverseFirst) ? limits1[1] : limits1[0]; int current2 = (reverseSecond) ? limits2[1] : limits2[0]; while (!finished1 || !finished2) { if (!finished1) { if (current1 >= limits1[0] && current1 <= limits1[1]) { page = pdf_writer.getImportedPage(pdf_reader1, current1); pdf_writer.addPage(page); current1 = (reverseFirst) ? (current1 - 1) : (current1 + 1); } else { out_message += LogFormatter.formatMessage("First file processed-\n"); finished1 = true; } } if (!finished2) { if (current2 >= limits2[0] && current2 <= limits2[1] && !finished2) { page = pdf_writer.getImportedPage(pdf_reader2, current2); pdf_writer.addPage(page); current2 = (reverseSecond) ? (current2 - 1) : (current2 + 1); } else { out_message += LogFormatter.formatMessage("Second file processed-\n"); finished2 = true; } } } pdf_reader1.close(); pdf_writer.freeReader(pdf_reader1); pdf_reader2.close(); pdf_writer.freeReader(pdf_reader2); pdf_document.close(); // step 6: temporary buffer moved to output file renameTemporaryFile(tmp_o_file, o_file, overwrite_boolean); out_message += LogFormatter.formatMessage("Alternate mix completed-\n"); } catch (Exception e) { throw new AlternateMixException(e); } finally { workCompleted(); } }
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 w w w. j av a2 s.c om 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 ww w . j a va2 s. c om 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;// w ww. j a va2s .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 a2 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 .ja va 2s. 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
/** * //from w ww . j ava 2s .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;// w w w. j a v a 2 s . c om 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.../*from w w w.j av a 2 s .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); }