/** * @see #testExtractImageLikeSteveB() */ private static List<BufferedImage> FindImages(PdfReader reader, PdfDictionary pdfPage) throws IOException { List<BufferedImage> result = new ArrayList<>(); Iterable<PdfObject> imgPdfObject = FindImageInPDFDictionary(pdfPage); for (PdfObject image : imgPdfObject) { int xrefIndex = ((PRIndirectReference)image).getNumber(); PdfObject stream = reader.getPdfObject(xrefIndex); // Exception occurs here : PdfImageObject pdfImage = new PdfImageObject((PRStream)stream); BufferedImage img = pdfImage.getBufferedImage(); // Do something with the image result.add(img); } return result; }
private void unpack(Set<PdfDictionary> dictionaries) throws TaskIOException { for (PdfDictionary dictionary : dictionaries) { PdfName type = dictionary.getAsName(PdfName.TYPE); if (PdfName.F.equals(type) || PdfName.FILESPEC.equals(type)) { PdfDictionary ef = dictionary.getAsDict(PdfName.EF); PdfString fn = dictionary.getAsString(PdfName.F); if (fn != null && ef != null) { PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F)); if (prs != null) { File tmpFile = copyToTemporaryFile(prs); outputWriter.addOutput(file(tmpFile).name(fn.toUnicodeString())); } } } } }
private File copyToTemporaryFile(PRStream prs) throws TaskIOException { File tmpFile = createTemporaryBuffer(); LOG.debug("Created output temporary buffer {}", tmpFile); ByteArrayInputStream inputStream = null; try { inputStream = new ByteArrayInputStream(PdfReader.getStreamBytes(prs)); FileUtils.copyInputStreamToFile(inputStream, tmpFile); LOG.debug("Attachment unpacked to temporary buffer"); } catch (IOException e) { throw new TaskIOException("Unable to copy attachment to temporary file.", e); } finally { IOUtils.closeQuietly(inputStream); } return tmpFile; }