Free Support Forum -

Color Gif conversion to PDF does not work

I am not sure what is the best approach to convert GIF to pdf. I got exception when i use the following code for the gif document.129754450.gif (36.9 KB)

try (com.aspose.imaging.Image objImage = com.aspose.imaging.Image.load(cachedFileName)) {

            // Convert the image to GIF image

            GifImage gif = (GifImage) objImage;

            // iterate through arry of blocks in the GIF image

            IGifBlock[] blocks = gif.getBlocks();

            for (int i = 0; i < blocks.length; i++) {

                // Check if gif block is then ignore it

                if (!(blocks[i] instanceof GifFrameBlock)) {



                // convert block to GifFrameBlock class instance

                GifFrameBlock gifBlock = ((GifFrameBlock) (blocks[i]));

                // Create an instance of TIFF Option class

                TiffOptions objTiff = new TiffOptions(TiffExpectedFormat.Default);

                // Save the GIFF block as TIFF image

                String tempCachedFileName = BUFFER_PATH + get_record().getBatch() + "\\" + get_record().get_doc() + "_" + (i+1) + ".tif";

      , objTiff);




Sorry I should use this code below. The last post was for converting to TIF which is not what i wanted to do. The real problem is that the pdf does not retain the aspect ratio for the document. How can i know the width and height of the original gif?

// For complete examples and data files, please go to


        // Instantiate Document Object

        Document doc = new Document();

        // Add a page to pages collection of document

        Page page = doc.getPages().add();

        // Load the source image file to Stream object fs = new;

        // Set margins so image will fit, etc.





        page.setCropBox(new Rectangle(0, 0, 400, 400));

        // Create an image object

        Image image1 = new Image();

        // Add the image into paragraphs collection of the section


        // Set the image file stream


        // Save resultant PDF file;


You can read image dimensions from BufferedImage and set PDF Page height/width accordingly. Please check below code:

BufferedImage readImage = null;

try {
 readImage = File(dataDir + "Fig_1.jpeg"));
 int h = readImage.getHeight();
 int w = readImage.getWidth();

 Document doc = new Document();
 Page page = doc.getPages().add();
 com.aspose.pdf.Image image = new Image();
 image.setFile(dataDir + "Fig_1.jpeg");
 page.getParagraphs().add(image); + "ImagetoPDF.pdf");
} catch (Exception e) {
 readImage = null;