PageSetup - Read Only. Margin settings are lost

First question, why are so many properties Read only?



I would like to pass my PageSetup settings from Worksheet to Worksheet and do only limited changes depending on the number of Columns.



Worksheet.PageSetup = myPageSetup is not possible but would be great.



Second Question/Problem:

I have assigned the following settings to the current Worksheet:



'Specify Top, Left, Right & Bottom Margin

.TopMargin = 0.62992125984252

.LeftMargin = 0.393700787401575

.RightMargin = 0.393700787401575

.BottomMargin = 0.511811023622047

'Specify Header / Footer margins

.HeaderMargin = 0.31496062992126

.FooterMargin = 0.31496062992126



then I save the WB but when I open it, the Margins look like this:



'Specify Top, Left, Right & Bottom Margin

.TopMargin = 0.248000496000992

.LeftMargin = 0.15500031000062

.RightMargin = 0.15500031000062

.BottomMargin = 0.201500403000806

'Specify Header / Footer margins

.HeaderMargin = 0.124000248000496

.FooterMargin = 0.124000248000496



All other settings like:

.Zoom = 75

.PaperSize = PaperSizeType.PaperA4

.PrintQuality = 600



Are reflected correctly. Is there an Inch conversion?

I just double checked it:



.TopMarginInch = 0.62992125984252

.LeftMarginInch = 0.393700787401575

.RightMarginInch = 0.393700787401575

.BottomMarginInch = 0.511811023622047

'Specify Header / Footer margins

.HeaderMarginInch = 0.31496062992126

.FooterMarginInch = 0.31496062992126



Is not recalculated and is reflected with the wanted settings. Nevertheless there seems to be a bug with the margin settings.

Hi,


Thanks for providing us details about your queries.

1) Which PageSetup properties you are talking about? Well, you may pass one worksheet’s PageSetup settings to another worksheet but you will specify a worksheet’s individual/each PageSetup attributes (e.g LeftMargin, TopMargin, BottomMargin, RightMargin, Orientation, Zoom, PageSize, etc.) for other worksheet PageSetup attributes accordingly.

2) For your information, in MS Excel (by default), you may either set top, left, right, bottom margins etc. for the worksheet in centimeters or Inches for Page Setup options. MS Excel only allows you to set the value upto 1 decimal place even if you have specified a value with more than 1 decimal places (manually or by code), MS Excel will round it to 1 decimal place. I have tested the following sample code with a template Excel file, it works find and as expected (see the comments attached to the lines of code).
e.g
Sample code:

Workbook workbook = new Workbook(“e:\test2\Book1.xlsx”);

PageSetup pageSetup = workbook.Worksheets[0].PageSetup;
pageSetup.Orientation = PageOrientationType.Landscape;
pageSetup.Zoom = 75;
pageSetup.PaperSize = PaperSizeType.PaperA4;
pageSetup.PrintQuality = 600;


//Set the margins in centimeters.
pageSetup.TopMargin = 0.635D; //It will set 0.6
pageSetup.BottomMargin = 1.905D;//It will set 1.9
pageSetup.LeftMargin = 0.635D;//It will set 0.6
pageSetup.RightMargin = 0.635D;//It will set 0.6

workbook.Save(“e:\test2\out1.xlsx”);

I have tested the above code with our latest version/fix: Aspose.Cells for .NET v8.7.1 (please try it if you are not already using it) and get the desired results.

Moreover, if you need to set the margins in Inches, Aspose.Cells does provide the similar margin attributes for Inches as well, please use the similar attributes ending with “Inch”, e.g TopMarginInch, BottomMarginInch, LeftMarginInch, RightMarginInch etc.

Thank you.