Printing Contents in WPF RichTextBox (SfRichTextBoxAdv)

29 Mar 20232 minutes to read

The SfRichTextBoxAdv supports API to print the rich text content rendered as pages using the print dialog.
The following sample code demonstrates how to print the document content as pages.

// Displays the Print Dialog to perform printing of document content as pages.
richTextBoxAdv.PrintDocument();
' Displays the Print Dialog to perform printing of document content as pages.
richTextBoxAdv.PrintDocument()

The SfRichTextBoxAdv also supports event to notify whenever the printing operation is completed. The following code example demonstrates how to handle for the print completed event.

// Hooks the print completed event.
richTextBoxAdv.PrintCompleted += RichTextBoxAdv_PrintCompleted;

// Called whenever the print completed event is fired.
private void RichTextBoxAdv_PrintCompleted(object obj, PrintCompletedEventArgs args)
{
    // Handle your code here.
}

// Unhooks the print completed event.
richTextBoxAdv.PrintCompleted -= RichTextBoxAdv_PrintCompleted;
' Hooks the print completed event.
AddHandler richTextBoxAdv.PrintCompleted, AddressOf RichTextBoxAdv_PrintCompleted

' Called whenever the print completed event is fired.
Private Sub RichTextBoxAdv_PrintCompleted(obj As Object, args As PrintCompletedEventArgs)
	' Handle your code here.
End Sub

' Unhooks the print completed event.
RemoveHandler richTextBoxAdv.PrintCompleted, AddressOf RichTextBoxAdv_PrintCompleted

UI Command for printing

The following code example demonstrates how to bind UI Command to invoke printing in SfRichTextBoxAdv.

<!-- Binds button to the PrintDocumentCommand -->
<Button Content="Print" Command="RichTextBoxAdv:SfRichTextBoxAdv.PrintDocumentCommand" CommandTarget="{Binding ElementName=richTextBoxAdv}" />

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

NOTE

In order to invoke printing, the standard keyboard shortcut CTRL + P can also be used.
You can refer to our WPF RichTextBox feature tour page for its groundbreaking feature representations.You can also explore our WPF RichTextBox example to knows how to render and configure the editing tools.