Auto Paging

When a worksheet is to be printed, ReoGrid performs paging automatically. The paging operation scans the worksheet and determines the print range for each page according to the paging settings. For example, the following image shows a worksheet that has been paged vertically.

108

Like Excel, ReoGrid finds the maximum number of cells that will fit on each page. Any cells that cannot be printed entirely are moved to the next page, as shown below:

109

Perform auto paging or reset page breaks

To perform auto paging or reset the current page breaks at any time:

sheet.AutoSplitPage();

::info This method itself runs very quickly (< 10 ms), but it may be slower if a remote printer is set as the default printer, as fetching the paper size from a remote printer can take more than 100 ms (depending on the network environment). ::

Show page break lines

To show page break lines on screen:

sheet.EnableSettings(WorksheetSettings.View_ShowPageBreaks);

Result: 120

Auto-check printable boundary

If no printable range is specified, ReoGrid will automatically find the used range of cells and set it as the printable range.

121

Change printable range

Use the PrintableRange property of the worksheet to change the printable range.

sheet.PrintableRange = new RangePosition(1, 1, 9, 9);

122

If the printable range is larger than the paper size, it will be split automatically. Cells outside the printable range will not be printed.

Change paging order

Use the PrintSettings.PageOrder property to change the paging direction.

112

sheet.PrintSettings.PageOrder = PrintPageOrder.DownThenOver;

Change page settings

Use the PrintSettings property of the worksheet to change paper settings.

// change page to landscape
sheet.PrintSettings.Landscape = true;

// set page margins
sheet.PrintSettings.Margins = new PageMargins(5);

Example: Auto paging to landscape

123

You can set the print scaling using the PageScaling property of PrintSettings.

sheet.PrintSettings.PageScaling = 0.7f;    // scale to 70%

::info When page scaling is changed, the worksheet will be re-paged automatically. ::

Insert a page break

To insert a page break, use the RowPageBreaks and ColumnPageBreaks properties of the worksheet:

// insert page break
sheet.ColumnPageBreaks.Add(5);

Spreadsheet is split into two pages:

124

User page breaks and system page breaks

There are two types of page breaks: solid lines are user page breaks, generated by the AutoSplitPage method or user operations; dashed lines are system page breaks, inserted automatically by ReoGrid to split the worksheet into multiple pages.

125

Change page breaks

To change or move a page break index (whether a user or system page break), use the following methods:

void sheet.ChangeColumnPageBreak(int oldIndex, int newIndex);
void sheet.ChangeRowPageBreak(int oldIndex, int newIndex);

Once a page break is changed, it becomes a user page break and will no longer be updated automatically by ReoGrid.

Iterate pages

The IteratePrintPages method of the worksheet is used to iterate over all print pages. This method returns the range position for each page:

sheet.IteratePrintPages( range => {
  Console.WriteLine("Page range is: " + range.ToAddress());
  return true;
});

The output is printed as follows:

Page range is: A1:E11
Page range is: F1:K11

It is also possible to iterate pages in a specified order:

sheet.IteratePrintPages(PrintPageOrder.OverThenDown, range => { ... });

If no paging order is specified, the page order from the print settings will be used.

Disable end-user changing/adjusting the page breaks

To prevent the end-user from changing the page break index with the mouse, set the worksheet setting:

sheet.SetSettings(WorksheetSettings.Behavior_AllowUserChangingPageBreaks, false);

A print session is used to print an entire workbook or specified worksheets. To print an entire workbook or multiple worksheets, create a print session from the workbook instance (the control). To print a single worksheet, create a print session from that worksheet.

263

var session = worksheet.CreatePrintSession();
session.Print();
var session = reoGridControl.CreatePrintSession();
session.Print();
var session = reoGridControl.CreatePrintSession();
session.Worksheets.Clear();
session.Worksheets.Add(grid.Worksheets["Sheet1"]);
session.Worksheets.Add(grid.Worksheets["Sheet3"]);
session.Print();

The following code shows a standard .NET print preview dialog:

var sheet = reoGridControl.CurrentWorksheet;

using (var session = sheet.CreatePrintSession())
{
  using (PrintPreviewDialog ppd = new PrintPreviewDialog())
  {
    ppd.Document = session.PrintDocument;
    ppd.SetBounds(200, 200, 1024, 768);
    ppd.PrintPreviewControl.Zoom = 1d;
    ppd.ShowDialog(this);
  }
}

Print

The following code prints the worksheet using the default print settings.

var session = sheet.CreatePrintSession();
session.Print();

The following code shows a standard print dialog and then prints the specified worksheet.

System.Drawing.Printing.PrintDocument doc = null;

try
{
  doc = worksheet.CreatePrintSession().PrintDocument;
}
catch (Exception ex)
{
  MessageBox.Show(this, ex.Message, this.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
  return;
}

using (var pd = new System.Windows.Forms.PrintDialog())
{
  pd.Document = doc;
  pd.UseEXDialog = true;  // in 64bit Windows

  if (pd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
    doc.PrinterSettings = pd.PrinterSettings;
    doc.Print();
  }
}

if (doc != null) doc.Dispose();

Show system page settings dialog

using (PageSetupDialog psd = new PageSetupDialog())
{
  // transform ReoGrid page settings to System page settings
  psd.PageSettings = worksheet.PrintSettings.CreateSystemPageSettings();

  psd.AllowMargins = true;
  psd.AllowPrinter = true;
  psd.AllowPaper = true;
  psd.EnableMetric = true;

  if (psd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
    // transform System page settings to ReoGrid page settings
    worksheet.PrintSettings.ApplySystemPageSettings(psd.PageSettings);
  }
}

Select printer programmatically

Use PrintSettings.PrinterName to send output directly to a specific printer without showing a dialog. Set it to null (the default) to use the system default printer.

sheet.PrintSettings.PrinterName = "Microsoft Print to PDF";

var session = sheet.CreatePrintSession();
session.Print();

By default, grid lines are not printed. Set ShowGridLines to true to include them in the printed output.

sheet.PrintSettings.ShowGridLines = true;

Show margin indicators

When ShowMargins is true, margin boundary marks are drawn at the edges of each printed page (visible in print preview and in the printed output).

sheet.PrintSettings.ShowMargins = true;

Set paper size

Use PaperName to select from the predefined paper sizes. The string must match a key in PaperManager.PaperSizesInch.

Common values: "A3", "A4", "A5", "Letter", "Legal", "Ledger", "B4", "B5", "JIS_B4", "JIS_B5".

sheet.PrintSettings.PaperName = "A4";

When PaperName is set to a known size, PaperWidth and PaperHeight can be left at their defaults. Setting PaperName to "Custom" lets you specify the dimensions explicitly:

sheet.PrintSettings.PaperName   = "Custom";
sheet.PrintSettings.PaperWidth  = 6.0f;   // inches
sheet.PrintSettings.PaperHeight = 9.0f;   // inches

All sizes are in inches. The Landscape flag swaps width and height automatically when computing the printable area.

Available paper sizes

CategoryNames
ISO A seriesA0 – A8
ISO B seriesB0 – B10
ISO C seriesC2 – C6
JIS B seriesJIS_B0 – JIS_B12
North AmericanLetter, Legal, Ledger, Tabloid, Executive
ANSIANSI_C, ANSI_D, ANSI_E
OtherD0, SRA0 – SRA4, RA0 – RA2, Custom

Set page margins

PageMargins stores four independent margin values in inches. Use the single-value constructor to set all four sides at once, or the four-value constructor for independent control.

// All four margins = 1 inch
sheet.PrintSettings.Margins = new PageMargins(1f);

// Top=0.5, Bottom=0.5, Left=1, Right=1 (inches)
sheet.PrintSettings.Margins = new PageMargins(0.5f, 0.5f, 1f, 1f);

// Adjust individual sides
var m = sheet.PrintSettings.Margins;
m.Left  = 0.75f;
m.Right = 0.75f;
sheet.PrintSettings.Margins = m;

// Remove all margins
sheet.PrintSettings.Margins = PageMargins.Empty;

Reset all page breaks

Call ResetAllPageBreaks() to clear all user-defined breaks and re-run auto paging from scratch.

sheet.ResetAllPageBreaks();

This is equivalent to clearing PrintableRange, removing all entries from RowPageBreaks and ColumnPageBreaks, and calling AutoSplitPage().

PrintableRange changed event

Subscribe to PrintableRangeChanged to be notified whenever the printable range changes.

sheet.PrintableRangeChanged += (s, e) =>
{
    Console.WriteLine("Printable range: " + sheet.PrintableRange.ToAddress());
};

PrintSettings property reference

PropertyTypeDefaultDescription
PrinterNamestringnullOutput printer name. null = system default.
PaperNamestring"Letter"Paper size name (key from PaperManager.PaperSizesInch).
PaperWidthfloat8.5fPaper width in inches (used when PaperName is "Custom").
PaperHeightfloat11fPaper height in inches (used when PaperName is "Custom").
Landscapeboolfalsetrue for landscape orientation.
MarginsPageMarginsnew PageMargins(1f)Page margins in inches.
PageOrderPrintPageOrderDownThenOverPage order: DownThenOver or OverThenDown.
PageScalingfloat1fScale factor (0.1 – 4.0). 1f = 100%.
ShowGridLinesboolfalsePrint cell grid lines.
ShowMarginsboolfalseDraw margin boundary marks on pages.
Was this article helpful?