List of usage examples for com.lowagie.text.pdf PdfWriter getImportedPage
public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber)
From source file:org.areasy.common.doclet.utilities.PDFUtility.java
License:Open Source License
/** * Inserts pages from a PDF file into the document. If only one * page should be imported, both parameters must have the value * of that page number./*from w w w. j a v a 2 s .co m*/ * * @param reader The reader from which to import the pages. * @param from The first page to import. * @param to The last page to import. * @throws Exception */ private static void insertPdfPages(PdfReader reader, int from, int to, boolean createPage) throws Exception { PdfWriter writer = Document.getWriter(); if (from == to) { if (createPage) Document.newPage(); PdfImportedPage page = writer.getImportedPage(reader, from); writer.getDirectContent().addTemplate(page, 0.94f, 0, 0, 0.94f, 0, 30.0f); } else { for (int pageNo = from; pageNo < to + 1; pageNo++) { if (createPage) Document.newPage(); PdfImportedPage page = writer.getImportedPage(reader, pageNo); writer.getDirectContent().addTemplate(page, 0.94f, 0, 0, 0.94f, 0, 30.0f); createPage = true; } } }
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * Resize./*w w w . ja v a 2 s . co m*/ * * @param _parameter Parameter as passed by the eFaps API * @param _file the file * @param _fileName the file name * @param _pageSize the page size * @return the file * @throws EFapsException on error */ public File resizePdf(final Parameter _parameter, final File _file, final String _fileName, final String _pageSize) throws EFapsException { final Document document = new Document(); final File ret = getFile(_fileName, "pdf"); try { final File destFile = new File(_file.getPath() + ".tmp"); FileUtils.copyFile(_file, destFile); final OutputStream outputStream = new FileOutputStream(ret); final PdfReader pdfReader = new PdfReader(new FileInputStream(destFile)); // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfImportedPage page; final PdfContentByte cb = writer.getDirectContent(); int pageOfCurrentReaderPDF = 0; // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); document.setPageSize(page.getWidth() <= page.getHeight() ? PageSize.getRectangle(_pageSize) : PageSize.getRectangle(_pageSize).rotate()); final float widthFactor = document.getPageSize().getWidth() / page.getWidth(); final float heightFactor = document.getPageSize().getHeight() / page.getHeight(); final float factor = Math.min(widthFactor, heightFactor); final float offsetX = (document.getPageSize().getWidth() - page.getWidth() * factor) / 2; final float offsetY = (document.getPageSize().getHeight() - page.getHeight() * factor) / 2; cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); } pageOfCurrentReaderPDF = 0; outputStream.flush(); document.close(); outputStream.close(); } catch (final FileNotFoundException e) { LOG.error("FileNotFoundException", e); } catch (final IOException e) { LOG.error("IOException", e); } catch (final DocumentException e) { LOG.error("DocumentException", e); } return ret; }
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * N up.// w w w .j a v a 2s . c o m * * @param _parameter Parameter as passed by the eFaps API * @param _file the file * @param _fileName the file name * @return the file * @throws EFapsException on error */ public File nUpPdf(final Parameter _parameter, final File _file, final String _fileName) throws EFapsException { final File ret = getFile(_fileName, "pdf"); try { final int pow = Integer.parseInt(getProperty(_parameter, "NUpPow", "1")); final boolean duplicate = "true".equalsIgnoreCase(getProperty(_parameter, "NUpDuplicate", "true")); final File destFile = new File(_file.getPath() + ".tmp"); FileUtils.copyFile(_file, destFile); final OutputStream outputStream = new FileOutputStream(ret); final PdfReader pdfReader = new PdfReader(new FileInputStream(destFile)); final Rectangle pageSize = pdfReader.getPageSize(1); final Rectangle newSize = pow % 2 == 0 ? new Rectangle(pageSize.getWidth(), pageSize.getHeight()) : new Rectangle(pageSize.getHeight(), pageSize.getWidth()); Rectangle unitSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight()); for (int i = 0; i < pow; i++) { unitSize = new Rectangle(unitSize.getHeight() / 2, unitSize.getWidth()); } final int n = (int) Math.pow(2, pow); final int r = (int) Math.pow(2, pow / 2); final int c = n / r; final Document document = new Document(newSize, 0, 0, 0, 0); // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfImportedPage page; final PdfContentByte cb = writer.getDirectContent(); // Create a new page in the target for each source page. Rectangle currentSize; float offsetX; float offsetY; float factor; final int total = pdfReader.getNumberOfPages(); for (int i = 0; i < total;) { if (i % n == 0) { document.newPage(); } currentSize = pdfReader.getPageSize(++i); factor = Math.min(unitSize.getWidth() / currentSize.getWidth(), unitSize.getHeight() / currentSize.getHeight()); offsetX = unitSize.getWidth() * (i % n % c) + (unitSize.getWidth() - currentSize.getWidth() * factor) / 2f; offsetY = newSize.getHeight() - (unitSize.getHeight() * (i % n % c) + 1) + (unitSize.getHeight() - currentSize.getHeight() * factor) / 2f; page = writer.getImportedPage(pdfReader, i); cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); if (duplicate) { for (int y = i + 1; y <= pow + 1; y++) { factor = Math.min(unitSize.getWidth() / currentSize.getWidth(), unitSize.getHeight() / currentSize.getHeight()); offsetX = unitSize.getWidth() * (y % n % c) + (unitSize.getWidth() - currentSize.getWidth() * factor) / 2f; offsetY = newSize.getHeight() - unitSize.getHeight() * (y % n / c + 1) + (unitSize.getHeight() - currentSize.getHeight() * factor) / 2f; cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); } } } outputStream.flush(); document.close(); outputStream.close(); } catch (final FileNotFoundException e) { LOG.error("FileNotFoundException", e); } catch (final IOException e) { LOG.error("IOException", e); } catch (final DocumentException e) { LOG.error("DocumentException", e); } return ret; }
From source file:org.egov.ptis.actions.reports.SearchNoticesAction.java
License:Open Source License
/** * @param streamOfPDFFiles//from ww w . ja v a2s .c o m * @param outputStream * @return */ private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { if (LOGGER.isDebugEnabled()) LOGGER.debug("Entered into concatPDFs method"); Document document = null; try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<>(); final Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { final InputStream pdf = iteratorPDFs.next(); final PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } if (LOGGER.isDebugEnabled()) LOGGER.debug("Exit from concatPDFs method"); return outputStream.toByteArray(); }
From source file:org.egov.wtms.web.controller.reports.GenerateBillForConsumerCodeController.java
License:Open Source License
private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { Document document = null;/* w ww. j a va 2 s . c o m*/ try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<>(); final Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { final InputStream pdf = iteratorPDFs.next(); final PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } return outputStream.toByteArray(); }
From source file:org.egov.wtms.web.controller.reports.GenerateConnectionBillController.java
License:Open Source License
private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { Document document = null;/*from w w w . j a v a2 s . c om*/ try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<PdfReader>(); final Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { final InputStream pdf = iteratorPDFs.next(); final PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } return outputStream.toByteArray(); }
From source file:org.egov.wtms.web.controller.reports.SearchNoticeController.java
License:Open Source License
private byte[] concatPDFs(final List<InputStream> streamOfPDFFiles, final ByteArrayOutputStream outputStream) { Document document = null;/* w w w. ja v a2s . c o m*/ try { final List<InputStream> pdfs = streamOfPDFFiles; final List<PdfReader> readers = new ArrayList<>(); final Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { final InputStream pdf = iteratorPDFs.next(); final PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); if (null == document) document = new Document(pdfReader.getPageSize(1)); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { LOGGER.error("Exception in concat PDFs : ", e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (final IOException ioe) { LOGGER.error("Exception in concat PDFs : ", ioe); } } return outputStream != null ? outputStream.toByteArray() : null; }
From source file:org.ghost4j.document.PDFDocument.java
License:LGPL
public Document extract(int begin, int end) throws DocumentException { this.assertValidPageRange(begin, end); PDFDocument result = new PDFDocument(); ByteArrayInputStream bais = null; ByteArrayOutputStream baos = null; if (content != null) { com.lowagie.text.Document document = new com.lowagie.text.Document(); try {//from www. j a va2s .c om bais = new ByteArrayInputStream(content); baos = new ByteArrayOutputStream(); PdfReader inputPDF = new PdfReader(bais); // create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page; while (begin <= end) { document.newPage(); page = writer.getImportedPage(inputPDF, begin); cb.addTemplate(page, 0, 0); begin++; } document.close(); result.load(new ByteArrayInputStream(baos.toByteArray())); } catch (Exception e) { throw new DocumentException(e); } finally { if (document.isOpen()) document.close(); IOUtils.closeQuietly(bais); IOUtils.closeQuietly(baos); } } return result; }
From source file:org.jaffa.modules.printing.services.PdfHelper.java
License:Open Source License
/** * Scale the pages of the input pdfOutput document to the given pageSize. * @param pdfOutput The PDF document to rescale, in the form of a ByteArrayOutputStream. * @param pageSize The new page size to which to scale to PDF document, e.g. "A4". * @param noEnlarge If true, center pages instead of enlarging them. * Use noEnlarge if the new page size is larger than the old one * and the pages should be centered instead of enlarged. * @param preserveAspectRatio If true, the aspect ratio will be preserved. * @return The PDF document with its pages scaled to the input pageSize. *//*from ww w . j av a2s . c o m*/ public static byte[] scalePdfPages(byte[] pdfOutput, String pageSize, boolean noEnlarge, boolean preserveAspectRatio) throws FormPrintException { if (pageSize == null || pdfOutput == null) { return pdfOutput; } // Get the dimensions of the given pageSize in PostScript points. // A PostScript point is a 72th of an inch. float dimX; float dimY; Rectangle rectangle; try { rectangle = PageSize.getRectangle(pageSize); } catch (Exception ex) { FormPrintException e = new PdfProcessingException( "scalePdfPages - Invalid page size = " + pageSize + " "); log.error(" scalePdfPages - Invalid page size: " + pageSize + ". " + ex.getMessage() + ". "); throw e; } if (rectangle != null) { dimX = rectangle.getWidth(); dimY = rectangle.getHeight(); } else { FormPrintException e = new PdfProcessingException("scalePdfPages - Invalid page size: " + pageSize); log.error(" scalePdfPages - Invalid page size: " + pageSize); throw e; } //Create portrait and landscape rectangles for the given page size. Rectangle portraitPageSize; Rectangle landscapePageSize; if (dimY > dimX) { portraitPageSize = new Rectangle(dimX, dimY); landscapePageSize = new Rectangle(dimY, dimX); } else { portraitPageSize = new Rectangle(dimY, dimX); landscapePageSize = new Rectangle(dimX, dimY); } // Remove the document rotation before resizing the document. byte[] output = removeRotation(pdfOutput); PdfReader currentReader = null; try { currentReader = new PdfReader(output); } catch (IOException ex) { FormPrintException e = new PdfProcessingException("scalePdfPages - Failed to create a PDF Reader"); log.error(" scalePdfPages - Failed to create a PDF Reader "); throw e; } OutputStream baos = new ByteArrayOutputStream(); Rectangle newSize = new Rectangle(dimX, dimY); Document document = new Document(newSize, 0, 0, 0, 0); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, baos); } catch (DocumentException ex) { FormPrintException e = new PdfProcessingException("scalePdfPages - Failed to create a PDF Writer"); log.error(" scalePdfPages - Failed to create a PDF Writer "); throw e; } document.open(); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page; float offsetX, offsetY; for (int i = 1; i <= currentReader.getNumberOfPages(); i++) { Rectangle currentSize = currentReader.getPageSizeWithRotation(i); if (currentReader.getPageRotation(i) != 0) { FormPrintException e = new PdfProcessingException("Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form."); log.error(" Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form. "); throw e; } //Reset the page size for each page because there may be a mix of sizes in the document. float currentWidth = currentSize.getWidth(); float currentHeight = currentSize.getHeight(); if (currentWidth > currentHeight) { newSize = landscapePageSize; } else { newSize = portraitPageSize; } document.setPageSize(newSize); document.newPage(); float factorX = newSize.getWidth() / currentSize.getWidth(); float factorY = newSize.getHeight() / currentSize.getHeight(); // Use noEnlarge if the new page size is larger than the old one // and the pages should be centered instead of enlarged. if (noEnlarge) { if (factorX > 1) { factorX = 1; } if (factorY > 1) { factorY = 1; } } if (preserveAspectRatio) { factorX = Math.min(factorX, factorY); factorY = factorX; } offsetX = (newSize.getWidth() - (currentSize.getWidth() * factorX)) / 2f; offsetY = (newSize.getHeight() - (currentSize.getHeight() * factorY)) / 2f; page = writer.getImportedPage(currentReader, i); cb.addTemplate(page, factorX, 0, 0, factorY, offsetX, offsetY); } document.close(); return ((ByteArrayOutputStream) baos).toByteArray(); }
From source file:org.jaffa.modules.printing.services.PdfHelper.java
License:Open Source License
/** * Remove the rotation from the pdfOutput document pages. *//*www . ja va 2s . co m*/ private static byte[] removeRotation(byte[] pdfOutput) throws FormPrintException { PdfReader currentReader = null; try { currentReader = new PdfReader(pdfOutput); } catch (IOException ex) { FormPrintException e = new PdfProcessingException( "Remove PDF Page Rotation - Failed to create a PDF Reader"); log.error(" Remove PDF Page Rotation - Failed to create a PDF Reader "); throw e; } boolean needed = false; for (int i = 1; i <= currentReader.getNumberOfPages(); i++) { if (currentReader.getPageRotation(i) != 0) { needed = true; } } if (!needed) { return pdfOutput; } OutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, baos); } catch (DocumentException ex) { FormPrintException e = new PdfProcessingException( "Remove PDF Page Rotation - Failed to create a PDF Writer"); log.error(" Remove PDF Page Rotation - Failed to create a PDF Writer "); throw e; } PdfContentByte cb = null; PdfImportedPage page; for (int i = 1; i <= currentReader.getNumberOfPages(); i++) { Rectangle currentSize = currentReader.getPageSizeWithRotation(i); currentSize = new Rectangle(currentSize.getWidth(), currentSize.getHeight()); // strip rotation document.setPageSize(currentSize); if (cb == null) { document.open(); cb = writer.getDirectContent(); } else { document.newPage(); } int rotation = currentReader.getPageRotation(i); page = writer.getImportedPage(currentReader, i); float a, b, c, d, e, f; if (rotation == 0) { a = 1; b = 0; c = 0; d = 1; e = 0; f = 0; } else if (rotation == 90) { a = 0; b = -1; c = 1; d = 0; e = 0; f = currentSize.getHeight(); } else if (rotation == 180) { a = -1; b = 0; c = 0; d = -1; e = currentSize.getWidth(); f = currentSize.getHeight(); } else if (rotation == 270) { a = 0; b = 1; c = -1; d = 0; e = currentSize.getWidth(); f = 0; } else { FormPrintException ex = new PdfProcessingException( "Remove PDF Page Rotation - Unparsable rotation value: " + rotation); log.error(" Remove PDF Page Rotation - Unparsable form rotation value: " + rotation); throw ex; } cb.addTemplate(page, a, b, c, d, e, f); } document.close(); return ((ByteArrayOutputStream) baos).toByteArray(); }