Printing Contents in UWP RichTextBox (SfRichTextBoxAdv)

29 Mar 20236 minutes to read

The SfRichTextBoxAdv supports API to retrieve each page as a bitmap image by specifying the page number. Using this API and Print manager support, you can easily achieve printing contents of SfRichTextBoxAdv page by page in Universal Windows Platform applications.
The following sample code demonstrates how to register for printing and how to implement print document event handlers.

// Initializes a list of BitmapImage to store images of pages.
List<BitmapImage> pageImages = new List<BitmapImage>();

// Initializes PrintDocument instance.
PrintDocument printDocument = new PrintDocument();
IPrintDocumentSource printDocumentSource;

// Registers for Printing
void RegisterForPrinting()
{
    // 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 retrieve each page in SfRichTextBoxAdv as bitmap image and how to invoke printing.

// 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;
}

// 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.

// 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;
}

In the SfRichTextBoxAdv control, comments will be shown by default on printing the document. You can hide the comments while printing by using the PrintComments property of EditorSettings class.

The following code example illustrates how to hide the comments on printing the document.

<RichTextBoxAdv:SfRichTextBoxAdv x:Name="richTextBoxAdv">
    <RichTextBoxAdv:SfRichTextBoxAdv.EditorSettings>
        <RichTextBoxAdv:EditorSettings PrintComments="False" />
    </RichTextBoxAdv:SfRichTextBoxAdv.EditorSettings>
</RichTextBoxAdv:SfRichTextBoxAdv>
SfRichTextBoxAdv richTextBoxAdv = new SfRichTextBoxAdv();
richTextBoxAdv.EditorSettings.PrintComments = false;
Dim richTextBoxAdv As New SfRichTextBoxAdv()
richTextBoxAdv.EditorSettings.PrintComments = False