Java – Convert Tiff to Pdf in java using itext

itextjavapdfpdfboxtiff

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();
            } 

        }

Best Solution

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();
    }      
    }    
}