Printing Contents in WPF DOCX Editor

18 Aug 20263 minutes to read

The WPF RichTextBox (SfRichTextBoxAdv) supports an API to print the rich text content as pages using the print dialog through the PrintDocument method.

Print

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()

PrintCompleted event

The SfRichTextBoxAdv also supports the PrintCompleted event that is raised when the printing operation completes. The PrintCompletedEventArgs provides information about the print operation. The following code example demonstrates how to handle 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 the PrintDocumentCommand UI command to invoke printing in SfRichTextBoxAdv.

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

Hide comments when printing

In the SfRichTextBoxAdv control, comments are shown by default when printing the document (the default value of PrintComments is true). You can hide the comments while printing by setting the PrintComments property of the EditorSettings class to false.

The following code example illustrates how to hide the comments when 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 know how to render and configure the editing tool.

See also