Setting Page Margins


#1

Hi,
I’m using Asponse.Excel .NET component. Using this component I had to generated some excel reports. The problem that I encountered relates to the PageSetup Excel class. There are properties in this class: LeftMargin, RightMargin, TopMargin, BottomMargin, FooterMargin, HeaderMargin. Using these properties I’m trying to set programmatically their values. Here is the code that does it:

sheet.PageSetup.LeftMargin = 0.25D;
sheet.PageSetup.TopMargin = 0.25D;
sheet.PageSetup.RightMargin = 0.25D;
sheet.PageSetup.BottomMargin = 0.5D;
sheet.PageSetup.FitToPagesWide = 1;
sheet.PageSetup.FitToPagesTall = 0;
sheet.PageSetup.FooterMargin = 0.25D;
sheet.PageSetup.HeaderMargin = 0.25D;

When my application finishes running I have excel files, but the page setup doesn’t reflect my values for the margins. Maybe this happens because I’m using FitToPagesWide and FitToPagesTall properties of the same class. My Aspose.Excel.dll Assembly version is: 1.8.8.1
Could you please explain why this happen?
Thanks


#2

Hi carbon,

I used the following code to test this issue.

Excel excel = new Excel();
Worksheet sheet = excel.Worksheets[0];
sheet.PageSetup.LeftMargin = 0.25D;
sheet.PageSetup.TopMargin = 0.25D;
sheet.PageSetup.RightMargin = 0.25D;
sheet.PageSetup.BottomMargin = 0.5D;
sheet.PageSetup.FitToPagesWide = 1;
sheet.PageSetup.FitToPagesTall = 0;
sheet.PageSetup.FooterMargin = 0.25D;
sheet.PageSetup.HeaderMargin = 0.25D;

excel.Save(“abc.xls”, SaveType.OpenInExcel, FileFormatType.Default, this.Response);

It worked fine.

Do you use a designer file? And could you post more code? If possible, could you send me your project?


#3

Hi, I sent the result excel file and my project to excel@aspose.com , please check it out

Carbon


#4

Hi Carbon,

In your file, margins of top, left, right, header, footer are all 0.2 and bottom margin is 0.5, right?
If you mannually set margin to 0.25 in MS Excel, margin will be changed to 0.3 automatically. So you can set margins to 0.3 directly.


#5

Hi Laurence,
I think I got it. The properties for margins in excel uses doubles. I played with different double values and finally I achieved the settings that I need, in my case if I need to have 0.25 inches for a margin, the double representation in double should be 0.635D.
Maybe in future release you might want to have something similar to MS Excel ActiveX component where there is method InchesToPoints(double d) in Application class.
Thanks for your assistance.


#6

Hi Carbon,

Thanks for your advice. I will think of this issue.


#7

I think I have found what the problem is. I did a simple test. I put 1 in all the margins, and when I opened my workbook, all the values got changed to:0.3937. I then divided 1 by this the result to see what the ratio was, and got: 2.54. Which is the Inch to Centimeter ratio. Which leads me to beleive that Aspose.Excel is somehow storeing the value entered in centimeters, and when we open the file in our local config, Excel is translating the value to inches by dividing by 2.54.


#8

The margin unit is centimeter. When you install Aspose.Excel, you can find correct api description in the chm. However, in our web site, those descriptions are not updated.

I will fix it. Thank you for the report.