Formatting Service

The IFormattingService interface exposes character-level, paragraph-level and block-structural formatting commands for the active selection in a WinFormHtmlEditor control. It is reached through the Formatting property on the editor (for example htmlEditor1.Formatting.Bold()). Every method below operates on whatever the user has currently selected, or on the insertion point when the selection is empty — in which case typing that follows inherits the new style.

Methods that toggle a style accept a FormatOptions enum: SetOn forces the style on, SetOff forces it off, and Toggle (the default) flips it. Every call participates in the editor undo stack, so htmlEditor1.Undo() reverses the most recent formatting change. The interface implements IDisposable because the runtime implementation holds a reference to the underlying MSHTML command target; the editor disposes it during teardown, so application code never needs to call Dispose directly.

Namespace: SpiceLogic.HtmlEditor.Abstractions.Services
Assembly: SpiceLogic.HtmlEditor.Abstractions
Declaration: public interface IFormattingService : IDisposable

ChangeFont(string fontFamilyName, string fontSize, Color foreColor)

Applies font family, font size and foreground color to the current selection in a single call. Use this when the user picks all three values together (for example from a font dialog) so that the change shows up as one undo step instead of three.

Syntax
void ChangeFont(string fontFamilyName, string fontSize, System.Drawing.Color foreColor);
Parameters
NameTypeDescription
fontFamilyNamestringCSS font-family name, for example "Arial" or "Times New Roman".
fontSizestringHTML legacy size ("1".."7") or any CSS size with unit, for example "12pt" or "16px".
foreColorSystem.Drawing.ColorThe foreground (text) color to apply.
Returns

None.

Example
using System.Drawing;

using SpiceLogic.HtmlEditor.Abstractions.Services;



// User selected some text and clicked OK on a font dialog.

htmlEditor1.Formatting.ChangeFont("Calibri", "12pt", Color.DarkBlue);

ChangeFontName(string fontFamilyName)

Changes only the font family of the current selection, leaving size and color untouched. This is the call to wire to a font-name combo box on a toolbar.

Syntax
void ChangeFontName(string fontFamilyName);
Parameters
NameTypeDescription
fontFamilyNamestringAny CSS-recognised font-family name.
Returns

None.

Example
private void fontNameCombo_SelectedIndexChanged(object sender, EventArgs e)

{

    var name = fontNameCombo.SelectedItem?.ToString();

    if (!string.IsNullOrEmpty(name))

        htmlEditor1.Formatting.ChangeFontName(name);

}

ChangeFontSize(string fontSize)

Changes only the font size of the current selection. Accepts either an HTML legacy size ("1".."7") or a CSS size with unit such as "16px", "10pt" or "1.2em".

Syntax
void ChangeFontSize(string fontSize);
Parameters
NameTypeDescription
fontSizestringHTML legacy size string or CSS size with unit.
Returns

None.

Example
// Bump the selected text to 14 points.

htmlEditor1.Formatting.ChangeFontSize("14pt");

ChangeForeColor(Color foreColor)

Sets the foreground (text) color of the current selection. Maps to the HTML foreColor command on the underlying MSHTML command target.

Syntax
void ChangeForeColor(System.Drawing.Color foreColor);
Parameters
NameTypeDescription
foreColorSystem.Drawing.ColorThe color to apply as text foreground.
Returns

None.

Example
using System.Drawing;



using (var dlg = new ColorDialog())

{

    if (dlg.ShowDialog() == DialogResult.OK)

        htmlEditor1.Formatting.ChangeForeColor(dlg.Color);

}

ChangeHighlightColor(Color highlightColor)

Sets the highlight (background) color of the current selection. Equivalent to a marker-pen tool in a word processor.

Syntax
void ChangeHighlightColor(System.Drawing.Color highlightColor);
Parameters
NameTypeDescription
highlightColorSystem.Drawing.ColorThe color to apply as the selection highlight (background).
Returns

None.

Example
using System.Drawing;



// Highlight the current selection in yellow.

htmlEditor1.Formatting.ChangeHighlightColor(Color.Yellow);

Bold(FormatOptions option = FormatOptions.Toggle)

Applies, removes, or toggles bold styling on the current selection. With the default Toggle value the call behaves exactly like a toolbar Bold button or Ctrl+B.

Syntax
void Bold(FormatOptions option = FormatOptions.Toggle);
Parameters
NameTypeDescription
optionFormatOptionsSetOn forces bold on, SetOff removes bold, Toggle flips the current state. Default is Toggle.
Returns

None.

Example
using SpiceLogic.HtmlEditor.Abstractions;



// Toolbar Bold button.

htmlEditor1.Formatting.Bold();



// Force-on (e.g. when applying a paragraph style).

htmlEditor1.Formatting.Bold(FormatOptions.SetOn);

Italic(FormatOptions option = FormatOptions.Toggle)

Applies, removes, or toggles italic styling on the current selection.

Syntax
void Italic(FormatOptions option = FormatOptions.Toggle);
Parameters
NameTypeDescription
optionFormatOptionsSetOn, SetOff, or Toggle (default).
Returns

None.

Example
htmlEditor1.Formatting.Italic();  // Ctrl+I-style toggle

Underline(FormatOptions option = FormatOptions.Toggle)

Applies, removes, or toggles underline styling on the current selection.

Syntax
void Underline(FormatOptions option = FormatOptions.Toggle);
Parameters
NameTypeDescription
optionFormatOptionsSetOn, SetOff, or Toggle (default).
Returns

None.

Example
htmlEditor1.Formatting.Underline();

Superscript(FormatOptions option = FormatOptions.Toggle)

Applies, removes, or toggles superscript styling on the current selection (rendered above the baseline at a smaller size). Mutually exclusive with subscript at the same caret — enabling superscript clears subscript automatically.

Syntax
void Superscript(FormatOptions option = FormatOptions.Toggle);
Parameters
NameTypeDescription
optionFormatOptionsSetOn, SetOff, or Toggle (default).
Returns

None.

Example
// Render the selected "2" in x^2 above the baseline.

htmlEditor1.Formatting.Superscript(FormatOptions.SetOn);

Subscript(FormatOptions option = FormatOptions.Toggle)

Applies, removes, or toggles subscript styling on the current selection (rendered below the baseline at a smaller size). Mutually exclusive with superscript.

Syntax
void Subscript(FormatOptions option = FormatOptions.Toggle);
Parameters
NameTypeDescription
optionFormatOptionsSetOn, SetOff, or Toggle (default).
Returns

None.

Example
// Render the selected "2" in H2O below the baseline.

htmlEditor1.Formatting.Subscript(FormatOptions.SetOn);

StrikeThrough(FormatOptions option = FormatOptions.Toggle)

Applies, removes, or toggles strike-through styling on the current selection.

Syntax
void StrikeThrough(FormatOptions option = FormatOptions.Toggle);
Parameters
NameTypeDescription
optionFormatOptionsSetOn, SetOff, or Toggle (default).
Returns

None.

Example
htmlEditor1.Formatting.StrikeThrough();

Outdent()

Decreases the block-level indent of the paragraph containing the current selection. If the paragraph is already at the leftmost level, the call is a no-op.

Syntax
void Outdent();
Parameters

None.

Returns

None.

Example
// Promote a nested bullet up one level.

htmlEditor1.Formatting.Outdent();

Indent()

Increases the block-level indent of the paragraph containing the current selection. Inside a list, Indent nests the current bullet or numbered item one level deeper.

Syntax
void Indent();
Parameters

None.

Returns

None.

Example
htmlEditor1.Formatting.Indent();   // nest the current bullet one level

htmlEditor1.Formatting.Outdent(); // promote it back

AlignLeft()

Left-aligns the paragraph(s) containing the current selection.

Syntax
void AlignLeft();
Parameters

None.

Returns

None.

Example
htmlEditor1.Formatting.AlignLeft();

AlignRight()

Right-aligns the paragraph(s) containing the current selection.

Syntax
void AlignRight();
Parameters

None.

Returns

None.

Example
htmlEditor1.Formatting.AlignRight();

AlignCenter()

Center-aligns the paragraph(s) containing the current selection.

Syntax
void AlignCenter();
Parameters

None.

Returns

None.

Example
htmlEditor1.Formatting.AlignCenter();

RemoveAlignment()

Clears explicit horizontal alignment from the paragraph(s) containing the current selection, returning them to the document's default flow direction (typically left in left-to-right languages).

Syntax
void RemoveAlignment();
Parameters

None.

Returns

None.

Remarks

"Justify" (full justification) is intentionally not exposed by the service — MSHTML's rendering of justified text proved inconsistent across document widths and was retired.

Example
htmlEditor1.Formatting.RemoveAlignment();

ApplyList(ListTypes listType, FormatOptions option = FormatOptions.Toggle)

Converts the paragraph(s) under the selection into a bullet or numbered list, removes the list, or toggles it depending on option.

Syntax
void ApplyList(ListTypes listType, FormatOptions option = FormatOptions.Toggle);
Parameters
NameTypeDescription
listTypeListTypesBulletList for unordered (<ul>) lists, NumberedList for ordered (<ol>) lists.
optionFormatOptionsSetOn, SetOff, or Toggle (default).
Returns

None.

Example
using SpiceLogic.HtmlEditor.Abstractions;



// Toolbar "Numbered List" button.

htmlEditor1.Formatting.ApplyList(ListTypes.NumberedList);

RemoveFormatFromSelection()

Strips inline character formatting (bold, italic, underline, font family, color, size) from the current selection while leaving paragraph structure intact. Block-level formatting such as alignment and list state is not affected.

Syntax
void RemoveFormatFromSelection();
Parameters

None.

Returns

None.

Example
// Clean up styles that came in via paste.

htmlEditor1.Formatting.RemoveFormatFromSelection();

RemoveFormatAll()

Strips inline character formatting from the entire document body. Useful when pasting content from a third-party word processor that brought along inline styles you do not want.

Syntax
void RemoveFormatAll();
Parameters

None.

Returns

None.

Remarks

This is destructive across the whole document. Consider prompting the user before calling it.

Example
if (MessageBox.Show("Strip all inline formatting?", "Confirm",

        MessageBoxButtons.YesNo) == DialogResult.Yes)

{

    htmlEditor1.Formatting.RemoveFormatAll();

}

ApplyTitleHeader(int headerNumber)

Wraps the paragraph(s) under the current selection in an HTML heading element <h1> through <h6>. Used to convert plain paragraphs into section headers.

Syntax
void ApplyTitleHeader(int headerNumber);
Parameters
NameTypeDescription
headerNumberintHeading level, 1 through 6. 1 produces <h1>, 2 produces <h2>, and so on.
Returns

None.

Example
// Promote the current paragraph to an H2 section header.

htmlEditor1.Formatting.ApplyTitleHeader(2);

RemoveHeaderTitle()

Removes the heading wrapper from the paragraph(s) under the current selection, converting <h1>..<h6> back to a normal <p> paragraph.

Syntax
void RemoveHeaderTitle();
Parameters

None.

Returns

None.

Example
htmlEditor1.Formatting.RemoveHeaderTitle();
Last updated on May 14, 2026