@Test public void generateMultiPageTiffFromPdf() throws PdfServiceException { assertNotNull(pdfService); FileSystemResource multipagePdf = new FileSystemResource(this.getClass().getResource("/pdfs/multipage_document.pdf").getFile()); assertTrue(multipagePdf.exists()); log.debug("file length is {}", tiffOutputFile.length()); pdfService.generateTiffFromPdf(multipagePdf.getFile(), tiffOutputFile); }
@Override public void generateTiffFromPdf(File inputPdf, File outputTiff) throws PdfServiceException { if (!inputPdf.exists()) { throw new PdfServiceException(inputPdf.getAbsolutePath() + " doesn't exists"); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff); PDDocument document = PDDocument.load(inputPdf)) { log.debug("Reading pdf from file path {}", outputTiff.getPath()); PDFRenderer pdfRenderer = new PDFRenderer(document); TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi(); ImageWriter writer = tiffspi.createWriterInstance(); log.debug("Preparing to generate multi image tiff."); writer.setOutput(ios); writer.prepareWriteSequence(null); log.debug("Pdf contains {} pages.", document.getNumberOfPages()); int page = 0; TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault()); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType("CCITT T.6"); writeParam.setCompressionQuality(0.75f); for (PDPage pdPage : document.getPages()) { log.debug("Rendering PDF page [{}/{}] as TIFF image", page, document.getNumberOfPages()); BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 200, ImageType.BINARY); IIOImage image = new IIOImage(bim, null, null); try { writer.writeToSequence(image, writeParam); } catch (UnsupportedOperationException e) { log.warn("Not supported compression:", e); writer.writeToSequence(image, null); } log.debug("Successfully written one image to the sequence, {} more to go.", document.getNumberOfPages() - page); page++; } ios.flush(); } catch (IOException e) { throw new PdfServiceException(e); } log.debug("Successfully written tiff sequence into file {}. With length: {} MBytes", outputTiff.getPath(), outputTiff.length() / MEGABYTE); }
PdfServiceImpl implements PdfService { @Override public void generateTiffFromPdf(File inputPdf, File outputTiff) throws PdfServiceException { if (!inputPdf.exists()) { throw new PdfServiceException(inputPdf.getAbsolutePath() + " doesn't exists"); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff); PDDocument document = PDDocument.load(inputPdf)) { log.debug("Reading pdf from file path {}", outputTiff.getPath()); PDFRenderer pdfRenderer = new PDFRenderer(document); TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi(); ImageWriter writer = tiffspi.createWriterInstance(); log.debug("Preparing to generate multi image tiff."); writer.setOutput(ios); writer.prepareWriteSequence(null); log.debug("Pdf contains {} pages.", document.getNumberOfPages()); int page = 0; TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault()); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType("CCITT T.6"); writeParam.setCompressionQuality(0.75f); for (PDPage pdPage : document.getPages()) { log.debug("Rendering PDF page [{}/{}] as TIFF image", page, document.getNumberOfPages()); BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 200, ImageType.BINARY); IIOImage image = new IIOImage(bim, null, null); try { writer.writeToSequence(image, writeParam); } catch (UnsupportedOperationException e) { log.warn("Not supported compression:", e); writer.writeToSequence(image, null); } log.debug("Successfully written one image to the sequence, {} more to go.", document.getNumberOfPages() - page); page++; } ios.flush(); } catch (IOException e) { throw new PdfServiceException(e); } log.debug("Successfully written tiff sequence into file {}. With length: {} MBytes", outputTiff.getPath(), outputTiff.length() / MEGABYTE); } }
PdfServiceImpl implements PdfService { @Override public void generateTiffFromPdf(File inputPdf, File outputTiff) throws PdfServiceException { if (!inputPdf.exists()) { throw new PdfServiceException(inputPdf.getAbsolutePath() + " doesn't exists"); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff); PDDocument document = PDDocument.load(inputPdf)) { log.debug("Reading pdf from file path {}", outputTiff.getPath()); PDFRenderer pdfRenderer = new PDFRenderer(document); TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi(); ImageWriter writer = tiffspi.createWriterInstance(); log.debug("Preparing to generate multi image tiff."); writer.setOutput(ios); writer.prepareWriteSequence(null); log.debug("Pdf contains {} pages.", document.getNumberOfPages()); int page = 0; TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault()); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType("CCITT T.6"); writeParam.setCompressionQuality(0.75f); for (PDPage pdPage : document.getPages()) { log.debug("Rendering PDF page [{}/{}] as TIFF image", page, document.getNumberOfPages()); BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 200, ImageType.BINARY); IIOImage image = new IIOImage(bim, null, null); try { writer.writeToSequence(image, writeParam); } catch (UnsupportedOperationException e) { log.warn("Not supported compression:", e); writer.writeToSequence(image, null); } log.debug("Successfully written one image to the sequence, {} more to go.", document.getNumberOfPages() - page); page++; } ios.flush(); } catch (IOException e) { throw new PdfServiceException(e); } log.debug("Successfully written tiff sequence into file {}. With length: {} MBytes", outputTiff.getPath(), outputTiff.length() / MEGABYTE); }  }
PdfServiceImpl implements PdfService { @Override public void generateTiffFromPdf(File inputPdf, File outputTiff) throws PdfServiceException { if (!inputPdf.exists()) { throw new PdfServiceException(inputPdf.getAbsolutePath() + " doesn't exists"); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff); PDDocument document = PDDocument.load(inputPdf)) { log.debug("Reading pdf from file path {}", outputTiff.getPath()); PDFRenderer pdfRenderer = new PDFRenderer(document); TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi(); ImageWriter writer = tiffspi.createWriterInstance(); log.debug("Preparing to generate multi image tiff."); writer.setOutput(ios); writer.prepareWriteSequence(null); log.debug("Pdf contains {} pages.", document.getNumberOfPages()); int page = 0; TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault()); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType("CCITT T.6"); writeParam.setCompressionQuality(0.75f); for (PDPage pdPage : document.getPages()) { log.debug("Rendering PDF page [{}/{}] as TIFF image", page, document.getNumberOfPages()); BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 200, ImageType.BINARY); IIOImage image = new IIOImage(bim, null, null); try { writer.writeToSequence(image, writeParam); } catch (UnsupportedOperationException e) { log.warn("Not supported compression:", e); writer.writeToSequence(image, null); } log.debug("Successfully written one image to the sequence, {} more to go.", document.getNumberOfPages() - page); page++; } ios.flush(); } catch (IOException e) { throw new PdfServiceException(e); } log.debug("Successfully written tiff sequence into file {}. With length: {} MBytes", outputTiff.getPath(), outputTiff.length() / MEGABYTE); }  @Override String generatePdf(File xslFile, Source source); @Override String generatePdf(File xslFile, Source source, Map<String, String> parameters); @Override String generatePdf(String xslFilename, Source source, Map<String, String> parameters); @Override String generatePdf(File xslFile, Map<String, String> parameters); @Override String generatePdf(String xslFilename, Map<String, String> parameters); @Override void generatePdf(File xslFile, Source source, OutputStream targetStream, Map<String, String> parameters); @Deprecated @Override PDDocument append(PDDocument pdDocument, InputStream is, PDFMergerUtility pdfMergerUtility); @Deprecated @Override PDDocument append(PDDocument pdDocument, String filename, PDFMergerUtility pdfMergerUtility); @Override void generateTiffFromPdf(File inputPdf, File outputTiff); @Override void addSource(PDFMergerUtility pdfMergerUtility, InputStream is); @Override void addSource(PDFMergerUtility pdfMergerUtility, String filename); @Override void mergeSources(PDFMergerUtility pdfMergerUtility, String filename); @Override InputStream extractPages(InputStream is, String filename, List<Integer> pageNumbers); }
PdfServiceImpl implements PdfService { @Override public void generateTiffFromPdf(File inputPdf, File outputTiff) throws PdfServiceException { if (!inputPdf.exists()) { throw new PdfServiceException(inputPdf.getAbsolutePath() + " doesn't exists"); } try (ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff); PDDocument document = PDDocument.load(inputPdf)) { log.debug("Reading pdf from file path {}", outputTiff.getPath()); PDFRenderer pdfRenderer = new PDFRenderer(document); TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi(); ImageWriter writer = tiffspi.createWriterInstance(); log.debug("Preparing to generate multi image tiff."); writer.setOutput(ios); writer.prepareWriteSequence(null); log.debug("Pdf contains {} pages.", document.getNumberOfPages()); int page = 0; TIFFImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault()); writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParam.setCompressionType("CCITT T.6"); writeParam.setCompressionQuality(0.75f); for (PDPage pdPage : document.getPages()) { log.debug("Rendering PDF page [{}/{}] as TIFF image", page, document.getNumberOfPages()); BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 200, ImageType.BINARY); IIOImage image = new IIOImage(bim, null, null); try { writer.writeToSequence(image, writeParam); } catch (UnsupportedOperationException e) { log.warn("Not supported compression:", e); writer.writeToSequence(image, null); } log.debug("Successfully written one image to the sequence, {} more to go.", document.getNumberOfPages() - page); page++; } ios.flush(); } catch (IOException e) { throw new PdfServiceException(e); } log.debug("Successfully written tiff sequence into file {}. With length: {} MBytes", outputTiff.getPath(), outputTiff.length() / MEGABYTE); }  @Override String generatePdf(File xslFile, Source source); @Override String generatePdf(File xslFile, Source source, Map<String, String> parameters); @Override String generatePdf(String xslFilename, Source source, Map<String, String> parameters); @Override String generatePdf(File xslFile, Map<String, String> parameters); @Override String generatePdf(String xslFilename, Map<String, String> parameters); @Override void generatePdf(File xslFile, Source source, OutputStream targetStream, Map<String, String> parameters); @Deprecated @Override PDDocument append(PDDocument pdDocument, InputStream is, PDFMergerUtility pdfMergerUtility); @Deprecated @Override PDDocument append(PDDocument pdDocument, String filename, PDFMergerUtility pdfMergerUtility); @Override void generateTiffFromPdf(File inputPdf, File outputTiff); @Override void addSource(PDFMergerUtility pdfMergerUtility, InputStream is); @Override void addSource(PDFMergerUtility pdfMergerUtility, String filename); @Override void mergeSources(PDFMergerUtility pdfMergerUtility, String filename); @Override InputStream extractPages(InputStream is, String filename, List<Integer> pageNumbers); static final long MEGABYTE; static final int MAX_MAIN_MEMORY_BYTES; }