In this blog post, we’ll review the extensive printing options available in the DevExpress Spreadsheet Document API and our Spreadsheet UI component for WinForms and WPF.
DevExpress Spreadsheet components support a broad range of print options – options you can apply to worksheets within your workbook. Our Spreadsheet API and our Spreadsheet UI components allow you to execute the following print-related actions with ease:
- Specify paper size (Letter, Legal, Tabloid, etc.).
- Define page margins.
- Change page orientation.
- Scale a printout to fit a specific number of pages.
- Specify cell range for a print operation (print area).
- Add headers and footers to a printout.
- Repeat specific rows or columns on every printed page.
- Insert manual page breaks.
- Print row and column headings.
- Print worksheet gridlines.
To specify print settings within the WinForms or WPF Spreadsheet control, use ribbon commands on the Page Layout tab or invoke the Page Setup dialog.
Once you configure desired print settings, you can switch to the File tab to print the document or invoke the Print Preview window.
Our WinForms and WPF Spreadsheet controls ship with the SpreadsheetControl.Options.Print.PrintContent
property. This property allows you to specify spreadsheet content to print/display within the Print Preview window. The following values are available:
- Default / EntireWorkbook - prints the entire workbook.
- ActiveSheets - prints the active sheet and all selected sheets (if any). To print multiple sheets, hold Ctrl when clicking sheet tabs in the component.
- Selection - prints selected cells for the active worksheet.
// Specify that the Spreadsheet control should print selected sheets.
spreadsheetControl.Options.Print.PrintContent =
DevExpress.XtraSpreadsheet.SpreadsheetPrintContent.ActiveSheets;
This option is also available within the new Settings pane that was added to the Print Preview window in our most recent major release (v21.2). This pane allows you to select what to print, set the number of copies to print, and specify page settings (such as document orientation, paper size, page margins, and scaling options). Once print settings are changed, the Print Preview window updates the document’s layout accordingly.
As you may already know, the Print Preview window also allows you to save your workbooks as PDF or image files. In v21.2, we extended our PDF export engine so you can now convert your spreadsheet documents to accessible PDF files. Feel free to review the following blog post to learn more about PDF accessibility support within our Office-inspired products:
Office File API & Office-Inspired Desktop UI Controls - Generate Accessible PDF Files (v21.2)
Print Documents in Code
You can also use our full-featured Spreadsheet API to specify print settings and print documents in code. We created a simple example that defines print options for the active worksheet. Please note that this example uses a new Worksheet.ActiveView.SetCustomPaperSize method (implemented in v21.2) to define custom paper size.
using DevExpress.Spreadsheet;
// …
// Access active worksheet in your document.
var worksheet = workbook.Worksheets.ActiveWorksheet;
// Specify measurement unit for the workbook.
workbook.Unit = DevExpress.Office.DocumentUnit.Inch;
// Specify page margins.
var pageMargins = worksheet.ActiveView.Margins;
pageMargins.Top = 0.7f;
pageMargins.Bottom = 0.7f;
// Add headers and footers to printout.
var headerFooterOptions = worksheet.HeaderFooterOptions;
// Insert sheet name into the header's left section.
headerFooterOptions.OddHeader.Left = "&A";
// Insert page number into the footer's left section.
headerFooterOptions.OddFooter.Left = "Page &P of &N";
// Change page orientation.
worksheet.ActiveView.Orientation = PageOrientation.Landscape;
// Set custom paper size (10 inches by 11 inches).
worksheet.ActiveView.SetCustomPaperSize(10, 11);
The following image illustrates the resulting output:
Use the appropriate Print method (for the Spreadsheet control or a non-visual Workbook instance) to print your document in code. If you need to print individual worksheets, you can call the Sheet.Print method for each worksheet or pass necessary sheet names to the Workbook.Print method (only available for the Spreadsheet Document API). Refer to the following help topics for additional information and examples:
- Printing in the Spreadsheet Document API
- Printing in the WinForms Spreadsheet
- Printing in the WPF Spreadsheet
Your Feedback Matters
Should you have any questions/feedback regarding printing support for our Spreadsheet Document API or WinForms/WPF Spreadsheet component, please comment below or submit a support ticket via the DevExpress Support Center. We will be happy to follow-up.