We're sorry Aspose doesn't work properply without JavaScript enabled.

# 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);
        <span style="color:green;">// Read the image from file, ensure it is disposed.</span>
<span style="color:blue;">using</span> (<span style="color:#2b91af;">Image</span> image = <span style="color:#2b91af;">Image</span>.FromFile(inputFileName))
{

<span style="color:green;">// Get the number of frames in the image.</span>
<span style="color:blue;">int</span> framesCount = image.GetFrameCount(<span style="color:#2b91af;">FrameDimension</span>.Page);

<span style="color:green;">// Loop through all frames.</span>
<span style="color:blue;">for</span> (<span style="color:blue;">int</span> frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
<span style="color:green;">// Insert a section break before each new page, in case of a multi-frame TIFF.</span>
<span style="color:blue;">if</span> (frameIdx != 0)
builder.InsertBreak(<span style="color:#2b91af;">BreakType</span>.SectionBreakNewPage);

<span style="color:green;">// Select active frame.</span>
image.SelectActiveFrame(<span style="color:#2b91af;">FrameDimension</span>.Page, frameIdx);

<span style="color:green;">// We want the size of the page to be the same as the size of the image.</span>
<span style="color:green;">// Convert pixels to points to size the page to the actual image size.</span>
<span style="color:#2b91af;">PageSetup</span> ps = builder.PageSetup;
ps.PageWidth = <span style="color:#2b91af;">ConvertUtil</span>.PixelToPoint(image.Width, image.HorizontalResolution);
ps.PageHeight = <span style="color:#2b91af;">ConvertUtil</span>.PixelToPoint(image.Height, image.VerticalResolution);

<span style="color:green;">// Insert the image into the document and position it at the top left corner of the page.</span>
builder.InsertImage(image,
<span style="color:#2b91af;">RelativeHorizontalPosition</span>.Page,
0,
<span style="color:#2b91af;">RelativeVerticalPosition</span>.Page,
0,
ps.PageWidth,
ps.PageHeight,
<span style="color:#2b91af;">WrapType</span>.None);
}
}

<span style="color:green;">// Save the document to PDF.</span>
doc.Save(outputFileName);
} <br><br>But when it reach at <br><span style="color:blue;">int</span> framesCount = image.GetFrameCount(<span style="color:#2b91af;">FrameDimension</span>.Page);

It is throwing an error "A generic error occurred in GDI+"Can you please help me on thisRegardsAnish

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.

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.

<span style=“font-size:
image from file, ensure it is disposed.<o:p></o:p>

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.

<span style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:
SimSun;mso-fareast-theme-font:minor-fareast;mso-ansi-language:EN-NZ;mso-fareast-language:
ZH-CN;mso-bidi-language:AR-SA;mso-no-proof:yes”> 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.