/* (non-Javadoc) * @see de.offis.health.icardea.cied.pdf.interfaces.PDFExtractor#getPDFPages(int, int) */ public byte[] getPDFPages(int fromPageNumber, int toPageNumber) { ByteArrayOutputStream byteArrayOutputStream = null; boolean extractionSuccessful = false; if (pdfReader != null) { int numberOfPages = getNumberOfPages(); /* * Check if the given page numbers are in the allowed range. */ if (fromPageNumber > 0 && fromPageNumber <= numberOfPages && toPageNumber > 0 && toPageNumber <= numberOfPages) { /* * Now check if the given fromPageNumber is smaller * as the given toPageNumber. If not swap the numbers. */ if (fromPageNumber > toPageNumber) { int tmpPageNumber = toPageNumber; toPageNumber = fromPageNumber; fromPageNumber = tmpPageNumber; } Document newDocument = new Document(); try { byteArrayOutputStream = new ByteArrayOutputStream(); PdfSmartCopy pdfCopy = new PdfSmartCopy(newDocument, byteArrayOutputStream); newDocument.open(); for (int currentPage = fromPageNumber; currentPage <= toPageNumber; currentPage++) { pdfCopy.addPage(pdfCopy.getImportedPage(pdfReader, currentPage)); } // end for pdfCopy.flush(); pdfCopy.close(); newDocument.close(); extractionSuccessful = true; } catch (DocumentException ex) { // TODO: Create an own exception for PDF processing errors. logger.error( "An exception occurred while extracting " + "pages from the input PDF file.", ex); } catch (IOException ex) { // TODO: Create an own exception for PDF processing errors. logger.error( "An exception occurred while extracting " + "pages from the input PDF file.", ex); } finally { if (!extractionSuccessful) { byteArrayOutputStream = null; } } // end try..catch..finally } // end if checking range of given pages } // end if (pdfReader != null) if (byteArrayOutputStream != null) { return byteArrayOutputStream.toByteArray(); } return null; }
public void setCompression(boolean compress) { if (compress) { pdfCopy.setFullCompression(); pdfCopy.setCompressionLevel(PdfStream.BEST_COMPRESSION); } }
public void addPage(PdfReader reader, int pageNumber) throws TaskException { try { pdfCopy.addPage(pdfCopy.getImportedPage(reader, pageNumber)); numberOfCopiedPages++; } catch (BadPdfFormatException e) { throw new TaskException( String.format("An error occurred adding page %d to the PdfSmartCopy.", pageNumber), e); } catch (IOException e) { throw new TaskIOException( String.format("An IO error occurred adding page %d to the PdfSmartCopy.", pageNumber), e); } }
/** * Opens the copier using the given reader and the given output version. * * @param reader * @param outputStream the output stream to write to. * @param version version for the created pdf copy, if null the version number is taken from the * input {@link PdfReader} */ void open(PdfReader reader, OutputStream outputStream, PdfVersion version) throws TaskException { try { pdfDocument = new Document(reader.getPageSizeWithRotation(1)); pdfCopy = new PdfSmartCopy(pdfDocument, outputStream); if (version == null) { pdfCopy.setPdfVersion(reader.getPdfVersion()); } else { pdfCopy.setPdfVersion(version.getVersionAsCharacter()); } pdfDocument.addCreator(Sejda.CREATOR); pdfDocument.open(); } catch (DocumentException e) { throw new TaskException("An error occurred opening the PdfSmartCopy.", e); } }
public void freeReader(PdfReader reader) throws TaskIOException { try { pdfCopy.freeReader(reader); } catch (IOException e) { throw new TaskIOException("An IO error occurred freeing the pdf reader.", e); } }
public void close() { if (pdfDocument != null) { pdfDocument.close(); } if (pdfCopy != null) { pdfCopy.close(); } closed = true; }
public void addBlankPage(PdfReader reader) { pdfCopy.addPage(reader.getPageSize(1), reader.getPageRotation(1)); numberOfCopiedPages++; }
public void setOutline(List<Map<String, Object>> outline) { if (outline != null && outline.size() > 0) { pdfCopy.setOutlines(outline); } }
public void setPageLabels(PdfPageLabels labels) { pdfCopy.setPageLabels(labels); }