V4 V5

Pagination is computed by a UI-independent engine (unvell.ReoGrid.Core.Printing) shared by PDF, WinForms, and WPF. Every output target produces the same result.

Page setup

using unvell.ReoGrid.Core.Printing;

ws.PrintSettings = new PrintSettings
{
	Paper = PaperKind.A4,
	Orientation = PageOrientation.Portrait,
	MarginLeft = 54,          // points (72 pt = 1 inch)
	MarginRight = 54,
	MarginTop = 54,
	MarginBottom = 54,
	PrintArea = RangePosition.Parse("A1:H60"),
	Scale = 1.0,
	CenterHorizontally = true,
	Order = PageOrder.DownThenOver,
	ShowGridLines = true,
	ShowHeaders = false,
};

Paper sizes

PaperKind is one of A5 / A4 / A3 / B5 / B4 / Letter / Legal / Tabloid / Executive / Custom.

For Custom, specify CustomWidthPt / CustomHeightPt in points.

Scale and fit-to-page

ws.PrintSettings = new PrintSettings
{
	FitToPagesWide = 1,       // fit to one page wide
	FitToPagesTall = null,    // as many pages tall as it takes
};

bool fit = ws.PrintSettings.IsFitToPage;

Scale ranges from 10% to 400% (MinScale / MaxScale). If either FitToPagesWide or FitToPagesTall is set, it takes precedence over Scale.

The on-screen zoom factor (control.Zoom) has no effect on printing.

Pagination rules

  • Rows and columns are never split. Just like Excel, a row that does not fit is pushed to the next page
  • Hidden rows and columns are treated as zero width/height
  • PageOrder is the order in which pages are emitted (DownThenOver = down first, OverThenDown = across first)

Getting the page count up front

// when you only need the page count (no rendering involved)
PrintLayout layout = Paginator.Paginate(ws, ws.PrintSettings);
Console.WriteLine($"{layout.Pages.Count}  pages");

Since no rendering is involved, this works for preview UIs and batch processing.

Calls per output target

Output targetCall
PDFPdfExporter.Export(ws, path, settings)
WinFormscontrol.CreatePrintDocument(settings)PrintDocument.Print()
WPFcontrol.Print(settings)
AvaloniaNot supported (use PDF export)

The WinForms version returns a PrintDocument, so you can pass it straight to the standard print dialog or print preview.

Repeat the heading rows at the top of every page (Excel’s “print titles”).

// Repeat rows 1-2 at the top of every page, and column A down the left.
ws.PrintSettings.RepeatRows = new LineSpan(0, 1);
ws.PrintSettings.RepeatColumns = new LineSpan(0, 0);

LineSpan is an inclusive, 0-based band (new LineSpan(0, 1) = rows 1-2).

A repeated row or column is left out of the body, so it never prints twice on the same page. A span that would cover the whole print range is ignored — pages of nothing but titles help no one.

var hf = ws.PrintSettings.HeaderFooter;
hf.Header.Center = "&A";                 // sheet name
hf.Footer.Right = "&P / &N";             // 2 / 5
hf.Footer.Left = "&D &T";                // date and time
hf.FontSizePt = 9;

// A different banner on page 1 only.
hf.DifferentFirst = true;
hf.FirstHeader.Center = "Quarterly report";

// Distance from the paper edge to each band, in points.
ws.PrintSettings.MarginHeader = 21.6;
ws.PrintSettings.MarginFooter = 21.6;

Each band has a left, centre and right section and uses Excel’s format codes.

CodeExpands to
&P / &NPage number / page count (offsets like &P+2 work)
&D / &TDate / time, in the current culture
&ASheet name
&F / &ZFile name / folder
&&A literal &

Codes that style part of a band — &B, &I, &14, &"font,style" — are not drawn, because a band is rendered in one style (FontSizePt). They are kept in the stored string, so a header authored in Excel round-trips intact.

&F expands only when a file name is supplied (PdfExporter.Export(..., documentName: path), or GridPrintDocument.DocumentFileName). An unsaved workbook leaves it empty rather than printing an invented name.

Persistence

PrintSettings round-trips through both XLSX (<pageSetup>, <headerFooter> and the built-in name _xlnm.Print_Titles) and reogrid-json (the print block). Opening the file in Excel gives you the same paper, orientation, margins, repeated rows and bands.

Not supported

  • Printing only specific pages within the print area
  • Manual page breaks (rowBreaks / colBreaks)
  • Drawing the header/footer styling codes (&B and friends). They are preserved in the file.

Freeze panes are a screen-display feature and have no effect on printing (Freeze Panes).

Was this article helpful?