Html to OpenXmlHtml to OpenXml
Home
  • Library Entry Points
  • Page Layout
  • Hyperlinks and Internal Navigation
  • Image and Asset Processing Guide
  • Styling
  • Tables
  • Numbering List
  • Preformated Text
  • Defined Terms and Notes
  • Right to Left (Rtl)
LLM Docs Friendly
NuGet
GitHub
Home
  • Library Entry Points
  • Page Layout
  • Hyperlinks and Internal Navigation
  • Image and Asset Processing Guide
  • Styling
  • Tables
  • Numbering List
  • Preformated Text
  • Defined Terms and Notes
  • Right to Left (Rtl)
LLM Docs Friendly
NuGet
  • API Reference

    • Quickstart
    • Library Entry Points
    • Page Layout
    • Hyperlinks and Internal Navigation
    • Image and Asset Processing Guide
    • Styling
    • Tables
    • Numbering List
    • Preformated Text
    • Defined Terms and Notes
    • Right to Left (Rtl)
    • Extensibility
  • LLM Docs Friendly
  • Performance
  • Appendix

    • Content Type MIME Header
    • Convert .dotx to .docx
    • Placeholder Replacement
    • Restrict Editing
  • Changelog

Transformers

Transformers are executed after HTML document has been parsed and before OpenXml conversion. Unlike HtmlFragment, which intentionally exposes only a limited HTML-element abstraction, transformers receive the full AngleSharp IDocument instance and therefore have unrestricted access to the underlying DOM tree and parser ecosystem.

Transformers are executed sequentially, in the order they are registered.

Typical use cases

  • Rewrite or normalise HTML generated by third-party systems.
  • Inline external stylesheets.
  • Process MathML or non-HTML elements.
  • Manipulate document metadata or content in the <head> section.
  • Rewrite image URLs or resource references.
  • Integrate AngleSharp-based extensions and services
  • Apply organisation-specific preprocessing rules before conversion
converter.Transformers.Add(new CorporateTransformer());
CorporateTransformer.cs
public sealed class CorporateTransformer : IHtmlTransformer
{
    public Task TransformAsync(AngleSharp.Dom.IDocument document, CancellationToken cancellationToken)
    {
        foreach (var note in document.QuerySelectorAll(".internal-note")
        {
            note.Remove();
        }
    }

    return Task.CompletedTask;
}

Parser Configurators

Parser Configurators provide access to the underlying AngleSharp parsing infrastructure used by HtmlConverter. They execute before any HTML is parsed and allow applications to customise the AngleSharp.IConfiguration used to create the document and browsing context. This extension point is intended for advanced scenarios that require access to AngleSharp services or third-party AngleSharp extensions.

Typical use cases

  • Enable support for AngleSharp extensions such as AngleSharp.Css.
  • Register custom services in the browsing context
  • Configure resource loaders and request pipelines
converter.ParserConfigurators.Add(new CssParserConfigurator());
CssParserConfigurator.cs
public sealed class CssParserConfigurator : IHtmlParserConfigurator
{
    public IConfiguration Configure(IConfiguration configuration)
    {
        return configuration.WithCss();
    }
}

Node Processors

Node Processors extend the OpenXml conversion pipeline by providing support for additional HTML nodes.

Unlike transformers, which modify the HTML document before conversion begins, node processors participate directly in the OpenXml generation process and produce OpenXml content for specific HTML nodes.

Processors are evaluated before the built-in HTML tag handlers. When a processor declares that it can handle a node, it becomes responsible for generating the corresponding OpenXml elements.

Typical use cases

  • Generate native Word content controls from HTML form elements.
  • Support custom HTML elements.
  • Implement MathML rendering.
  • Interpret organisation-specific markup.
  • Generate domain-specific OpenXml content.
converter.NodeProcessors.Add(
    new CurrentDateProcessor());
CurrentDateProcessor.cs
public sealed class CurrentDateProcessor : IHtmlNodeProcessor
{
    public bool CanProcess(INode node)
    {
        return node.NodeName.Equals(
            "h2o-date",
            StringComparison.OrdinalIgnoreCase);
    }

    public IEnumerable<OpenXmlElement> Interpret(IHtmlNodeContext context, INode node)
    {
        yield return new Run(
            new Text(DateTime.Today.ToShortDateString()));
    }
}

Using Custom Elements

A common approach is to introduce custom HTML elements that are interpreted by a node processor.

<h2o-date />

These elements are not part of standard HTML and are ignored by browsers, but can be used as placeholders for custom OpenXml generation.

IHtmlNodeContext

IHtmlNodeContext provides access to document-level services during node interpretation.

public interface IHtmlNodeContext
{
    MainDocumentPart MainPart { get; }

    WordDocumentStyle DocumentStyle { get; }

    void CascadeStyles(OpenXmlElement element);
}

CascadeStyles()

When a processor generates OpenXml elements directly, inherited HTML styles are not applied automatically.

Use CascadeStyles() to ensure generated content participates in the same style inheritance rules as content produced by HtmlToOpenXml itself.

public IEnumerable<OpenXmlElement> Interpret(IHtmlNodeContext context, INode node)
{
    var run = new Run(
        new Text("Generated content"));

    context.CascadeStyles(run);

    yield return run;
}

Processing Order

The loading and conversion pipeline executes in the following order:

  1. Create the default AngleSharp.IConfiguration.
  2. Apply all registered parser configurators.
  3. Create the IBrowsingContext.
  4. Parse the HTML document.
  5. Execute OnDocumentLoadedAsync().
  6. Execute all registered transformers.
  7. Build the HtmlToOpenXml expression tree.
  8. Evaluate registered node processors during OpenXml conversion.
  9. Generate the resulting OpenXml elements.
Edit this page
Last Updated: 11/27/26, 9:12 PM
Contributors: Olivier Nizet
Prev
Right to Left (Rtl)