Printing in WPF Spreadsheet (SfSpreadsheet)

28 Jul 20211 minute to read

SfSpreadsheet control allows you to print the data in the workbook with the help of PDF Conversion. To provide the printing support in SfSpreadsheet, you need to convert the workbook into PDF document using ExcelToPdfConverter.

For Conversion of Excel Workbook in SfSpreadsheet to PDF document, use Convert method of ExcelToPdfConverter.

For viewing the PDF document, you can use PdfViewerControl to load the saved PDF stream.

//Create the pdf viewer for load the document.
PdfViewerControl pdfViewer = new PdfViewerControl();

//Create Memory Stream to save pdf document
MemoryStream pdfStream = new MemoryStream();
ExcelToPdfConverter converter = new ExcelToPdfConverter (spreadsheet.Workbook);  

//Initialize the ExcelToPdfConverter Settings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings(); 
settings.LayoutOptions = LayoutOptions.NoScaling;

For print preview you can load the PDF stream into viewer and for direct printing use Print method in PdfViewerControl which is available under the namespace “Syncfusion.PdfViewer.Wpf”

//Initialize the PdfDocument
PdfDocument pdfDoc = new PdfDocument ();

//Assign the PdfDocument to the templateDocument property of ExcelToPdfConverterSettings  
settings.TemplateDocument = pdfDoc;
settings.DisplayGridLines = GridLinesDisplayStyle.Invisible;

//Convert Excel Document into PDF document
pdfDoc = converter.Convert(settings);

//Save the PDF file     
pdfDoc.Save(pdfStream);

//Load the document to pdf viewer
pdfViewer.Load(pdfStream);

//Print the doc
pdfViewer.Print(true);

NOTE

You can refer to our WPF Spreadsheet feature tour page for its groundbreaking feature representations. You can also explore our WPF Spreadsheet example to know how to render and configure the spreadsheet.