Convert DOCX to HTML in CSharp

Either you want to convert a document to HTML format to get the content for your website, or you have come across an online document submission website that requires documents to be submitted in HTML format. In either case, you need a DOC to HTML converter. However, if you need to convert your documents to HTML programmatically, then this article is for you only. This article will cover the following ways to convert documents to HTML in C#:

  • Simplest conversion of documents like DOCX to HTML in C#.
  • Convert to HTML with customized options.
  • Convert using the option to show or hide page borders.

C# Document Conversion Library

GroupDocs.Conversion for .NET is an easy to use powerful API with the ability to convert any document from the wide list of supported document formats into any supported target formats. You may download the API from the downloads section or install it from NuGet.

Convert DOCX to HTML in C# - Simple

This is the simplest and very useful conversion. I better say that you can convert any of your documents to the HTML format. Just check your format from the supported formats list and go-ahead to get it converted.

  • Create the instance of the Converter class to start with your source document.
  • Instantiate MarkupConvertOptions object.
  • Call the Convert method of the Converter class.
  • That’s it.

Your document will be converted to HTML and the resultant document will be there in your repository. The following small code sample shows the conversion of a DOCX file into HTML using the Converter class in C#.

// Converting DOCX to HTML in C#
using (Converter converter = new Converter("document.docx"))
{
    MarkupConvertOptions options = new MarkupConvertOptions();
    converter.Convert("converted.html", options);
}

Convert DOC/DOCX to HTML with Customized Options

GroupDocs.Conversion provides different other options to get the desired conversion result. The customized options include:

  • Fixed Layout
  • Fixed Layout - Show Borders
  • Format
  • Page Number
  • Pages
  • Pages Count
  • Use PDF
  • Watermark
  • Zoom

You may visit the documentation or GitHub samples to see each option in detail. I will show some of the customizations while again converting the DOCX to HTML format in below code sample.

// Converting DOCX to HTML in C# with advance options.
using (Converter converter = new Converter("document.docx"))
{
    MarkupConvertOptions options = new MarkupConvertOptions
    { // Setting customized options
        PageNumber = 2,
        PagesCount = 1,
        FixedLayout = true
    };
    converter.Convert("converted.html", options);
}

Convert DOC/DOCX to HTML - Show or Hide Page Borders

Last but not least, you can now control the visibility of page borders while converting documents to HTML in C#. The GroupDocs.Conversion for .NET gives this control to the C# programmers. The below example shows that by setting the FixedLayoutShowBorders property of MarkupConvertOptions class to true or false, you can show or hide the page borders in the resultant HTML document.

// Converting DOCX to HTML in C# with show or hide borders control.
using (Converter converter = new Converter("document.docx"))
{
    MarkupConvertOptions options = new MarkupConvertOptions
    {
        PageNumber = 2,
        FixedLayout = true,
        PagesCount = 1,
        FixedLayoutShowBorders = false
    };
    converter.Convert("converted.html", options);
}

Images below showing the original DOCX document and the converted HTML with and without page borders.

Docx document to convert into HTML

Original DOCX Document

HTML File with page borders and no borders.

The above figure shows the HTML files that are converted from DOCX with show borders and do not show borders options.

Learn more about GroupDocs.Conversion

Let’s talk more @ Free Support Forum.