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

Free Support Forum - aspose.com

Calculating the available height in a page

Hi,


I am using the code below to calculate the available height in a page and then resizing an inserted image to fit into that space. However, I think I am not taking the height of the header and footer for the section into account. Could you tell me if you think I am correctly the calculating the height/width for the inserted image? At the moment the images are slightly too tall for the available space.

var shape = builder.InsertImage(image);
var ps = builder.CurrentSection.PageSetup;
var pageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
var pageHeight = ps.PageHeight - ps.TopMargin - ps.BottomMargin;

var ratioWidth = shape.Width / pageWidth;
var ratioHeight = shape.Height / pageHeight;

if (ratioHeight > 1 || ratioWidth > 1)
{
if (ratioWidth > ratioHeight)
{
shape.Height = shape.Height / ratioWidth;
shape.Width = shape.Width / ratioWidth;
}
else
{
shape.Height = shape.Height / ratioHeight;
shape.Width = shape.Width / ratioHeight;
}
}
shape.BehindText = true;

Hi

Thanks for your request. I think, you should also take in account Header and Footer distances:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.pagesetup.headerdistance.html

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.pagesetup.footerdistance.html

Hope this helps.

Best regards,

Thanks. Could you let me know which of these two would be correct:


var availableHeight1 = ps.PageHeight - ps.TopMargin - ps.BottomMargin - ps.HeaderDistance - ps.FooterDistance;
var availableHeight2 = ps.PageHeight - ps.HeaderDistance - ps.FooterDistance;

Hi there,

Thanks for your inquiry.

I think you should use the code like below:

var availableHeight1 = ps.PageHeight - Math.Max(ps.TopMargin, ps.HeaderDistance) - Math.Max(ps.BottomMargin - ps.FooterDistance);

Thanks,