List of usage examples for com.lowagie.text.pdf PdfReader PdfReader
public PdfReader(PdfReader reader)
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * Resize.// w ww . ja va 2 s.c om * * @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./*from www .j a va 2 s.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/* w w w.ja va 2 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;/*from w ww .j a v a 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 .ja va 2s. c o m*/ 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;// ww w.j ava 2s. co 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.fenixedu.academic.domain.candidacy.util.GenericApplicationUploadBean.java
License:Open Source License
protected byte[] readStreamContents() throws IOException { try (final InputStream stream = this.getStream()) { if (stream == null || getFileSize() == 0) { return null; }//from w w w. j a v a 2 s . c o m if (getFileSize() > MAX_FILE_SIZE) { throw new DomainException("error.file.to.big"); } byte[] contents = ByteStreams.toByteArray(stream); // Ensure the submitted file is a PDF new PdfReader(contents); return contents; } }
From source file:org.fenixedu.academic.servlet.ProcessCandidacyPrintAllDocumentsFilter.java
License:Open Source License
private ByteArrayOutputStream concatenateDocs(byte[] originalDoc, Person person) throws IOException, DocumentException { ByteArrayOutputStream concatenatedPdf = new ByteArrayOutputStream(); PdfCopyFields copy = new PdfCopyFields(concatenatedPdf); //if no documents are added there is nothing to close boolean isToClose = false; if (!FirstTimeDocumentsConfiguration.getInstance().isToExclude("adminProcessSheet")) { try {/*from w ww. ja va2 s. c o m*/ copy.addDocument(new PdfReader(createAcademicAdminProcessSheet(person))); isToClose = true; } catch (Exception e) { logger.error(e.getMessage(), e); } } if (originalDoc.length > 0) { copy.addDocument(new PdfReader(originalDoc)); isToClose = true; } for (PdfFiller pdfFiller : pdfFillersSet) { if (!isPdfFillerToExclude(pdfFiller.getClass().getName())) { copy.addDocument(new PdfReader(pdfFiller.getFilledPdf(person).toByteArray())); isToClose = true; } } if (isToClose) { copy.close(); } return concatenatedPdf; }
From source file:org.fenixedu.idcards.ui.candidacydocfiller.BPIPdfFiller.java
License:Open Source License
@Override public ByteArrayOutputStream getFilledPdf(Person person) throws IOException, DocumentException { ByteArrayOutputStream concatenatedBPIPdf = new ByteArrayOutputStream(); PdfCopyFields copy = new PdfCopyFields(concatenatedBPIPdf); copy.addDocument(new PdfReader(getFilledPdfBPICardAEIST(person).toByteArray())); copy.addDocument(new PdfReader(getFilledPdfBPIDigitalDoc(person).toByteArray())); copy.addDocument(new PdfReader(getFilledPdfBPIPersonalInformation(person).toByteArray())); copy.addDocument(new PdfReader(getFilledPdfBPIProductsandServices(person).toByteArray())); copy.close();/*from www. jav a 2s .c o m*/ return concatenatedBPIPdf; }
From source file:org.fenixedu.idcards.ui.candidacydocfiller.BPIPdfFiller.java
License:Open Source License
private ByteArrayOutputStream getFilledPdfBPICardAEIST(Person person) throws IOException, DocumentException { InputStream istream = getClass().getResourceAsStream(BPI_AEIST_CARD_PDF_PATH); PdfReader reader = new PdfReader(istream); reader.getAcroForm().remove(PdfName.SIGFLAGS); ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, output); form = stamper.getAcroFields();//www . j a v a 2 s.c o m setField("BI/CC", person.getDocumentIdNumber()); setField("Nome", person.getName()); setField("topmostSubform[0].Page1[0].Datavalidade[0]", person.getExpirationDateOfDocumentIdYearMonthDay() .toString(DateTimeFormat.forPattern("dd/MM/yyyy"))); setField("dia", String.format("%02d", person.getExpirationDateOfDocumentIdYearMonthDay().getDayOfMonth())); setField("Ms", String.format("%02d", person.getExpirationDateOfDocumentIdYearMonthDay().getMonthOfYear())); setField("Ano", String.valueOf(person.getExpirationDateOfDocumentIdYearMonthDay().getYear())); stamper.setFormFlattening(true); stamper.close(); return output; }