Exception when converting tiff file to pdf file wi

2020-04-11 03:40发布

问题:

When I try to convert a tiff file to pdf file by using itextpdf 5.5.5, the following exception occurs:

java.lang.RuntimeException: Scanline must begin with EOL code word. at com.itextpdf.text.pdf.codec.TIFFFaxDecoder.readEOL(TIFFFaxDecoder.java:1303) at com.itextpdf.text.pdf.codec.TIFFFaxDecoder.decode2D(TIFFFaxDecoder.java:811) at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:223) at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:315) at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:303) at com.itextpdf.text.Image.getInstance(Image.java:308) at com.itextpdf.text.Image.getInstance(Image.java:242) at com.itextpdf.text.Image.getInstance(Image.java:365) at com.minstone.convert.PicConvertor.convertPicToPdf1(PicConvertor.java:81) at com.minstone.convert.DocConverter$ConvertWork.king2pdf(DocConverter.java:143) at com.minstone.convert.DocConverter$ConvertWork.run(DocConverter.java:99) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619)

I can open the tiff file in any image viewer, so it's valid.

回答1:

iText has a few fallbacks when dealing with invalid or corrupt Tiff files. By default, these fallbacks aren't used, you'll need to explicitly use one of the getinstance() methods with the recoverFromImageError flag set to true if you want iText to try and parse the invalid Tiff files (e.g. http://api.itextpdf.com/itext/com/itextpdf/text/Image.html#getInstance%28byte[],%20boolean%29 )

If this boolean is set to true iText will only throw an error if it exhausted all of its options. This should be a sign to inspect your TIFF file...

Another workaround could be to use TiffImage and bypassing the Image class altogether. TiffImage also uses the recoverFromImageError flag, but it also has an additional flag called "direct" which might also solve your issues. (http://api.itextpdf.com/itext/com/itextpdf/text/pdf/codec/TiffImage.html)



回答2:

Since there is no code snippet available, I guess you were using below code to read the image.

Image tiffImg=TiffImage.getTiffImage(randomAccessFileOrArrayObj, pageNum);

If so change it to (Add true to try recovering the file)

Image tiffImg=TiffImage.getTiffImage(randomAccessFileOrArrayObj, true, pageNum);