Q: We are trying to export a PDF to TIFF using jPDFImages and are getting a Negative Array Size Exception. What could be the issue?
Sample Code:
public static void testPDFToTiff() throws Exception{ PDFImages pdf = new PDFImages("C:\\mydoc.pdf", null); TIFFOptions options = new TIFFOptions(300, TIFFOptions.TIFF_CCITT_T6); pdf.saveDocumentAsTIFF("C:\\mydoc.tif", options); } |
Error message in the log:
Exception in thread "main" java.lang.NegativeArraySizeException
at java.awt.image.DataBufferInt.(DataBufferInt.java:75)
at java.awt.image.Raster.createPackedRaster(Raster.java:467)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
at java.awt.image.BufferedImage.(BufferedImage.java:340)
at com.qoppa.b.y.d(Unknown Source)
at com.qoppa.b.d.ae.b(Unknown Source)
at com.qoppa.b.u.b(Unknown Source)
at com.qoppa.pdfImages.PDFImages.saveDocumentAsTIFF(Unknown Source)
A: You are trying to create an image that too big.
Image Size Limit
When exporting a PDF page to image, internally, Qoppa PDF engine needs to create a BufferedImage.
The formula to determine the size of this internal BufferedImage is simple:
Image Size = Image Height x Image Width = (DPI x Page Width) x (DPI x Page Height)
For a standard A4 page size, and a resolution of 300 DPI, the image size is about 9 million pixels.
Image Size = (8.27 × 300) x (11.7 x 300) = 8,708,310
For a huge page size of 180 in x 180 in, at the same 300 DPI, the image size is about 3 billions pixels:
Image Size = (180 x 300) x (180 x 300) = 2,916,000,000
Qoppa’s PDF rendering code is unable to create an image of this size as it is higher than the Integer.MAX_VALUE which is about 2 billions.
Workaround
As a workaround, you can either export the TIFF at a lower resolution or use the function getPageSubImage() to only export a section of a page at a time.
For instance, for the image page size of 180in x 180in, lowering the resolution to 100 DPI, would create an image size of 324 millions pixels, which is large but would not give you an error:
Image Size = (180 x 100) x (180 x 100) = 324,000,000
For the next library version v2017R1, we will enhance our code in order to throw a PDFException with a more informative message in such cases.