Convert TIFF to PDF iText Java Example Tutorial

In this example, we will discuss how to convert a TIFF image to a PDF document using Java iText library. As you may be aware, a TIFF image contains multiple images tagged into a single file, often produced by a scanner. Converting such images to PDF requires extracting individual images from the TIFF file and write them to PDF document one after another. There are surplus examples available in the web, but I want to provide one that hits the requirement straight. Simple code, changes a TIFF to PDF and provides a PDF file in the output. Here we go with this tutorial. You will need iText version  5.1.0 or equivalent to get started with this example. A commented step by step code is provided below;
package jpegtopdf;
//This object will hold our Tiff File
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();
    }      
    }    
}
Now, this code accepts a Tiff image and changes it to a PDF by using Java and iText. The code essentially extracts individual image files from each page and assigns it to a Image object. This Image object is written to PDF document using Document.add() method. We will also provide an example to accept an user uploaded document (Tiff file) and then use a servlet to render a PDF back to the browser in another tutorial. Keep watching this space for more.

10 comments:

  1. I am getting an error during compiling:

    Error: PdfBody$PdfCrossReference not found in class com.itextpdf.text.pdf.PdfWriter.PdfBody.PdfCrossReference

    Any ideas?

    Thxs.

    ReplyDelete
    Replies
    1. hi is the issue resolved, because i am facing the same issue

      Delete
  2. Are you trying the same code? Can you post your iText version?

    ReplyDelete
  3. I'm getting this exception after use TiffImage.getTiffImage

    com.lowagie.text.ExceptionConverter: The document has no pages.

    could you help me?

    ReplyDelete
  4. Hi, sure, can you send us your TIFF image so we can have a look? Send it to thinktibits88@gmail.com

    ReplyDelete
  5. Hello.
    Thanks for the exampel. Hugely helpfull.
    However, I get an output PDF that contains only the top left corner (about a fourth) of the original TIFF-image.
    I've been looking in the API for any method calls I can do to set the size of the PDF, but get no change.
    E.g:
    for(int i=1;i<=numberOfPages;i++){
    Image tempImage=TiffImage.getTiffImage(myTiffFile, i);
    TifftoPDF.setPageSize(new com.itextpdf.text.Rectangle(40000, 80000)); //I tried to increase the size of the PDF document page here... but no change...
    TifftoPDF.add(tempImage);
    }
    input tiff-file: https://www.dropbox.com/s/rsa4v9dods83krr/blankett.tif?dl=0
    output pdf-file: https://www.dropbox.com/s/2i24rd9df6itn26/blankett2.pdf?dl=0

    ReplyDelete
    Replies
    1. Solved it!
      tempImage.scaleToFit(pdfDoc.getPageSize());

      Delete
  6. Hi Am getting half of the PDF content.

    ReplyDelete
  7. i am also getting half content , were you able to fix?

    ReplyDelete
  8. I am getting error:

    java.lang.IllegalArgumentException: Bad endianness tag (not 0x4949 or 0x4d4d).
    at com.itextpdf.text.pdf.codec.TIFFDirectory.getNumDirectories(TIFFDirectory.java:618)
    at com.itextpdf.text.pdf.codec.TiffImage.getNumberOfPages(TiffImage.java:75)
    at pruebasPDF.Convetir.main(Convetir.java:15)

    Any ideas?

    ReplyDelete