Programmatically Configure Excel Page Layout and Print Settings in C#

When generating Excel reports programmatically, ensuring consistent and professional print formatting is often overlooked—yet critical for enterprise-grade outputs. This guide demonstrates how to precisely control page layout, margins, scaling, and print regions in C# using the Free Spire.XLS library, enabling fully automated, repeatable document generation.

Installation:
Install the Free Spire.XLS NuGet package via Visual Studio’s Package Manager. No additional setup is required—reference the assembly and begin configuring.

Core Object: PageSetup

All page layout settings in Free Spire.XLS are managed through the PageSetup class, which exposes properties for margins, orientation, paper size, scaling, headers/footers, print areas, and page breaks. The workflow follows a straightforward pattern:

  1. Instantiate a Workbook.
  2. Load a existing Excel file using LoadFromFile().
  3. Access the target worksheet via Workbook.Worksheets[index].
  4. Retrieve its PageSetup instance.
  5. Modify properties as needed.
  6. Persist changes with SaveToFile().

Setting Page Margins

Margins define the blank space around printed content. Free Spire.XLS uses inches as the default unit.

using Spire.Xls;

var workbook = new Workbook();
workbook.LoadFromFile("input.xlsx");
var sheet = workbook.Worksheets[0];
var pageSetup = sheet.PageSetup;

pageSetup.TopMargin = 1.0;
pageSetup.BottomMargin = 1.0;
pageSetup.LeftMargin = 0.75;
pageSetup.RightMargin = 0.75;
pageSetup.HeaderMarginInch = 0.5;
pageSetup.FooterMarginInch = 0.5;

workbook.SaveToFile("output_with_margins.xlsx", ExcelVersion.Version2016);
workbook.Dispose();

To convert centimeters to inches:
inches = centimeters / 2.54


Page Orientation and Paper Size

Choose between portrait and landscape based on data structure.

pageSetup.Orientation = PageOrientationType.Landscape; // Wide tables
// pageSetup.Orientation = PageOrientationType.Portrait; // Tall tables

For multi-sheet workbooks, enable individual sheet settings to avoid global overrides:

workbook.ConverterSetting.PrintWithSheetPageSetting = true;

Set paper size using standard enums:

pageSetup.PaperSize = PaperSizeType.PaperA4; // A4
// pageSetup.PaperSize = PaperSizeType.PaperLetter; // US Letter
// pageSetup.PaperSize = PaperSizeType.PaperA3; // A3


Scaling Content to Fit Pages

Two methods control how content scales during printing:

Fixed Zoom Ratio

Scale content to a specific percentage (valid range: 10–400):

pageSetup.Zoom = 85; // Print at 85% of original size

Fit to Number of Pages

Fit content to a fixed number of pages—useful for avoiding multi-page outputs:

// Fit all columns into 1 page wide, any number of pages tall
pageSetup.FitToPagesWide = 1;
pageSetup.FitToPagesTall = 0;

// Fit entire content into exactly one page
// pageSetup.FitToPagesWide = 1;
// pageSetup.FitToPagesTall = 1;

⚠️ Important: Zoom and FitToPages are mutually exclusive. Setting FitToPagesWide or FitToPagesTall overrides Zoom.


Defining Print Ranges and Repeating Headers

Print Area

Limit printing to a specific cell range:

pageSetup.PrintArea = "B3:K80"; // Only print data in this range

Repeat Rows and Columns

Ensure headers appear on every printed page:

// Repeat first two rows on every page
pageSetup.PrintTitleRows = "$1:$2";

// Repeat column A on every page
// pageSetup.PrintTitleColumns = "$A:$A";

// Combine with print area
pageSetup.PrintArea = "A1:F120";
pageSetup.PrintTitleRows = "$1:$1"; // Only row 1 as header


Controlling Page Breaks

Manually insert horizontal and vertical page breaks to control where pages split.

Horizontal Page Breaks

Insert a break above a specified row:

sheet.HPageBreaks.Add(sheet.Range["A25"]); // Break before row 25
sheet.HPageBreaks.Add(sheet.Range["A50"]); // Break before row 50

Vertical Page Breaks

Insert a break to the left of a specified column:

sheet.VPageBreaks.Add(sheet.Range["G1"]); // Break before column G

These breaks override automatic page layout and ensure content is split exactly where intended.

Tags: FreeSpire.XLS C# Excel PageSetup PrintSettings

Posted on Sat, 19 Sep 2026 16:00:33 +0000 by willeadie