2

I am writing a code the convert tiff files to pdf using pdfbox but I receive the below error when reading some pages of the TIFF file. This does not happen to all files or all pages.

        PDDocument pdfDoc = new PDDocument();
    ImageInputStream isb = ImageIO.createImageInputStream(is);

    Iterator<ImageReader> iterator = ImageIO.getImageReaders(isb);
    if (iterator == null || !iterator.hasNext())
    {
        throw new IOException("Image file format not supported by ImageIO: ");
    }

    ImageReader reader = (ImageReader) iterator.next();
    iterator = null;
    reader.setInput(isb);

    int nbPages = reader.getNumImages(true);

    for(int p=0;p<nbPages;p++)
    {
        BufferedImage bufferedImage = reader.read(p);

        PDPage page = new PDPage();
        pdfDoc.addPage(page);

        PDImageXObject image = LosslessFactory.createFromImage(pdfDoc, bufferedImage);

        PDPageContentStream content =new PDPageContentStream(pdfDoc, page);
        content.drawImage(image, 0,0 ,page.getMediaBox().getWidth(),page.getMediaBox().getHeight());

        content.close();
        iterator = null;
    }
    pdfDoc.save(os);
    pdfDoc.close();

Error received is below

java.lang.ArrayIndexOutOfBoundsException: Index 23465 out of bounds for length 23465 at java.desktop/com.sun.imageio.plugins.tiff.TIFFFaxDecompressor.nextLesserThan8Bits(TIFFFaxDecompressor.java:1540) at java.desktop/com.sun.imageio.plugins.tiff.TIFFFaxDecompressor.decodeT6(TIFFFaxDecompressor.java:1080) at java.desktop/com.sun.imageio.plugins.tiff.TIFFFaxDecompressor.decodeRaw(TIFFFaxDecompressor.java:655) at java.desktop/com.sun.imageio.plugins.tiff.TIFFDecompressor.decode(TIFFDecompressor.java:2526) at java.desktop/com.sun.imageio.plugins.tiff.TIFFImageReader.decodeTile(TIFFImageReader.java:1124) at java.desktop/com.sun.imageio.plugins.tiff.TIFFImageReader.read(TIFFImageReader.java:1342) at java.desktop/javax.imageio.ImageReader.read(ImageReader.java:938) at com.allstate.acg.claims.tiff.TiffToPDF.convert(TiffToPDF.java:37) at com.allstate.acg.claims.tiff.TiffToPDF.main(TiffToPDF.java:60)

Any help on what could be causing this?

NewCoder
  • 21
  • 2
  • I think it is related to broken images. See: https://itext-questions.narkive.com/SDwiR0tY/error-on-pdf-from-a-tiff-file – pringi Feb 10 '22 at 15:03
  • Is there any easy way of checking if the image is broken? – NewCoder Feb 11 '22 at 13:42
  • 1
    Does other software read your TIFF file? If you attach/link the TIFF file in question, we could check it for you. – Harald K Feb 11 '22 at 17:41
  • I was able to convert it using the Adobe Acrobat reader but not through the application. I tried uploading the TIFF document but was not able to upload it here. – NewCoder Feb 15 '22 at 16:34
  • We decided to just replace the broken pdf file with a local pdf version so we do not have to convert certain files. Thanks everyone for helping out. – NewCoder Feb 17 '22 at 18:37

0 Answers0