0

I am using the below code for converting tiff to pdf It works fine for tiff images of dimensions 850*1100.But when I am trying to give the input tiff image of dimensions(Eg :- 1574*732, 684*353 or other 850*1100), I am getting the below error. Please help me how to convert tiff images of different dimensions to pdf.

Error Occured for below code .Compression JPEG is only supported with a single strip. This image has 45 strips.

RandomAccessFileOrArray myTifFile = null;
com.itextpdf.text.Document tiffToPDF= null;
PdfWriter pdfWriter = null;
try{
        myTifFile = new RandomAccessFileOrArray(fileName);
        int numberOfPages = TiffImage.getNumberOfPages(myTifFile);
        tiffToPDF = new com.itextpdf.text.Document(PageSize.LETTER_LANDSCAPE);      
        String temp = fileName.substring(0, fileName.lastIndexOf("."));
        pdfWriter = PdfWriter.getInstance(tiffToPDF, new FileOutputStream(temp+".pdf"));
        pdfWriter.setStrictImageSequence(true);
        tiffToPDF.open();
        for(int tiffImageCounter = 1;tiffImageCounter <= numberOfPages;tiffImageCounter++) 
             {
                Image img = TiffImage.getTiffImage(myTifFile, tiffImageCounter);

                img.setAbsolutePosition(0,0);

                img.scaleToFit(612,792);

                tiffToPDF.add(img);

                tiffToPDF.newPage();
            } 

        }
byreddy
  • 23
  • 1
  • 3
  • 9
  • 1
    What iText version are you using? A lot of TIFFs are corrupted or don't follow the spec all too closely and I have added fallbacks in iText recently. I see you're using com.itextpdf.text.Document in your code sample here, but as a comment to an answer it appears you're using 2.x: "com.lowagie.text.pdf.RandomAccessFileOrArray". So which version is it? – Michaël Demey Jul 31 '14 at 08:40
  • In my code I am using itextpdf-5.4.1.jar – byreddy Jul 31 '14 at 08:47
  • Can you share your TIFF? Can you upgrade to 5.5.2? – Michaël Demey Jul 31 '14 at 08:50
  • I should mention that it's probably not going to work using only iText. The exception message is quite clear: Compression JPEG is only supported with a single strip. This image has 45 strips. – Michaël Demey Jul 31 '14 at 08:51
  • [link] https://www.dropbox.com/s/7t469wdoccwayzq/02.tif. I will replace the jar with latest version and will check it. – byreddy Jul 31 '14 at 09:08
  • Still the same error persists, even after replacing with new jar file. – byreddy Jul 31 '14 at 09:15
  • I looked into it and iText does not support JPEG Compression with multiple strips and I'm not sure if we're going to add such support. I tried looking into alternative methods of getting the image in a PDF, like using Java Advanced Imaging to preprocess the image, but it failed. I haven't tried any other imaging libs (like the apache one), but if you can get a Java Image object of the tif, you can add it to a PDF using iText. – Michaël Demey Jul 31 '14 at 14:14

1 Answers1

1

This code will explain how you can convert tiff to pdf.. more information can be found here and here

   import com.itextpdf.text.pdf.RandomAccessFileOrArray;
//Read Tiff File, Get number of Pages
import com.itextpdf.text.pdf.codec.TiffImage;
//We need the library below to write the final 
//PDF file which has our image converted to PDF
import java.io.FileOutputStream;
//The image class to extract separate images from Tiff image
import com.itextpdf.text.Image;
//PdfWriter object to write the PDF document
import com.itextpdf.text.pdf.PdfWriter;
//Document object to add logical image files to PDF
import com.itextpdf.text.Document;
public class TiffToPDF {
public static void main(String args[]){
    try{
        //Read the Tiff File
        RandomAccessFileOrArray myTiffFile=new RandomAccessFileOrArray("c:\\java\\test.tif");
        //Find number of images in Tiff file
        int numberOfPages=TiffImage.getNumberOfPages(myTiffFile);
        System.out.println("Number of Images in Tiff File" + numberOfPages);
        Document TifftoPDF=new Document();
        PdfWriter.getInstance(TifftoPDF, new FileOutputStream("c:\\java\\tiff2Pdf.pdf"));
        TifftoPDF.open();
        //Run a for loop to extract images from Tiff file
        //into a Image object and add to PDF recursively
        for(int i=1;i<=numberOfPages;i++){
            Image tempImage=TiffImage.getTiffImage(myTiffFile, i);
            TifftoPDF.add(tempImage);
        }
        TifftoPDF.close();
        System.out.println("Tiff to PDF Conversion in Java Completed" );
    }
    catch (Exception i1){
        i1.printStackTrace();
    }      
    }    
}
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
  • 1
    I have checked with the above code it works for tiff image of dimensions 850*1100 . but it shows an error for tiffs of different dimensions . **ExceptionConverter: java.io.EOFException** – byreddy Jul 31 '14 at 06:52
  • refer the links@byr and also [this link](http://stackoverflow.com/questions/664331/when-will-an-eofexception-occur-in-javas-streams) – Deepanshu J bedi Jul 31 '14 at 06:56
  • I have checked the links also , still the same kind of error persists.I reffered the link in the above comment , I have placed myTifFile.read(); but still the problem persists. – byreddy Jul 31 '14 at 07:08
  • can you provide the errors? please edit your question to provide errors. – Deepanshu J bedi Jul 31 '14 at 07:09
  • ExceptionConverter: java.io.EOFException at com.lowagie.text.pdf.RandomAccessFileOrArray.readUnsignedShort(Unknown Source) at com.lowagie.text.pdf.codec.TIFFDirectory.getNumDirectories(Unknown Source) at com.lowagie.text.pdf.codec.TiffImage.getNumberOfPages(Unknown Source) at com.viewer.download.DocumentConversion.generatePDFFromTIF(DocumentConversion.java:218) at com.viewer.download.DocumentConversion.main(DocumentConversion.java:271) – byreddy Jul 31 '14 at 07:11
  • @byr will get back to you when i try it and solve it. – Deepanshu J bedi Jul 31 '14 at 07:13