Editor Service

    The Editor Service is the central programmatic surface of the WPF HTML Editor. Reach it through the Editor property of WpfHtmlEditor; it exposes the IEditorService contract through the marker interface IEditorServiceBase that the WPF binding implements.

    The service covers four broad areas: layout (resizing and scroll bars), the clipboard pipeline (cut, copy, paste, including MS Word paste), command execution (undo, redo, justification, generic exec-commands), and search-and-replace including the IE-style dialog and programmatic variants.

    All members operate against the currently loaded document. Methods that depend on a focused selection (for example Cut or JustifyLeft) silently no-op when the editor has no selection or no focus. In every example below, MyEditor is a WpfHtmlEditor declared in XAML with x:Name="MyEditor".

    AdjustEditorSize Method

    Resizes the editor control so both its width and height match the natural size of the rendered content. Equivalent to calling AdjustEditorWidth followed by AdjustEditorHeight.

    Syntax
    void AdjustEditorSize();
    Example
    private void MyEditor_DocumentReady(object sender, EventArgs e)
    {
        // Auto-grow the editor to fit the loaded document.
        MyEditor.Editor.AdjustEditorSize();
    }
    Private Sub MyEditor_DocumentReady(sender As Object, e As EventArgs)
        ' Auto-grow the editor to fit the loaded document.
        MyEditor.Editor.AdjustEditorSize()
    End Sub

    AdjustEditorWidth Method

    Resets the control's width so that the horizontal scroll bar is no longer needed to reveal clipped content; the editor expands or shrinks to match the visible content width.

    Syntax
    void AdjustEditorWidth();
    Example
    private void FitWidthButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.AdjustEditorWidth();
    }
    Private Sub FitWidthButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.AdjustEditorWidth()
    End Sub

    AdjustEditorHeight Method

    Resets the control's height so that the vertical scroll bar is no longer needed; the editor expands or shrinks to match the visible content height. Pair with AdjustEditorWidth to produce an auto-sized editor.

    Syntax
    void AdjustEditorHeight();
    Example
    private void FitHeightButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.AdjustEditorHeight();
    }
    Private Sub FitHeightButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.AdjustEditorHeight()
    End Sub

    GetScrollBarSetting Method

    Returns the editor's current scroll-bar policy as a ScrollBarVisibility value (Auto, Both, Horizontal, Vertical, None, Default, or Custom).

    Syntax
    ScrollBarVisibility GetScrollBarSetting();
    Returns

    ScrollBarVisibility - the active scroll-bar policy.

    Example
    ScrollBarVisibility before = MyEditor.Editor.GetScrollBarSetting();
    
    MyEditor.Editor.SetScrollBarSetting(ScrollBarVisibility.None);
    
    PrintDocument();
    
    MyEditor.Editor.SetScrollBarSetting(before); // restore

    SetScrollBarSetting Method

    Changes the editor's scroll-bar policy. Use Custom if you supply your own overflow CSS through BodyStyle; in Custom mode the editor leaves your style untouched.

    Syntax
    void SetScrollBarSetting(ScrollBarVisibility scrollbarValue);
    Parameters
    NameTypeDescription
    scrollbarValueScrollBarVisibilityThe desired scroll-bar policy.
    Example
    // Show both scrollbars only when content overflows.
    
    MyEditor.Editor.SetScrollBarSetting(ScrollBarVisibility.Auto);

    GetScrollPosition Method

    Returns the current scroll offset of the editor viewport as a System.Drawing.Point in pixels.

    Syntax
    System.Drawing.Point GetScrollPosition();
    Returns

    System.Drawing.Point - X is the horizontal scroll offset and Y is the vertical scroll offset.

    Example
    var saved = MyEditor.Editor.GetScrollPosition();
    MyEditor.LoadHtml(updatedHtml);
    MyEditor.Editor.SetScrollPosition(saved.X, saved.Y);
    Dim saved = MyEditor.Editor.GetScrollPosition()
    MyEditor.LoadHtml(updatedHtml)
    MyEditor.Editor.SetScrollPosition(saved.X, saved.Y)

    SetScrollPosition Method

    Scrolls the editor viewport to the absolute pixel offset (x, y). Values are clamped to the valid scroll range of the current document.

    Syntax
    void SetScrollPosition(int x, int y);
    Parameters
    NameTypeDescription
    xSystem.Int32Target horizontal scroll offset in pixels.
    ySystem.Int32Target vertical scroll offset in pixels.
    Example
    // Jump back to the top of the document.
    
    MyEditor.Editor.SetScrollPosition(0, 0);

    IsMsWordContentInClipboard Method

    Returns true when the system clipboard holds content originating from Microsoft Word (detected via Word-specific clipboard formats). Use it to decide whether to offer a Paste from Word command in your UI.

    Syntax
    bool IsMsWordContentInClipboard();
    Returns

    System.Boolean - true if MS Word content is on the clipboard; otherwise false.

    Example
    PasteFromWordButton.IsEnabled =
    
        MyEditor.Editor.IsMsWordContentInClipboard();

    PasteFromMsWord Method

    Pastes clipboard content using the editor's Word-aware cleaning pipeline. Word-specific markup such as MSO conditional comments, mso-* CSS, and VML imagedata is stripped before the content is inserted.

    Syntax
    void PasteFromMsWord();
    Example
    if (MyEditor.Editor.IsMsWordContentInClipboard())
        MyEditor.Editor.PasteFromMsWord();
    else
        MyEditor.Editor.Paste();
    If MyEditor.Editor.IsMsWordContentInClipboard() Then
        MyEditor.Editor.PasteFromMsWord()
    Else
        MyEditor.Editor.Paste()
    End If

    Cut Method

    Performs a standard cut: removes the current selection from the document and places it on the system clipboard. No-op when the selection is empty.

    Syntax
    void Cut();
    Example
    private void CutCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MyEditor.Editor.Cut();
    }
    Private Sub CutCommand_Executed(sender As Object, e As ExecutedRoutedEventArgs)
        MyEditor.Editor.Cut()
    End Sub

    Copy Method

    Copies the current selection to the system clipboard without modifying the document. No-op when the selection is empty.

    Syntax
    void Copy();
    Example
    private void CopyCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MyEditor.Editor.Copy();
    }
    Private Sub CopyCommand_Executed(sender As Object, e As ExecutedRoutedEventArgs)
        MyEditor.Editor.Copy()
    End Sub

    Delete Method

    Deletes the current selection from the document. When there is no selection, deletes the character to the right of the caret (forward delete).

    Syntax
    void Delete();
    Example
    private void DeleteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MyEditor.Editor.Delete();
    }
    Private Sub DeleteCommand_Executed(sender As Object, e As ExecutedRoutedEventArgs)
        MyEditor.Editor.Delete()
    End Sub

    Paste Method

    Pastes clipboard content at the caret position using the editor's standard paste pipeline. For Word-origin clipboards prefer PasteFromMsWord to apply Word-specific cleaning.

    Syntax
    void Paste();
    Example
    private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MyEditor.Editor.Paste();
    }
    Private Sub PasteCommand_Executed(sender As Object, e As ExecutedRoutedEventArgs)
        MyEditor.Editor.Paste()
    End Sub

    Undo Method

    Reverts the most recent edit. The undo stack covers typing, formatting, and structural changes; see ClearUndoStack to reset it.

    Syntax
    void Undo();
    Example
    private void UndoButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.Undo();
    }
    Private Sub UndoButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.Undo()
    End Sub

    Redo Method

    Reapplies the most recently undone edit. The redo stack is cleared whenever a new edit is made after an undo.

    Syntax
    void Redo();
    Example
    private void RedoButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.Redo();
    }
    Private Sub RedoButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.Redo()
    End Sub

    ToggleOverWriteMode Method

    Switches the editor between insert mode and overwrite mode, mirroring the effect of the Insert key. In overwrite mode each typed character replaces the character under the caret.

    Syntax
    void ToggleOverWriteMode();
    Example
    private void InsertKeyBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        MyEditor.Editor.ToggleOverWriteMode();
    }
    Private Sub InsertKeyBinding_Executed(sender As Object, e As ExecutedRoutedEventArgs)
        MyEditor.Editor.ToggleOverWriteMode()
    End Sub

    Print Method

    Prints the current document. When showDialog is true the standard Windows print dialog is shown first so the user can choose a printer and options; otherwise the default printer is used silently.

    Syntax
    void Print(bool showDialog);
    Parameters
    NameTypeDescription
    showDialogSystem.BooleanIf true, show the print dialog before printing.
    Remarks

    If no printer is installed on the machine the editor shows a message box; the caption of that box is controlled by NoPrinterMessageCaption.

    Example
    private void PrintButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.Print(showDialog: true);
    }
    Private Sub PrintButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.Print(showDialog:=True)
    End Sub

    ExecCommand Method

    Executes one of the underlying browser exec-commands (the same command identifiers used by the legacy document.execCommand DOM API). Useful as an escape hatch when a built-in editor method does not cover the operation you need.

    Syntax
    void ExecCommand(string cmdId, bool showUi = false, object? value = null);
    Parameters
    NameTypeDescription
    cmdIdSystem.StringThe exec-command identifier, for example "Bold" or "InsertOrderedList".
    showUiSystem.BooleanWhether the command may show a user-interface element. Default false.
    valueSystem.ObjectOptional command parameter; semantics depend on cmdId.
    Example
    // Apply a red foreground colour to the current selection.
    MyEditor.Editor.ExecCommand("ForeColor", false, "#cc0000");
    ' Apply a red foreground colour to the current selection.
    MyEditor.Editor.ExecCommand("ForeColor", False, "#cc0000")

    ShowPrintPreviewDialog Method

    Opens the system print-preview dialog showing how the current document will appear on paper. The dialog is modal with respect to the editor's host window.

    Syntax
    void ShowPrintPreviewDialog();
    Example
    private void PrintPreviewButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.ShowPrintPreviewDialog();
    }
    Private Sub PrintPreviewButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.ShowPrintPreviewDialog()
    End Sub

    ShowPageSetupDialog Method

    Opens the system page-setup dialog so the user can adjust margins, orientation, and paper size. Settings chosen here apply to subsequent Print and ShowPrintPreviewDialog calls.

    Syntax
    void ShowPageSetupDialog();
    Example
    private void PageSetupButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.ShowPageSetupDialog();
    }
    Private Sub PageSetupButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.ShowPageSetupDialog()
    End Sub

    JustifyLeft Method

    Toggles left-justification on the block containing the caret. Calling the method twice removes the justification.

    Syntax
    void JustifyLeft();
    Example
    private void AlignLeftButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.JustifyLeft();
    }
    Private Sub AlignLeftButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.JustifyLeft()
    End Sub

    JustifyRight Method

    Toggles right-justification on the block containing the caret.

    Syntax
    void JustifyRight();
    Example
    private void AlignRightButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.JustifyRight();
    }
    Private Sub AlignRightButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.JustifyRight()
    End Sub

    JustifyCenter Method

    Toggles centred justification on the block containing the caret.

    Syntax
    void JustifyCenter();
    Example
    private void AlignCenterButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.JustifyCenter();
    }
    Private Sub AlignCenterButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.JustifyCenter()
    End Sub

    Search Method

    Searches the document for the next or previous occurrence of text. When a match is found the editor selects it and scrolls it into view; the method returns true. When no match is found nothing is selected and the method returns false.

    Syntax
    bool Search(string text, bool forward, bool matchWholeWord, bool matchCase, bool useCaretPosition = false);
    Parameters
    NameTypeDescription
    textSystem.StringThe text to find.
    forwardSystem.BooleanIf true, search forward from the current selection or caret; otherwise search backward.
    matchWholeWordSystem.BooleanIf true, only whole-word matches count.
    matchCaseSystem.BooleanIf true, the comparison is case-sensitive.
    useCaretPositionSystem.BooleanIf true and the current selection is empty, start the search from the caret position instead of the document start. Default false.
    Returns

    System.Boolean - true if a match was found and selected; otherwise false.

    Example
    bool found = MyEditor.Editor.Search(text: "invoice", forward: true, matchWholeWord: false, matchCase: false);
    if (!found)
        MessageBox.Show("No more matches.");
    Dim found As Boolean = MyEditor.Editor.Search(text:="invoice", forward:=True, matchWholeWord:=False, matchCase:=False)
    If Not found Then MessageBox.Show("No more matches.")

    Replace Method

    If the current selection equals searchString (subject to the case and whole-word flags), replaces it with replacement. Returns true when a replacement actually happened. Typically called in a loop with Search to implement an interactive Find and Replace workflow.

    Syntax
    bool Replace(string searchString, string replacement, bool matchWholeWord, bool matchCase);
    Parameters
    NameTypeDescription
    searchStringSystem.StringThe text that the current selection must match.
    replacementSystem.StringThe replacement text.
    matchWholeWordSystem.BooleanIf true, only whole-word matches qualify.
    matchCaseSystem.BooleanIf true, the comparison is case-sensitive.
    Returns

    System.Boolean - true if the selection matched and was replaced; otherwise false.

    Example
    // Interactive workflow: Find, then Replace on user click.
    if (MyEditor.Editor.Search("colour", true, false, false))
    {
        MyEditor.Editor.Replace("colour", "color", false, false);
    }
    ' Interactive workflow: Find, then Replace on user click.
    If MyEditor.Editor.Search("colour", True, False, False) Then
        MyEditor.Editor.Replace("colour", "color", False, False)
    End If

    ReplaceAll Method

    Replaces every occurrence of replaceWhat in the document with replaceWith. Returns the number of replacements performed.

    Syntax
    int ReplaceAll(string replaceWhat, string replaceWith, bool matchWholeWord, bool matchCase);
    Parameters
    NameTypeDescription
    replaceWhatSystem.StringThe text to find.
    replaceWithSystem.StringThe replacement text.
    matchWholeWordSystem.BooleanIf true, only whole-word matches qualify.
    matchCaseSystem.BooleanIf true, the comparison is case-sensitive.
    Returns

    System.Int32 - the number of occurrences found and replaced.

    Example
    int n = MyEditor.Editor.ReplaceAll(replaceWhat: "colour", replaceWith: "color", matchWholeWord: true, matchCase: false);
    MessageBox.Show($"Replaced {n} occurrence(s).");
    Dim n As Integer = MyEditor.Editor.ReplaceAll(replaceWhat:="colour", replaceWith:="color", matchWholeWord:=True, matchCase:=False)
    MessageBox.Show($"Replaced {n} occurrence(s).")

    SearchWithDefaultIeDialog Method

    Opens the built-in IE-style search dialog so the end user can interactively find text within the editor. The dialog raises the editor's search-related events (see SearchEventArg, ReplaceEventArg, ReplaceAllEventArg).

    Syntax
    void SearchWithDefaultIeDialog();
    Example
    private void FindButton_Click(object sender, RoutedEventArgs e)
    {
        MyEditor.Editor.SearchWithDefaultIeDialog();
    }
    Private Sub FindButton_Click(sender As Object, e As RoutedEventArgs)
        MyEditor.Editor.SearchWithDefaultIeDialog()
    End Sub

    DeleteText Method

    Deletes length characters starting at the plain-text startIndex. Useful for programmatic editing without disturbing the user's selection.

    Syntax
    void DeleteText(int startIndex, int length);
    Parameters
    NameTypeDescription
    startIndexSystem.Int32Zero-based plain-text offset where the deletion begins.
    lengthSystem.Int32Number of characters to delete.
    Example
    // Strip the first ten characters of plain text from the document.
    
    MyEditor.Editor.DeleteText(startIndex: 0, length: 10);

    ReplaceText Method

    Replaces length characters starting at the plain-text startIndex with replacementText. The replacement may be longer or shorter than the original span.

    Syntax
    void ReplaceText(int startIndex, int length, string replacementText);
    Parameters
    NameTypeDescription
    startIndexSystem.Int32Zero-based plain-text offset where the replacement begins.
    lengthSystem.Int32Number of characters to replace.
    replacementTextSystem.StringThe text to insert in place of the original span.
    Example
    // Replace 4 characters at offset 12 with a longer word.
    
    MyEditor.Editor.ReplaceText(12, 4, "Hello");

    PrintPageSetup Method (Uniform Margin)

    Sets uniform page margins (top, bottom, left, and right) for subsequent printing. The value is in inches.

    Syntax
    void PrintPageSetup(double margin);
    Parameters
    NameTypeDescription
    marginSystem.DoubleUniform page margin in inches, applied to all four sides.
    Example
    // One-inch margins, then print silently.
    MyEditor.Editor.PrintPageSetup(margin: 1.0);
    MyEditor.Editor.Print(showDialog: false);
    ' One-inch margins, then print silently.
    MyEditor.Editor.PrintPageSetup(margin:=1.0)
    MyEditor.Editor.Print(showDialog:=False)

    PrintPageSetup Method (Per-Side Margins)

    Sets per-side page margins for subsequent printing. All values are in inches.

    Syntax
    void PrintPageSetup(double topMargin, double bottomMargin, double rightMargin, double leftMargin);
    Parameters
    NameTypeDescription
    topMarginSystem.DoubleTop margin in inches.
    bottomMarginSystem.DoubleBottom margin in inches.
    rightMarginSystem.DoubleRight margin in inches.
    leftMarginSystem.DoubleLeft margin in inches.
    Example
    // Wider left margin for binding.
    
    MyEditor.Editor.PrintPageSetup(
    
        topMargin:    1.0,
    
        bottomMargin: 1.0,
    
        rightMargin:  0.75,
    
        leftMargin:   1.25);

    ClearUndoStack Method

    Clears the WYSIWYG editor's undo history. Typically called immediately after loading a document so the user cannot undo past the load point.

    Syntax
    void ClearUndoStack();
    Example
    // Load fresh content and prevent users from undoing into the previous document.
    
    MyEditor.LoadHtml(myHtml);
    
    MyEditor.Editor.ClearUndoStack();

    HasHorizontalScrollbar Property

    Gets a value indicating whether the editor is currently showing a horizontal scroll bar. The value reflects the actual visible state, not the configured policy.

    Syntax
    bool HasHorizontalScrollbar { get; }
    Returns

    System.Boolean - true when the horizontal scroll bar is visible; otherwise false.

    Example
    if (MyEditor.Editor.HasHorizontalScrollbar)
        MyEditor.Editor.AdjustEditorWidth();
    If MyEditor.Editor.HasHorizontalScrollbar Then MyEditor.Editor.AdjustEditorWidth()

    HasVerticalScrollbar Property

    Gets a value indicating whether the editor is currently showing a vertical scroll bar.

    Syntax
    bool HasVerticalScrollbar { get; }
    Returns

    System.Boolean - true when the vertical scroll bar is visible; otherwise false.

    Example
    if (MyEditor.Editor.HasVerticalScrollbar)
        MyEditor.Editor.AdjustEditorHeight();
    If MyEditor.Editor.HasVerticalScrollbar Then MyEditor.Editor.AdjustEditorHeight()

    NoPrinterMessageCaption Property

    Gets or sets the caption of the message box shown when Print or ShowPrintPreviewDialog is invoked on a machine with no printer installed. Localise this string to match your application's language.

    Syntax
    string NoPrinterMessageCaption { get; set; }
    Returns

    System.String - the caption of the "no printer" message box.

    Example
    // Localise the no-printer message caption at startup.
    
    MyEditor.Editor.NoPrinterMessageCaption = "Printer not available";

    Last updated on May 14, 2026