Search code examples
c#excel-interoppage-setup

Why does setting the width and height of pages to print not get applied?


I have this code to set various print options:

private void ConfigureByCustomerForPrinting()
{
    _xlSheet.PageSetup.PrintArea = "A1:" + 
        GetExcelTextColumnName(
            _xlSheet.UsedRange.Columns.Count) + 
            _xlSheet.UsedRange.Rows.Count;
    _xlSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
    _xlSheet.PageSetup.FitToPagesWide = 1;
    _xlSheet.PageSetup.FitToPagesTall = 100;
    _xlSheet.PageSetup.Zoom = false;

    _xlSheet.PageSetup.LeftMargin = _xlApp.Application.InchesToPoints(0.5);
    _xlSheet.PageSetup.RightMargin = _xlApp.Application.InchesToPoints(0.5);
    _xlSheet.PageSetup.TopMargin = _xlApp.Application.InchesToPoints(0.5);
    _xlSheet.PageSetup.BottomMargin = _xlApp.Application.InchesToPoints(0.5);
    _xlSheet.PageSetup.HeaderMargin = _xlApp.Application.InchesToPoints(0.5);
    _xlSheet.PageSetup.FooterMargin = _xlApp.Application.InchesToPoints(0.5);
}

When the sheet is generated, it respects the landscape orientation value but, as can be seen below:

enter image description here

...although width (wide) and height (tall) are set to 1 and 100, respectively, that is not the radio button selected. Instead, "Adjust to - % normal size" is set and selected. Why? I reckon this is probably fine, but the user wants it set to 1 and 100.


Solution

  • You need to set Zoom property to false before sets FitToPagesWide and FitToPagesTall:

    _xlSheet.PageSetup.Zoom = false;
    _xlSheet.PageSetup.FitToPagesWide = 1;
    _xlSheet.PageSetup.FitToPagesTall = 100;
    

    From MSDN:

    If the Zoom property is True, the FitToPagesWide property is ignored.

    If the Zoom property is True, the FitToPagesTall property is ignored.