Convert GIF to PDF iText Java Example Tutorial

In this tutorial, we will discuss how to convert an animated GIF image into a PDF document using Java iText library. We need to make note of the fact that PDF does not support animated images by itself. Itext provides a class com.itextpdf.text.pdf.codec.GifImage which can be used to extract specific frames out of a GIF image to an Image object and then stamp the resulting Image object into a PDF document. It is also possible to extract all the frames in a loop (similar to our Tiff to PDF conversion) and then embed each frame as a separate image inside a PDF file. In this tutorial, we will discuss how to extract all the frames out of a GIF image and embed them into a PDF document. You may wish to edit the code to suit to your needs if you are looking to extract only specific frames, to  avoid redundancy. Later on, we will also provide a servlet example that will accept an user uploaded GIF file and render a PDF document back to the user.The complete commented code to convert a GIF file frame by frame to a PDF file is provided below;
import java.io.FileOutputStream;
//Image class to extract frames from GIF 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 frames to PDF
import com.itextpdf.text.Document;
//we need the class below to read input GIF file
import com.itextpdf.text.pdf.codec.GifImage;
public class GifToPDF {
public static void main(String[] args) {
try{
    //Create Document Object
    Document convertGif2Pdf=new Document();
    //Create PdfWriter for Document to hold physical file
    PdfWriter.getInstance(convertGif2Pdf, new FileOutputStream("c:\\myjava\\ConvertImagetoPDF.pdf"));
    convertGif2Pdf.open();
    GifImage myGif=new GifImage("c:\\myjava\\test.gif");
    //Get the number of frames in Gif Image
    System.out.println("Number of Frames to be converted" + myGif.getFrameCount());
    for(int i=1;i<=myGif.getFrameCount();i++)
    {
            convertGif2Pdf.add(myGif.getImage(i));
    }    
    //Close Document
    convertGif2Pdf.close();
    System.out.println("Successfully Converted GIF to PDF in iText");
}
catch (Exception i1){
    i1.printStackTrace();
}
}
}
Now, when you run this code against some animated GIF files, you will only get a skeleton in subsequent frames and not the full image.(refer to example below) This is due to the underlying technique used to create GIF file by eliminating redundany picture information and provide only the part that differs from previous frames. You may wish to have only one active frame of the GIF file to be converted to PDF in such cases; The Java code example for this case would be provided in subsequent tutorials.
Redundant Frame in PDF files when Converting Gif to PDF
Redundant Frame in PDF files when Converting Gif to PDF
Above: An example where this code extracts a frame which contains only the changed part from the first frame. For such images, an approach to convert them to PDF will be explained in the next tutorial.

No comments:

Post a Comment