How to avoid an exception when importing a TIFF file?
When I try to convert a specific tiff file to PDF, the following exception occurs:
java.lang.RuntimeException: Scanline must begin with EOL code word.
I can open the tiff file in any image viewer, so it's valid.
Posted on StackOverflow on Apr 22, 2015 by hecatcat
Comment by Bruno: this statement is not always true: I can open the tiff file in any image viewer, so it's valid. A TIFF file can be invalid and open in an image viewer anyway because the image viewer tolerates errors. iText can also tolerate errors, as explained in the answer provided by Michaƫl Demey (from iText).
iText has a few fallbacks when dealing with invalid or corrupt Tiff files. By default, these fallbacks aren't used, you'll need to use one of the create()
methods with the recoverImage
flag set to true if you want iText to try and parse the invalid Tiff files create(String filename, boolean recoverImage)
. If the boolean parameter is set to true, iText will only throw an error if it exhausted all of its options.
Image img = new Image(ImageDataFactory.create(path, true));
Another workaround could be to use the createTiff()
, it also uses the recoverFromImageError
flag, but it also has an additional flag called direct
: createTiff(URL url, boolean recoverFromImageError, int page, boolean direct)
. This might also solve your issues.
Image tiff = new Image(ImageDataFactory.createTiff(new File(path).toURI().toURL(), true, page, true))
Click this link if you want to see how to answer this question in iText 5.