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

Free Support Forum - aspose.com

Determine Height Width of Word Page and Resize Image to Fit the Page using C# .NET

I am moving the documentbuilder to a bookmark and then doing an insertImage to place an image. Now how do I make the image fit the page. I saw other posts on how to fit based on a table cell, but that does not work here.

thanks for your help.



<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. You can calculate size of the image. For example, you can try using the following code:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//Read image from file to Image object

Image img = Image.FromFile(@"Common\large.jpg");

double targetHeight;

double targetWidth;

CalculateImageSize(builder, img, out targetHeight, out targetWidth);

//Add the image to the document and set it's position and size

Shape shp = builder.InsertImage(img, targetWidth, targetHeight);

shp.WrapType = WrapType.Inline;

shp.BehindText = true;

//Save document




/// Calculates size of Image


/// DocumentBuilder is used to determine Height and Width of current Page

/// Original image

/// Height of the image

/// Width of the image

private void CalculateImageSize(DocumentBuilder builder, Image img, out double targetHeight, out double targetWidth)


//Calculate width and height of the page

PageSetup ps = builder.CurrentSection.PageSetup;

targetHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;

targetWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

//Get size of an image

double imgHeight = ConvertUtil.PixelToPoint(img.Height);

double imgWidth = ConvertUtil.PixelToPoint(img.Width);

if (imgHeight < targetHeight && imgWidth < targetWidth)


targetHeight = imgHeight;

targetWidth = imgWidth;




//Calculate size of an image in the document

double ratioWidth = imgWidth / targetWidth;

double ratioHeight = imgHeight / targetHeight;

if (ratioWidth > ratioHeight)

targetHeight = (targetHeight * (ratioHeight / ratioWidth));


targetHeight = (targetWidth * (ratioWidth / ratioHeight));



Hope this could help you.

Best regards.

this is exactly what I was looking for. your support is fantastic for the product. I will definitely promote your product out there.

thanks again.


The code you provided worked, but only if the image wasn’t larger than the page itself.

Here is updated code that handles larger images.

Dim ps As PageSetup = builder.CurrentSection.PageSetup
Dim targetHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin - ps.FooterDistance
Dim targetWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin
   <span style="color:#57a64a;">'Get size of an image</span>
   <span style="color:#569cd6;">Dim</span> imgHeight <span style="color:#569cd6;">As</span> <span style="color:#569cd6;">Double</span> = <span style="color:#4ec9b0;">ConvertUtil</span>.PixelToPoint(img.Height)
   <span style="color:#569cd6;">Dim</span> imgWidth <span style="color:#569cd6;">As</span> <span style="color:#569cd6;">Double</span> = <span style="color:#4ec9b0;">ConvertUtil</span>.PixelToPoint(img.Width)

   <span style="color:#569cd6;">If</span> imgHeight < targetHeight <span style="color:#569cd6;">AndAlso</span> imgWidth < targetWidth <span style="color:#569cd6;">Then</span>
       targetHeight = imgHeight
       targetWidth = imgWidth
   <span style="color:#569cd6;">Else</span>
       <span style="color:#57a64a;">'Calculate size of an image in the document</span>
       <span style="color:#569cd6;">Dim</span> ratioX = targetWidth / imgWidth
       <span style="color:#569cd6;">Dim</span> ratioY = targetHeight / imgHeight
       <span style="color:#569cd6;">Dim</span> ratio = System.<span style="color:#4ec9b0;">Math</span>.Min(ratioX, ratioY)

       targetWidth = <span style="color:#569cd6;">CInt</span>(imgWidth * ratio)
       targetHeight = <span style="color:#569cd6;">CInt</span>(imgHeight * ratio)
   <span style="color:#569cd6;">End</span> <span style="color:#569cd6;">If</span></pre></div>

Hi Brandon,

Thanks for the additional information. This will definitely help other developers looking to achieve the same.

Best regards,

A post was split to a new topic: Resizing an image to fit the page in Word document