List of usage examples for com.lowagie.text.pdf PdfReader getPageSize
public Rectangle getPageSize(PdfDictionary page)
From source file:net.sf.jsignpdf.preview.Pdf2Image.java
License:Mozilla Public License
/** * Returns image (or null if failed) generated from given page in PDF using * JPedal LGPL./*from w w w . j a v a2 s . co m*/ * * @param aPage * page in PDF (1 based) * @return image or null */ public BufferedImage getImageUsingJPedal(final int aPage) { BufferedImage tmpResult = null; PdfReader reader = null; PdfDecoder pdfDecoder = null; try { reader = PdfUtils.getPdfReader(options.getInFile(), options.getPdfOwnerPwdStrX().getBytes()); if (JPEDAL_MAX_IMAGE_RENDER_SIZE > reader.getPageSize(aPage).getWidth() * reader.getPageSize(aPage).getHeight()) { pdfDecoder = new PdfDecoder(); try { pdfDecoder.openPdfFile(options.getInFile(), options.getPdfOwnerPwdStrX()); } catch (PdfException e) { try { // try to read PDF with empty password pdfDecoder.openPdfFile(options.getInFile(), ""); } catch (PdfException e1) { // try to read PDF without password pdfDecoder.openPdfFile(options.getInFile()); } } tmpResult = pdfDecoder.getPageAsImage(aPage); } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } if (pdfDecoder != null) { pdfDecoder.closePdfFile(); } } return tmpResult; }
From source file:net.sf.sze.service.impl.converter.PdfConverterImpl.java
License:GNU General Public License
/** * Orders the pages from an DIN-A4-document on a DIN-A3-document. * @param reader reader for DIN-A4-document * @param pdfFileA3 file in which the A3-document will be saved. * @param pages page-numbers in the order they will placed the paper in the * order left, right-side must be a multiple of 4. 0 is interpreted as an * empty-page./*w w w. j a v a 2s .c o m*/ * @throws DocumentException problems in iText. * @throws IOException io-problems. */ private void createA3Subdocument(PdfReader reader, File pdfFileA3, int... pages) throws DocumentException, IOException { if (pages.length % 4 != 0) { throw new IllegalArgumentException("The number of pages must be a " + "multiple of 4."); } // we retrieve the size of the first page final Rectangle psize = reader.getPageSize(1); final float width = psize.getWidth(); final float leftMargin = 0f; final float topMargin = 0f; // step 1: creation of a document-object final Document document = new Document(PageSize.A3.rotate()); // step 2: we create a writer that listens to the document final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFileA3)); writer.setPDFXConformance(PdfWriter.PDFA1B); // step 3: we open the document document.open(); addPdfAInfosToDictonary(writer); // step 4: we add content final PdfContentByte cb = writer.getDirectContent(); final PdfTemplate[] pdfPages = new PdfTemplate[pages.length]; for (int i = 0; i < pdfPages.length; i++) { final int pageNr = pages[i]; final PdfTemplate page; if (pageNr == EMPTY_PAGE) { page = writer.getImportedPage(getEmptyPDFPage(psize), 1); } else { page = writer.getImportedPage(reader, pageNr); } if (i % 2 == 0) { document.newPage(); cb.addTemplate(page, 1f, 0f, 0f, 1f, leftMargin, topMargin); } else { cb.addTemplate(page, 1f, 0f, 0f, 1f, width + leftMargin, topMargin); } } writer.createXmpMetadata(); // step 5: we close the document document.close(); }
From source file:net.sf.sze.service.impl.converter.PdfConverterImpl.java
License:GNU General Public License
/** * Create from a DIN-A4-document a DIN-A4 subdocument. * @param reader reader for DIN-A4-document * @param pdfFileA4 file in which the new A4-document will be saved. * @param pages page-numbers in the order they will placed the paper. * @throws IOException//from w w w . j a v a 2 s . c om * @throws DocumentException */ private void createA4Subdocument(PdfReader reader, File pdfFileA4, int... pages) throws IOException, DocumentException { // step 1: creation of a document-object Document document = new Document(reader.getPageSize(1)); // step 2: we create a writer that listens to the document PdfCopy copy = new PdfCopy(document, new FileOutputStream(pdfFileA4)); copy.setPDFXConformance(PdfWriter.PDFA1B); // step 3: we open the document document.open(); addPdfAInfosToDictonary(copy); final Rectangle psize = reader.getPageSize(1); for (int pageNr : pages) { if (pageNr == EMPTY_PAGE) { copy.addPage(copy.getImportedPage(getEmptyPDFPage(psize), 1)); } else { copy.addPage(copy.getImportedPage(reader, pageNr)); } } copy.createXmpMetadata(); // step 5: we close the document document.close(); }
From source file:org.efaps.esjp.common.file.FileUtil_Base.java
License:Apache License
/** * N up.//from www. j a va2s .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 w w w . j a v a2s. c om*/ * @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 w w . j av a 2 s . com 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;/*w ww . ja v a2 s. co 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;// w ww . j ava 2s.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.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void setCrop(int pageCount, PdfPageData currentPageData, CropPDFPages cropPage) { File tempFile = null;/*from w w w. j a v a2 s . c o m*/ try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = cropPage.getPages(); if (pgsToEdit == null) return; List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); boolean applyToCurrent = cropPage.applyToCurrentCrop(); for (int page = 1; page <= pageCount; page++) { if (pagesToEdit.contains(new Integer(page))) { float currentLeftCrop = currentPageData.getCropBoxX(page); float currentBottomCrop = currentPageData.getCropBoxY(page); float currentRightCrop = currentPageData.getCropBoxWidth(page) + currentLeftCrop; float currentTopCrop = currentPageData.getCropBoxHeight(page) + currentBottomCrop; float[] newCrop = cropPage.getCrop(); if (applyToCurrent) { newCrop[0] = currentLeftCrop + newCrop[0]; newCrop[1] = currentBottomCrop + newCrop[1]; newCrop[2] = currentRightCrop - newCrop[2]; newCrop[3] = currentTopCrop - newCrop[3]; } else { newCrop[2] = reader.getPageSize(page).width() - newCrop[2]; newCrop[3] = reader.getPageSize(page).height() - newCrop[3]; } reader.getPageN(page).put(PdfName.CROPBOX, new PdfArray(newCrop)); } } PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void stampImage(int pageCount, PdfPageData currentPageData, final StampImageToPDFPages stampImage) { File tempFile = null;// w w w.ja va2 s . com try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = stampImage.getPages(); if (pgsToEdit == null) return; File fileToTest = new File(stampImage.getImageLocation()); if (!fileToTest.exists()) { currentGUI.showMessageDialog(Messages.getMessage("PdfViewerError.ImageDoesNotExist")); return; } List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); final PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); int n = reader.getNumberOfPages(); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); Image img = Image.getInstance(fileToTest.getAbsolutePath()); int chosenWidthScale = stampImage.getWidthScale(); int chosenHeightScale = stampImage.getHeightScale(); img.scalePercent(chosenWidthScale, chosenHeightScale); String chosenPlacement = stampImage.getPlacement(); int chosenRotation = stampImage.getRotation(); img.setRotationDegrees(chosenRotation); String chosenHorizontalPosition = stampImage.getHorizontalPosition(); String chosenVerticalPosition = stampImage.getVerticalPosition(); float chosenHorizontalOffset = stampImage.getHorizontalOffset(); float chosenVerticalOffset = stampImage.getVerticalOffset(); for (int page = 0; page <= n; page++) { if (pagesToEdit.contains(new Integer(page))) { PdfContentByte cb; if (chosenPlacement.equals("Overlay")) cb = stamp.getOverContent(page); else cb = stamp.getUnderContent(page); int currentRotation = currentPageData.getRotation(page); Rectangle pageSize; if (currentRotation == 90 || currentRotation == 270) pageSize = reader.getPageSize(page).rotate(); else pageSize = reader.getPageSize(page); float startx, starty; if (chosenVerticalPosition.equals("From the top")) { starty = pageSize.height() - ((img.height() * (chosenHeightScale / 100)) / 2); } else if (chosenVerticalPosition.equals("Centered")) { starty = (pageSize.height() / 2) - ((img.height() * (chosenHeightScale / 100)) / 2); } else { starty = 0; } if (chosenHorizontalPosition.equals("From the left")) { startx = 0; } else if (chosenHorizontalPosition.equals("Centered")) { startx = (pageSize.width() / 2) - ((img.width() * (chosenWidthScale / 100)) / 2); } else { startx = pageSize.width() - ((img.width() * (chosenWidthScale / 100)) / 2); } img.setAbsolutePosition(startx + chosenHorizontalOffset, starty + chosenVerticalOffset); cb.addImage(img); } } stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }