Products Technologies Demo Docs Blog Support Company

DOCX to HTML: Convert Documents to HTML and Prepare for Shadow DOM Rendering

Learn how to convert DOCX documents to HTML and prepare the HTML for rendering in a Shadow DOM. The sample project uses the TX Text Control .NET Server component to convert DOCX to HTML, to create stylesheets and to prepare the HTML for the Shadow DOM.

DOCX to HTML: Convert Documents to HTML and Prepare for Shadow DOM Rendering

To embed components into existing HTML pages without breaking styles or functionality, the HTML shadow DOM is typically used. We will use this concept to embed styled HTML into a document with its own set of styles that TX Text Control creates by converting a document such as Office Open XML (DOCX) or PDF.

TX Text Control provides a feature to convert documents to HTML including all styles and formatting. But in this example, we will be using a feature that has been the subject of a recent blog post, where new stylesheets are created from documents where no styles are present.

Learn More

This example shows how to identify paragraphs that are commonly formatted, and then convert them to style sheets. The example checks for character and paragraph formatting and compares them using Reflection.

Converting Paragraphs to Styles by Comparing Formatting Attributes

Generating the HTML

First, we need to create a new instance of the ServerTextControl class and load a document. In this example, we are loading a DOCX file. The document is loaded and the HTML is generated using the Save method with the TXTextControl.StreamType.HTMLFormat StreamType parameter.

public IActionResult Index()
{
    using (var tx = new TXTextControl.ServerTextControl())
    {
        var filename = "computers.docx";

        tx.Create();
        tx.Load($"Documents/{filename}", TXTextControl.StreamType.WordprocessingML);

        var styleManager = new StyleManager(tx);
        styleManager.ApplyStyles();

        var saveSettings = new TXTextControl.SaveSettings
        {
            CssSaveMode = TXTextControl.CssSaveMode.Inline
        };

        tx.Save($"Documents/{filename}.htm", 
            TXTextControl.StreamType.HTMLFormat, 
            saveSettings);

        using (var sr = new StreamReader($"Documents/{filename}.htm"))
        {
            ViewBag.Document = sr.ReadToEnd();
        }
    }

    return View();
}

After loading the DOCX file into the ServerTextControl, the styles are applied using the ApplyStyles function from the above article.

Finally, the HTML is exported and returned in a ViewBag for demo purposes.

Embedding the HTML

Now, we have the HTML that was generated from the document. This HTML can be embedded into an existing HTML page using the shadow DOM. The shadow DOM is a feature that allows you to attach a hidden DOM tree to an element in the regular DOM tree. This hidden tree is separate from the main document and can be styled and manipulated without affecting the main document.

First, we need to create a new shadow root in the view where the document will be rendered. The view is made up of two columns, with the shadow DOM being inserted in the second column. Inside the shadow DOM, the raw html from the ViewBag is being rendered.

<div class="row">
    <div class="col-md-4 text-danger bg-black p-3">
        <p>The document is rendered as HTML next to this box.</p>
    </div>
    <div class="col-md-8">
            <template shadowrootmode="open">
                @Html.Raw(ViewBag.Document)

                <style>

                    :host > p {
                        /*font-family: Arial, sans-serif !important;*/
                    }

                    .Style1 {
                        color: red;
                    }

                </style>

            </template>
    </div>
</div>

The following screenshot shows the result of the shadow DOM with the HTML rendered from the DOCX file:

HTML rendered in a shadow DOM

The HTML contains the generated CSS classes that are only used for the shadow DOM. Because we generated the styles in the controller method, we know the class names and can now modify the styles inside the shadow DOM. In the example above, the font size for Style1 is changed to red.

The commented code in the style section would change the entire font family to Arial.

HTML rendered in a shadow DOM

By using the shadow DOM, the styles of the embedded HTML can be controlled and modified without affecting the main document.

Conclusion

Using the shadow DOM to embed HTML into existing documents is a powerful feature to keep the styles of the embedded content separate from the main document. This allows you to control the styles of the embedded content without affecting the main document.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CoreDOCX

Why HTML is not a Substitute for Page-Oriented Formats like DOCX

In this blog post, we will discuss the limitations of HTML as a document format and explain why page-oriented formats, such as DOCX, remain essential for certain use cases. We will explore the…


ASP.NETASP.NET Core

Creating Trusted Document Containers with PDF/A-3b in .NET C#

TX Text Control allows developers to do more than just generate PDFs. They can also build trusted digital archives that combine human-readable documents and machine-readable data in one secure,…


ASP.NETASP.NET Core

Best Practices for Image Compression when Exporting to PDF in .NET C#

When generating PDFs programmatically, one of the most important factors affecting file size and rendering performance is how images are embedded and compressed. This article explores best…


ASP.NETASP.NET CoreFiltering

Filtering and Sorting Repeating Blocks in MailMerge using C#

TX Text Control MailMerge's ability to filter and sort repeating merge blocks is a key strength, making it ideal for creating dynamic reports, lists, and catalogs.


ASP.NETASP.NET CoreConference

Text Control at NDC Copenhagen Developers Festival 2025

Join Text Control at the 2025 NDC Copenhagen Developers Festival, where we will present our newest innovations and solutions for document processing, reporting, and PDF generation. This unique…