1

I have a code to convert first page of pdf document to png format image. The code looks working fine with most of pdf file, except this one:https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwiqp4KovobSAhXJ54MKHdtbD54QFggeMAA&url=http%3A%2F%2Fitrevolution.com%2Fwp-content%2Fuploads%2Ffiles%2FPhoenixProjectExcerpt.pdf&usg=AFQjCNGYt1ALVc2ramVw_oM4Qb4rQCTDmw&cad=rja This document give me a white page for first page. Not sure what is wrong here.

PDDocument pdf = PDDocument.loadNonSeq(file, new RandomAccessFile(tmp_file, "rw"));
List<PDPage> pdPages = pdf.getDocumentCatalog().getAllPages();
PDPage page = pdPages.get(0);
BufferedImage bim = page.convertToImage(BufferedImage.TYPE_INT_RGB, 300);
ImageIOUtil.writeImage(bim, thumbnailPath + "/" + objectId +  ".png", 300);
ouyadi
  • 475
  • 1
  • 6
  • 13

2 Answers2

3

Adding this dependency to my POM helped me (used pdfbox 2.0.8):

<dependency>
    <groupId>com.github.jai-imageio</groupId>
    <artifactId>jai-imageio-jpeg2000</artifactId>
    <version>1.3.0</version>
</dependency>
Victoria Agafonova
  • 2,048
  • 10
  • 33
  • 50
1

When I run a similar program using PDFBox 2.0.3 (your code looks like 1.8.x code):

try (PDDocument pdf = PDDocument.load(file)) {
  PDFRenderer pdfRenderer = new PDFRenderer(pdf);
  BufferedImage bim = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
  ImageIOUtil.writeImage(bim, outputFileName, 300);
}

I get the following output:

Feb 10, 2017 10:43:33 PM org.apache.pdfbox.contentstream.PDFStreamEngine operatorException
SEVERE: Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed

I believe the JPEG2000 format support in JAI was a little confusing (requires an additional jar containing the SPI, but it is difficult to get hold of).

More details about the issue are in PDFBox issue 1752, with a potential workaround using the decoder that JPedal uses. I imagine that with a suitable JPEG2000 JAI codec available (and detected) in your JRE the image is recognised.

James Fry
  • 1,133
  • 1
  • 11
  • 28