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.
-Dileepa
Hi
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
doc.Save(@"Test001\out.doc");
=======================================================================
///
/// 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;
}
else
{
// Calculate size of an image in the document
double ratioWidth = imgWidth / targetWidth;
double ratioHeight = imgHeight / targetHeight;
if (ratioWidth > ratioHeight)
targetHeight = (targetHeight * (ratioHeight / ratioWidth));
else
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.
-Dileepa
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
'Get size of an image
Dim imgHeight As Double = ConvertUtil.PixelToPoint(img.Height)
Dim imgWidth As Double = ConvertUtil.PixelToPoint(img.Width)
If imgHeight < targetHeight AndAlso imgWidth < targetWidth Then
targetHeight = imgHeight
targetWidth = imgWidth
Else
'Calculate size of an image in the document
Dim ratioX = targetWidth / imgWidth
Dim ratioY = targetHeight / imgHeight
Dim ratio = System.Math.Min(ratioX, ratioY)
targetWidth = CInt(imgWidth * ratio)
targetHeight = CInt(imgHeight * ratio)
End If
Hi Brandon,
Thanks for the additional information. This will definitely help other developers looking to achieve the same.
Best regards,