How to: Convert a Word Document to HTML Format

  • 2 minutes to read

This example demonstrates how to use the RichEditDocumentServer.SaveDocument method to export the document to the HTML format.

server.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.OpenXml);
string filePath = "Document_HTML.html";
using (FileStream htmlFileStream = new FileStream(filePath, FileMode.Create))
{
    server.SaveDocument(htmlFileStream, DocumentFormat.Html);
}

System.Diagnostics.Process.Start(filePath);

Asynchronous Save

Important

The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v21.2.dll assembly. Add this assembly to your project to use the RichEditDocumentServerExtensions members. You need an active license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this library in production code.

Use the RichEditDocumentServerExtensions.SaveDocumentAsync method to asynchronously save a document to a file or stream.

Important

Take into account the following when you call this method:

  • The events fired by this method call may occur in a different thread than the target operation.

  • The operation is not thread safe (the document should not be accessed simultaneously by different threads). Wait until the operation is completed before you continue to work with the document (for example, use the await operator).

The code sample below uses the CancellationToken object to asynchronously export a document to HTML format:

private async void ConvertDocx2HtmlWithCancellation()
{
  //10 second limit
  using (CancellationTokenSource source = new CancellationTokenSource(10000))
  {
    using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
      {
          try
          {
              await wordProcessor.LoadDocumentAsync("Document.docx", source.Token);
              await wordProcessor.SaveDocumentAsync("result.html", DocumentFormat.Html, source.Token);
          }
          catch (OperationCanceledException)
          {
              // Your code to handle cancellation
          }
      }
  }
}
See Also