List of usage examples for com.lowagie.text Document newPage
public boolean newPage()
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * N up./*from ww w .j a v a 2 s .c om*/ * * @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 w ww . j a v a2 s. 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; try {//from ww w . j a v a 2 s. c o m 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; try {/*ww w . jav a2 s . c om*/ 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; try {/*from w ww . ja v a2 s . com*/ 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.esa.nest.dat.reports.PDFFormat.java
License:Open Source License
private static void addTitlePage(Document document) throws DocumentException { Paragraph preface = new Paragraph(); // We add one empty line addEmptyLine(preface, 1);//from w w w. ja va 2 s. co m // Lets write a big header preface.add(new Paragraph("Title of the document", catFont)); addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ smallBold)); addEmptyLine(preface, 3); preface.add(new Paragraph("This document describes something which is very important ", smallBold)); addEmptyLine(preface, 8); preface.add(new Paragraph( "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).", redFont)); document.add(preface); // Start a new page document.newPage(); }
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 w ww. j a v a 2 s . c o m*/ 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.inbio.modeling.core.manager.impl.ExportManagerImpl.java
License:Open Source License
@Override public Document exportPDF(Document document, Double resolution, String imageName, String limitLayerName, List<GrassLayerDTO> layerList, long currentSessionId, Locale locale) throws Exception { // set the current locale globally this.currentLocale = locale; // Add metadata this.addMetadata(document); // Modeling threats report (Main title) this.addTitleOne(document, this.getI18nString("pdfReport.threatModel")); // Generated escenary (Sub title) this.addTitleTwo(document, this.getI18nString("pdfReport.proposedScenary"), true); this.addNewLine(document, 20); // Map and scale image this.addImageSection(document, imageName, currentSessionId); // General information of the scenario this.addTitleTwo(document, this.getI18nString("pdfReport.generalInfo")); // resolution information document.add(new Paragraph(this.getI18nString("pdfReport.resolution") + ": " + resolution + " " + this.getI18nString("pdfReport.meters"))); this.addNewLine(document, 1); // Limit layer document.add(new Paragraph(this.getI18nString("pdfReport.limitLayer") + " : " + limitLayerName)); this.addNewLine(document, 1); document.newPage(); // Layer sub-section this.addTitleTwo(document, this.getI18nString("pdfReport.layers")); Table table;// www . jav a2s .c o m LayerDTO layer = null; List<CategoryDTO> categorys; for (GrassLayerDTO grassLayerDTO : layerList) { // layer name (title) this.addTitleThree(document, grassLayerDTO.getDisplayName()); // Get the layer information layer = layerManager.getLayerByName(grassLayerDTO.getName()); // create the table with the metadata information. table = this.create2columnTable(); this.addTableHeader(table, this.getI18nString("pdfReport.metadata")); this.addTableRow(table, this.getI18nString("pdfReport.source"), layer.getSource()); this.addTableRow(table, this.getI18nString("pdfReport.year"), layer.getYear()); this.addTableRow(table, this.getI18nString("pdfReport.visualizationScale"), layer.getVizScale()); this.addTableRow(table, this.getI18nString("pdfReport.dataScale"), layer.getDataScale()); this.addTableRow(table, this.getI18nString("pdfReport.generationProcedure"), layer.getGenerationProcedure()); this.addTableRow(table, this.getI18nString("pdfReport.description"), layer.getDescription()); this.addTableRow(table, this.getI18nString("pdfReport.dataType"), grassLayerDTO.getType().toString()); document.add(table); // Create the table with the infomation about the reclasification or parameters used // in the scenario generation process. table = this.create2columnTable(); this.addTableHeader(table, this.getI18nString("pdfReport.layerDataConf")); // Print the intervals/category/Radio information categorys = grassLayerDTO.getCategories(); if (grassLayerDTO.getType().equals(LayerType.AREA)) { for (CategoryDTO categoryDTO : categorys) { if (categoryDTO != null) { table.addCell(categoryDTO.getValue()); table.addCell(categoryDTO.getDescription()); } } } else if (grassLayerDTO.getType().equals(LayerType.LINE)) { String minimal = "0"; int counter = 2; table.addCell("1"); table.addCell(this.getI18nString("pdfReport.lineMarker")); for (CategoryDTO categoryDTO : categorys) { table.addCell(counter++ + " "); table.addCell( minimal + " - " + categoryDTO.getValue() + this.getI18nString("pdfReport.meters")); minimal = categoryDTO.getValue(); } } else { table.addCell(this.getI18nString("pdfReport.radio")); table.addCell(categorys.get(0).getValue() + this.getI18nString("pdfReport.meters")); } document.add(table); } String footerText[] = { this.getI18nString("pdfReport.footer1"), this.getI18nString("pdfReport.footer2"), this.getI18nString("pdfReport.footer3") }; this.addDocumentFooter(document, footerText); return document; }
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. */// w ww .j av a 2s . co 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. *///from w ww . j a v a2s .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(); }