使用itext将Tiff转换为PDF中的Pdf

时间:2014-07-31 06:36:05

标签: java pdf itext tiff pdfbox

我使用以下代码将tiff转换为pdf 它适用于尺寸为850 * 1100的tiff图像。但是当我试图给出尺寸的输入tiff图像(例如: - 1574 * 732,684 * 353或其他850 * 1100)时,我得到以下错误。请帮助我如何将不同尺寸的tiff图像转换为pdf。

错误发现以下代码。压缩JPEG仅支持单个条带。此图片有45个条带。

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

        }

1 个答案:

答案 0 :(得分:1)

此代码将说明如何将tiff转换为pdf ..可以找到更多信息herehere

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