Converting gif to pdf

I am using aspose component
With that i try to convert a gif file to pdf by using the following code.

public static void ConvertImageToPdf(string inputFileName, string outputFileName)
{
    // Create Aspose.Words.Document and DocumentBuilder.

    // The builder makes it simple to add content to the document.

    Document doc = new Document();

    DocumentBuilder builder = new DocumentBuilder(doc);
    // Read the image from file, ensure it is disposed.
    using (Image image = Image.FromFile(inputFileName))
    {
        // Get the number of frames in the image.
        int framesCount = image.GetFrameCount(FrameDimension.Page);
        // Loop through all frames.
        for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
        {
            // Insert a section break before each new page, in case of a multi-frame TIFF.
            if (frameIdx != 0)

                builder.InsertBreak(BreakType.SectionBreakNewPage);
            // Select active frame.
            image.SelectActiveFrame(FrameDimension.Page, frameIdx);
            // We want the size of the page to be the same as the size of the image.
            // Convert pixels to points to size the page to the actual image size.
            PageSetup ps = builder.PageSetup;
            ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
            ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
            // Insert the image into the document and position it at the top left corner of the page.

            builder.InsertImage(image,
            RelativeHorizontalPosition.Page,
            0,
            RelativeVerticalPosition.Page,
            0,
            ps.PageWidth,
            ps.PageHeight,
            WrapType.None);

        }
    }
    // Save the document to PDF.

    doc.Save(outputFileName);
}

But when it reach at

int framesCount = image.GetFrameCount(FrameDimension.Page);

It is throwing an error “A generic error occurred in GDI+”
Can you please help me on this

Regards
Anish

Hi Anish,

Thanks for your inquiry. I have tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem as WORDSNET-6988 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Moreover, please try using the following code snippet instead:

DocumentBuilder builder = new DocumentBuilder();
using (Image image = Image.FromFile(@"C:\temp\in.gif"))
{
    PageSetup ps = builder.PageSetup;
    ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
    ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

    builder.InsertImage(image,
    RelativeHorizontalPosition.Page,
    0,
    RelativeVerticalPosition.Page,
    0,
    ps.PageWidth,
    ps.PageHeight,
    WrapType.None);

}
builder.Document.Save(@"C:\temp\out.pdf");

Best Regards,

Hi Anish,

Thanks for reporting this issue to us. You were correct, the code from that article was not working correctly for GIF images. It turns out that TIFF images store frames “page based” whereas the frames of a GIF images are “time based”. Therefore TIFF frames are extracted by using FrameDimension.Page and GIF by using FrameDimension.Time.

I have modified the example code to pick the appropriate dimension found in the image. Please see the code changes below.

// Read the image from file, ensure it is disposed.
using (Image image = Image.FromFile(inputFileName))
{
    // Find which dimension the frames in this image represent. For example
    // the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
    FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
    // Get the number of frames in the image.
    int framesCount = image.GetFrameCount(dimension);
    // Loop through all frames.
    for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
    {
        // Insert a section break before each new page, in case of a multi-frame TIFF.
        if (frameIdx != 0)
            builder.InsertBreak(BreakType.SectionBreakNewPage);
        // Select active frame.

        image.SelectActiveFrame(dimension, frameIdx);

Thanks,

The issues you have found earlier (filed as WORDSNET-6988) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.