How to print the document contents of SfRichTextBoxAdv control by retrieving document pages as Image?
The SfRichTextBoxAdv control supports printing the document contents using the PrintManager of the Universal Windows Platform.
The first step is to retrieve all the document pages as images, as shown below using Essential DocIO.
The following code example demonstrates how to retrieve each page in SfRichTextBoxAdv as a bitmap image.
C#
// Initializes a list of BitmapImage to store images of pages. List<BitmapImage> pageImages = new List<BitmapImage>(); // Gets the page images asynchronously. async Task<bool> GetPageImagesAsync() { TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>(); // Clears the page images. pageImages.Clear(); int pageCount = richTextBoxAdv.PageCount; for (int i = 0; i < pageCount; i++) { // Retrieve page in RichTextBoxAdv as BitmapImage by specifying page number. BitmapImage bitmapImage = await richTextBoxAdv.GetPageAsImageAsync(i); pageImages.Add(bitmapImage); } taskCompletionSource.SetResult(true); return await taskCompletionSource.Task; }
The second step is to print the document pages that are already retrieved as images using the PrintManager of the Universal Windows Platform.
The following sample code demonstrates how to register for printing and how to implement print document event handlers.
C#
// Initializes PrintDocument instance. PrintDocument printDocument = new PrintDocument(); IPrintDocumentSource printDocumentSource; // Registers for Printing void RegisterForPrinitng() { // Hooks print task requested event handler. PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested += PrintManager_PrintTaskRequested; // Initializes the print document source. printDocumentSource = printDocument.DocumentSource; // Hooks print document event handlers. printDocument.Paginate += PrintDocument_Paginate; printDocument.GetPreviewPage += PrintDocument_GetPreviewPage; printDocument.AddPages += PrintDocument_AddPages; } // Print Task Requested event handler private void PrintManager_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args) { PrintTask printTask = null; printTask = args.Request.CreatePrintTask("Document", sourceRequested => { // Prints Task event handler invoked when the print job is completed. printTask.Completed += PrintTask_Completed; sourceRequested.SetSource(printDocumentSource); }); } // Print Task Completed Event Handler private void PrintTask_Completed(PrintTask sender, PrintTaskCompletedEventArgs args) { pageImages.Clear(); } // Print Document Paginate event handler. private void PrintDocument_Paginate(object sender, PaginateEventArgs e) { int pageCount = richTextBoxAdv.PageCount; PrintDocument printDocument = sender as PrintDocument; // Report the number of preview pages created. printDocument.SetPreviewPageCount(pageCount, PreviewPageCountType.Intermediate); } // Print Document Get Preview Page event handler. private void PrintDocument_GetPreviewPage(object sender, GetPreviewPageEventArgs e) { PrintDocument printDocument = sender as PrintDocument; int currentPreviewPage = 0; Interlocked.Exchange(ref currentPreviewPage, e.PageNumber - 1); if (pageImages.Count >= e.PageNumber) { BitmapImage bitmap = pageImages[e.PageNumber - 1]; Image image = new Image(); image.Source = bitmap; printDocument.SetPreviewPage(e.PageNumber, image); } } // Print Document Add Pages event handler. private void PrintDocument_AddPages(object sender, AddPagesEventArgs e) { int pageCount = richTextBoxAdv.PageCount; for (int i = 0; i < pageImages.Count; i++) { Image image = new Image(); image.Source = pageImages[i]; printDocument.AddPage(image); } printDocument.AddPagesComplete(); }
The following code example demonstrates how to invoke printing.
C#
// Invokes printing asynchronously. async void InvokePrintAsync() { bool pagesRetrieved = await GetPageImagesAsync(); if (pagesRetrieved) await PrintManager.ShowPrintUIAsync(); }
The following code example demonstrates how to unregister printing and print document event handlers.
C#
// Unregisters printing. void UnRegisterPrinting() { // Unhooks the print document event handlers. if (printDocument != null) { printDocument.Paginate -= PrintDocument_Paginate; printDocument.GetPreviewPage -= PrintDocument_GetPreviewPage; printDocument.AddPages -= PrintDocument_AddPages; printDocumentSource = null; printDocument = null; } // Unhooks the print task requested event handler. PrintManager printManager = PrintManager.GetForCurrentView(); printManager.PrintTaskRequested -= PrintManager_PrintTaskRequested; }
The following sample demonstrates how to print the contents of the SfRichTextBoxAdv control.