List of usage examples for com.lowagie.text.pdf PdfReader getNumberOfPages
public int getNumberOfPages()
From source file:org.sejda.impl.itext.component.AbstractPdfCopier.java
License:Apache License
/** * Adds to the {@link PdfSmartCopy} all the pages from the input reader * /*from www.jav a 2 s . c om*/ * @param reader * @throws TaskException */ public void addAllPages(PdfReader reader) throws TaskException { for (int i = 1; i <= reader.getNumberOfPages(); i++) { addPage(reader, i); } }
From source file:org.sejda.impl.itext.component.FormFieldsAwarePdfCopier.java
License:Apache License
public void addAllPages(PdfReader reader) throws TaskException { try {/*from w ww . j a v a 2 s . com*/ pdfCopy.addDocument(reader); numberOfCopiedPages += reader.getNumberOfPages(); } catch (DocumentException e) { throw new TaskException("An error occurred adding all pages.", e); } catch (IOException e) { throw new TaskIOException("An IO error occurred adding all pages.", e); } }
From source file:org.sejda.impl.itext.component.ITextOutlineSubsetProvider.java
License:Apache License
public ITextOutlineSubsetProvider(PdfReader reader) { if (reader == null) { throw new IllegalArgumentException("Unable to retrieve bookmarks from a null reader."); }//from w ww . j a v a 2s. c o m this.totalNumberOfPages = reader.getNumberOfPages(); this.bookmarks = getBookmarksOrEmpty(reader); }
From source file:org.sejda.impl.itext.component.PdfUnpacker.java
License:Apache License
private Set<PdfDictionary> getFileAttachmentsDictionaries(PdfReader reader) { Set<PdfDictionary> retSet = new NullSafeSet<PdfDictionary>(); for (int k = 1; k <= reader.getNumberOfPages(); ++k) { PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS)); if (annots != null) { for (@SuppressWarnings("unchecked") Iterator<PdfObject> iter = annots.listIterator(); iter.hasNext();) { PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next()); PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE)); if (PdfName.FILEATTACHMENT.equals(subType)) { retSet.add((PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS))); }//w w w . j a v a2s .co m } } } return retSet; }
From source file:org.sejda.impl.itext.component.split.AbstractPdfSplitter.java
License:Apache License
/** * Creates a new splitter using the given reader. * /* w w w.java 2 s . c om*/ * @param reader */ AbstractPdfSplitter(PdfReader reader, T parameters) { this.reader = reader; this.parameters = parameters; this.totalPages = reader.getNumberOfPages(); this.outlineSubsetProvider = new ITextOutlineSubsetProvider(reader); this.outputWriter = OutputWriters.newMultipleOutputWriter(parameters.isOverwrite()); }
From source file:org.signserver.module.pdfsigner.PDFSigner.java
License:Open Source License
/** * get the page number at which to draw signature rectangle * /* www .j a v a 2s . co m*/ * @param pReader * @param pParams * @return */ private int getPageNumberForSignature(PdfReader pReader, PDFSignerParameters pParams) { int totalNumOfPages = pReader.getNumberOfPages(); if (pParams.getVisible_sig_page().trim().equals("First")) { return 1; } else if (pParams.getVisible_sig_page().trim().equals("Last")) { return totalNumOfPages; } else { try { int pNum = Integer.parseInt(pParams.getVisible_sig_page()); if (pNum < 1) { return 1; } else if (pNum > totalNumOfPages) { return totalNumOfPages; } else { return pNum; } } catch (NumberFormatException ex) { // not a numeric argument draw on first line return 1; } } }
From source file:org.silverpeas.core.importexport.control.ImportExport.java
License:Open Source License
/** * @param userDetail//from w w w . j a v a2 s.co m * @param itemsToExport * @return * @throws ImportExportException */ public ExportPDFReport processExportPDF(UserDetail userDetail, List<WAAttributeValuePair> itemsToExport, NodePK rootPK) throws ImportExportException { ExportPDFReport report = new ExportPDFReport(); report.setDateDebut(new Date()); PublicationsTypeManager pubTypeManager = getPublicationsTypeManager(); String fileExportName = generateExportDirName(userDetail, "fusion"); String tempDir = FileRepositoryManager.getTemporaryPath(); File fileExportDir = new File(tempDir + fileExportName); if (!fileExportDir.exists()) { try { FileFolderManager.createFolder(fileExportDir); } catch (org.silverpeas.core.util.UtilException ex) { throw new ImportExportException("ImportExport", "importExport.EX_CANT_CREATE_FOLDER", ex); } } File pdfFileName = new File(tempDir + fileExportName + ".pdf"); try { // cration des rpertoires avec le nom des thmes et des publications List<AttachmentDetail> pdfList = pubTypeManager.processPDFExport(report, userDetail, itemsToExport, fileExportDir.getPath(), true, rootPK); try { int pageOffset = 0; List master = new ArrayList(); Document document = null; PdfCopy writer = null; if (!pdfList.isEmpty()) { boolean firstPage = true; for (AttachmentDetail attDetail : pdfList) { PdfReader reader = null; try { reader = new PdfReader( fileExportDir.getPath() + File.separatorChar + attDetail.getLogicalName()); } catch (IOException ioe) { // Attached file is not physically present on disk, ignore it and log event SilverLogger.getLogger(this).error("Cannot find PDF {0}", new String[] { attDetail.getLogicalName() }, ioe); } if (reader != null) { reader.consolidateNamedDestinations(); int nbPages = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += nbPages; if (firstPage) { document = new Document(reader.getPageSizeWithRotation(1)); writer = new PdfCopy(document, new FileOutputStream(pdfFileName)); document.open(); firstPage = false; } for (int i = 1; i <= nbPages; i++) { try { PdfImportedPage page = writer.getImportedPage(reader, i); writer.addPage(page); } catch (Exception e) { // Can't import PDF file, ignore it and log event SilverLogger.getLogger(this).error("Cannot merge PDF {0}", new String[] { attDetail.getLogicalName() }, e); } } PRAcroForm form = reader.getAcroForm(); if (form != null) { writer.copyAcroForm(reader); } } } if (!master.isEmpty()) { writer.setOutlines(master); } writer.flush(); document.close(); } else { return null; } } catch (DocumentException e) { // Impossible de copier le document throw new ImportExportException("ImportExport", "root.EX_CANT_WRITE_FILE", e); } } catch (IOException e) { // Pb avec le rpertoire de destination throw new ImportExportException("ImportExport", "root.EX_CANT_WRITE_FILE", e); } report.setPdfFileName(pdfFileName.getName()); report.setPdfFileSize(pdfFileName.length()); report.setPdfFilePath(FileServerUtils.getUrlToTempDir(pdfFileName.getName())); report.setDateFin(new Date()); return report; }
From source file:org.silverpeas.core.util.PdfUtil.java
License:Open Source License
/** * Gets some document info from a PDF file. * @param pdfSource the source pdf file, this content is not modified by this method * @return a {@link DocumentInfo} instance. *//*w w w . j av a2 s . c o m*/ public static DocumentInfo getDocumentInfo(File pdfSource) { if (pdfSource == null || !pdfSource.isFile()) { throw new SilverpeasRuntimeException(PDF_FILE_ERROR_MSG); } else if (!FileUtil.isPdf(pdfSource.getPath())) { throw new SilverpeasRuntimeException(NOT_PDF_FILE_ERROR_MSG); } PdfReader reader = null; try (final InputStream pdfSourceIS = FileUtils.openInputStream(pdfSource)) { reader = new PdfReader(pdfSourceIS); final DocumentInfo documentInfo = new DocumentInfo(); documentInfo.setNbPages(reader.getNumberOfPages()); for (int i = 1; i <= documentInfo.getNbPages(); i++) { final Rectangle rectangle = reader.getPageSize(i); final int maxWidth = Math.round(rectangle.getWidth()); final int maxHeight = Math.round(rectangle.getHeight()); if (maxWidth > documentInfo.getMaxWidth()) { documentInfo.setMaxWidth(maxWidth); } if (maxHeight > documentInfo.getMaxHeight()) { documentInfo.setMaxHeight(maxHeight); } } return documentInfo; } catch (Exception e) { SilverLogger.getLogger(PdfUtil.class).error(e); throw new SilverpeasRuntimeException("A problem has occurred during the reading of a pdf file", e); } finally { if (reader != null) { reader.close(); } } }
From source file:org.silverpeas.core.util.PdfUtil.java
License:Open Source License
/** * Add a image under or over content on each page of a PDF file. * @param pdfSource the source pdf file, this content is not modified by this method * @param imageToAdd the image file// ww w. j av a2 s.c o m * @param pdfDestination the destination pdf file, with the image under or over content * @param isBackground indicates if image is addes under or over the content of the pdf source * file */ private static void addImageOnEachPage(InputStream pdfSource, File imageToAdd, OutputStream pdfDestination, final boolean isBackground) { // Verify given arguments if (imageToAdd == null || !imageToAdd.isFile()) { throw new SilverpeasRuntimeException("The image file doesn't exist"); } else if (!FileUtil.isImage(imageToAdd.getPath())) { throw new SilverpeasRuntimeException("The picture to add is not an image file"); } PdfReader reader = null; try { // Get a reader of PDF content reader = new PdfReader(pdfSource); // Obtain the total number of pages int pdfNbPages = reader.getNumberOfPages(); PdfStamper stamper = new PdfStamper(reader, pdfDestination); // Load the image Image image = Image.getInstance(imageToAdd.getPath()); float imageWidth = image.getWidth(); float imageHeigth = image.getHeight(); // Adding the image on each page of the PDF for (int i = 1; i <= pdfNbPages; i++) { // Page sizes Rectangle rectangle = reader.getPageSize(i); // Compute the scale of the image float scale = Math.min(100, (rectangle.getWidth() / imageWidth * 100)); image.scalePercent(Math.min(scale, (rectangle.getHeight() / imageHeigth * 100))); // Setting the image position for the current page image.setAbsolutePosition(computeImageCenterPosition(rectangle.getWidth(), image.getScaledWidth()), computeImageCenterPosition(rectangle.getHeight(), image.getScaledHeight())); // Adding image PdfContentByte imageContainer = isBackground ? stamper.getUnderContent(i) : stamper.getOverContent(i); imageContainer.addImage(image); } // End of the treatment : closing the stamper stamper.close(); } catch (Exception e) { SilverLogger.getLogger(PdfUtil.class).error(e); throw new SilverpeasRuntimeException( "A problem has occurred during the adding of an image into a pdf file", e); } finally { if (reader != null) { reader.close(); } } }
From source file:org.silverpeas.util.PdfUtil.java
License:Open Source License
/** * Add a image under or over content on each page of a PDF file. * @param pdfSource the source pdf file, this content is not modified by this method * @param imageToAdd the image file/*from w ww. j a va2 s .co m*/ * @param pdfDestination the destination pdf file, with the image under or over content * @param isBackground indicates if image is addes under or over the content of the pdf source * file */ private static void addImageOnEachPage(InputStream pdfSource, File imageToAdd, OutputStream pdfDestination, final boolean isBackground) { // Verify given arguments if (imageToAdd == null || !imageToAdd.isFile()) { throw new RuntimeException("The image file doesn't exist"); } else if (!FileUtil.isImage(imageToAdd.getPath())) { throw new RuntimeException("The picture to add is not an image file"); } PdfReader reader = null; try { // Get a reader of PDF content reader = new PdfReader(pdfSource); // Obtain the total number of pages int pdfNbPages = reader.getNumberOfPages(); PdfStamper stamper = new PdfStamper(reader, pdfDestination); // Load the image Image image = Image.getInstance(imageToAdd.getPath()); float imageWidth = image.getWidth(); float imageHeigth = image.getHeight(); // Adding the image on each page of the PDF for (int i = 1; i <= pdfNbPages; i++) { // Page sizes Rectangle rectangle = reader.getPageSize(i); // Compute the scale of the image float scale = Math.min(100, (rectangle.getWidth() / imageWidth * 100)); image.scalePercent(Math.min(scale, (rectangle.getHeight() / imageHeigth * 100))); // Setting the image position for the current page image.setAbsolutePosition(computeImageCenterPosition(rectangle.getWidth(), image.getScaledWidth()), computeImageCenterPosition(rectangle.getHeight(), image.getScaledHeight())); // Adding image PdfContentByte imageContainer = isBackground ? stamper.getUnderContent(i) : stamper.getOverContent(i); imageContainer.addImage(image); } // End of the treatment : closing the stamper stamper.close(); } catch (Exception e) { SilverTrace.error("util", "PdfUtil.stamp", "EX_ERROR_PDF_ADD_WATERWARK", e); throw new RuntimeException("A problem has occured during the adding of an image into a pdf file", e); } finally { if (reader != null) { reader.close(); } } }