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

Free Support Forum - aspose.com

Image to pdf

Hi ,

I got the below code from aspose site to convert image to PDF.
But after conversion the image is showing at the top left position of the pdf document.
Ho can i make the image fit to the pdf document.

Please see the code which i am using.

public static void ConvertImageToPdf(string inputFileName, string outputFileName)
        Aspose.Words.<span style="color:#2b91af;">Document</span> doc = <span style="color:blue;">new</span> Aspose.Words.<span style="color:#2b91af;">Document</span>();
        <span style="color:#2b91af;">DocumentBuilder</span> builder = <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentBuilder</span>(doc);

        <span style="color:green;">// Read the image from file, ensure it is disposed.</span>
        <span style="color:blue;">using</span> (System.Drawing.<span style="color:#2b91af;">Image</span> image = System.Drawing.<span style="color:#2b91af;">Image</span>.FromFile(inputFileName))
            <span style="color:#2b91af;">FrameDimension</span> dimension = <span style="color:blue;">new</span> <span style="color:#2b91af;">FrameDimension</span>(image.FrameDimensionsList[0]);
            <span style="color:green;">//int framesCount = image.GetFrameCount(dimension);</span>

            <span style="color:blue;">int</span> framesCount = 1;

            <span style="color:blue;">try</span>

                framesCount = image.GetFrameCount(<span style="color:#2b91af;">FrameDimension</span>.Page);


            <span style="color:blue;">catch</span>

                <span style="color:green;">// do nothing.</span>


            <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>
                <span style="color:green;">//builder.InsertImage(image, ps.PageWidth, ps.PageHeight);</span>

                    <span style="color:#2b91af;">RelativeHorizontalPosition</span>.Page,
                    <span style="color:#2b91af;">RelativeVerticalPosition</span>.Page,
                    <span style="color:#2b91af;">WrapType</span>.None);

        <span style="color:green;">// Save the document to PDF.</span>
    } <br><br><br>Regards<br>Anish<br></pre><br>


I downloaded the latest version of aspose.word.dll v13.5

But now the generated tif from JPG is showing as blank.

Can you please help me to correct this.

Please find the attached file. Which is having the jpg image and converted tif image


Hi Anish,

Thanks for your inquiry. The problem occurs because the size of image is bigger than max allowed size of MS Word page. Please use the following code snippet to achieve your requirements.

public static void ConvertImageToPdf(string inputFileName, string outputFileName)<o:p></o:p>


// Create Aspose.Words.Document and DocumentBuilder.<o:p></o:p>

// The builder makes it simple to add content to the document.<o:p></o:p>

Document doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);<o:p></o:p>

// Read the image from file, ensure it is disposed.<o:p></o:p>

using (Image image = Image.FromFile(inputFileName))<o:p></o:p>


// Get the number of frames in the image.<o:p></o:p>

int framesCount = image.GetFrameCount(FrameDimension.Page);<o:p></o:p>

// Loop through all frames.<o:p></o:p>

for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)<o:p></o:p>


// Insert a section break before each new page, in case of a multi-frame TIFF.<o:p></o:p>

if (frameIdx != 0)<o:p></o:p>


// Select active frame.<o:p></o:p>

image.SelectActiveFrame(FrameDimension.Page, frameIdx);<o:p></o:p>

// Max page size<o:p></o:p>

const double maxPageHeight = 1584;<o:p></o:p>

const double maxPageWidth = 1584;<o:p></o:p>

double currentImageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);<o:p></o:p>

double currentImageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);<o:p></o:p>

if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight)<o:p></o:p>


// Get max image size.<o:p></o:p>

CalculateImageSize(image, maxPageHeight, maxPageWidth, out currentImageHeight, out currentImageWidth);<o:p></o:p>


// We want the size of the page to be the same as the size of the image.<o:p></o:p>

// Convert pixels to points to size the page to the actual image size.<o:p></o:p>

PageSetup ps = builder.PageSetup;<o:p></o:p>

ps.PageWidth = currentImageWidth;<o:p></o:p>

ps.PageHeight = currentImageHeight;<o:p></o:p>

// Insert the image into the document and position it at the top left corner of the page.<o:p></o:p>












// Save the document to PDF.<o:p></o:p>



/// Calculates size of Image<o:p></o:p>

/// <o:p></o:p>

/// Original image<o:p></o:p>

/// Height of container where image should be inserted.<o:p></o:p>

/// Width of container where image should be inserted.<o:p></o:p>

/// Height of the image<o:p></o:p>

/// Width of the image<o:p></o:p>

public static void CalculateImageSize(Image img, double containerHeight, double containerWidth, out double targetHeight, out double targetWidth)<o:p></o:p>


//Calculate width and height<o:p></o:p>

targetHeight = containerHeight;<o:p></o:p>

targetWidth = containerWidth;<o:p></o:p>

//Get size of an image<o:p></o:p>

double imgHeight = ConvertUtil.PixelToPoint(img.Height);<o:p></o:p>

double imgWidth = ConvertUtil.PixelToPoint(img.Width);<o:p></o:p>

if (imgHeight < targetHeight && imgWidth < targetWidth)<o:p></o:p>


targetHeight = imgHeight;<o:p></o:p>

targetWidth = imgWidth;<o:p></o:p>




//Calculate size of an image in the document<o:p></o:p>

double ratioWidth = imgWidth / targetWidth;<o:p></o:p>

double ratioHeight = imgHeight / targetHeight;<o:p></o:p>

if (ratioWidth > ratioHeight)<o:p></o:p>

targetHeight = (targetHeight * (ratioHeight / ratioWidth));<o:p></o:p>


targetWidth = (targetWidth * (ratioWidth / ratioHeight));<o:p></o:p>



Hi Tahir
Still the pdf document generate from JPG is showing blank only.
I include all the above mentioned code in my application.

Can you please convert the JPG file which is already attached in my previous post and try to convert to PDF.


Hi Anish,

Thanks for your inquiry. Please find the output Pdf file in attachment. Please share the following detail for investigation purpose.
  • OS (Windows Version or Linux Version)
  • Architecture (32 / 64 bit)
  • .NET Framework version

ConvertImageToPdf(MyDir + “in.jpg”,
MyDir + “out.pdf”);<o:p></o:p>

Hi Tahir,
Please find the details.

OS - Windows XP
Architecture - 32 bit
.NET Framework version - 4.0


Hi Anish,

Thanks for sharing the detail. I have tested the scenario at Windows XP (32 bit), .NET Framework 4.0 and have not found the shared issue.

Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v13.5.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v13.5.0 and let us know how it goes on your side. I hope, this will help. I have attached the output file generated at Windows XP with this post for your kind reference.

Hi Tahir,

I am using the latest version of Aspose.Words.
Please see the attached screenshot.
But still the generated PDF is blank. But it having some size.
Do i need to do something else?


Hi Anish,

Thanks for your inquiry. Are you using the same image file which you shared in this post? If you are using some different image, please share it here along with output Pdf file for testing purposes.

It would be great if you please test this scenario by creating a separate test application with the code shared here:

Hi Tahir,
Thanks for your help.

I am able to convert the file.

I didn’t added a a line of code . So it came as blank


Hi Anish,

Thanks for your feedback. It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.