Back to Devexpress

How to: Convert a Word Document to HTML Format

officefileapi-116818-word-processing-document-api-examples-document-conversion-how-to-convert-a-docx-document-to-html-format.md

latest3.9 KB
Original Source

How to: Convert a Word Document to HTML Format

  • Sep 19, 2023
  • 2 minutes to read

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

View Example

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

System.Diagnostics.Process.Start(filePath);
vb
server.LoadDocument("Documents\MovieRentals.docx", DocumentFormat.Docx)
Dim filePath As String = "Document_HTML.html"
Using htmlFileStream As New FileStream(filePath, FileMode.Create)
    server.SaveDocument(htmlFileStream, DocumentFormat.Html)
End Using

System.Diagnostics.Process.Start(filePath)

Asynchronous Save

Important

The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v25.2.dll assembly and DevExpress.Document.Processor NuGet package. Add this assembly or package to your project to use the RichEditDocumentServerExtensions members. You need a 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:

csharp
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
          }
      }
  }
}
vb
Private Async Sub ConvertDocx2HtmlWithCancellation()
    Using source As CancellationTokenSource = New CancellationTokenSource(10000)

        Using wordProcessor As RichEditDocumentServer = New RichEditDocumentServer()

            Try
                Await wordProcessor.LoadDocumentAsync("Document.docx", source.Token)
                Await wordProcessor.SaveDocumentAsync("result.html", DocumentFormat.Html, source.Token)
            Catch __unusedOperationCanceledException1__ As OperationCanceledException
            ' Your code to handle cancellation
            End Try
        End Using
    End Using
End Sub

See Also

Use Word Processing Document API to Load HTML Files or Export Documents to HTML