Free Support Forum - aspose.com

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)) {

                    continue;

                }

                // 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";

                gifBlock.save(tempCachedFileName, objTiff);

            }

            gif.close();

        }

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

        // https://github.com/aspose-pdf/Aspose.Pdf-for-Java

        // 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

        java.io.FileInputStream fs = new java.io.FileInputStream(cachedFileName);

        // Set margins so image will fit, etc.

        page.getPageInfo().getMargin().setBottom(0);

        page.getPageInfo().getMargin().setTop(0);

        page.getPageInfo().getMargin().setLeft(0);

        page.getPageInfo().getMargin().setRight(0);

        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

        page.getParagraphs().add(image1);

        // Set the image file stream

        image1.setImageStream(fs);

        // Save resultant PDF file

        doc.save(outputPdfLocation());

@kshengivari

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

BufferedImage readImage = null;

try {
 readImage = ImageIO.read(new 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.getPageInfo().setHeight(h);
 page.getPageInfo().setWidth(w);
 page.getPageInfo().getMargin().setBottom(0);
 page.getPageInfo().getMargin().setTop(0);
 page.getPageInfo().getMargin().setRight(0);
 page.getPageInfo().getMargin().setLeft(0);
 page.getParagraphs().add(image);
 doc.save(dataDir + "ImagetoPDF.pdf");
} catch (Exception e) {
 readImage = null;
}