Come convertire un documento di Word in HTML usando C#

This post will show you how to convert Word Document to HTML using C#. We will concentrate on developing a step-by-step guide along with sample code to generate HTML from DOCX using C#. This document conversion to HTML format can be done without the need of any third-party software on any operating system, including MS Windows, Linux, and macOS.

Steps to Convert Word Document to HTML using C#

  1. Install GroupDocs.Conversion for .NET from the NuGet package manager
  2. Add a reference to GroupDocs.Conversion namespace
  3. Create an instance of the Converter class and load the source Word file
  4. Initialize MarkupConvertOptions class for defining attributes for the converted document
  5. Invoke Converter class Convert method, pass the converted file name and instance of MarkupConvertOptions class

As you can see, the process for developing the convert Word to HTML C# capability is fairly straightforward. To begin, download and install the appropriate package from NuGet, then reference it in the code. Then, for document transformation, create an instance of the Converter and MarkupConvertOptions classes and utilize the Convert method.

Code to Convert Word Document to HTML using C#

using System;
using GroupDocs.Conversion.Options.Convert;
namespace ConvertWordDocumentToHtmlUsingCSharp
{
class Program
{
public static void Main(string[] args) // Main function to convert Word Document to HTML using C#
{
// Remove the watermark in output HTML file by adding license
string licensePath = "GroupDocs.Conversion.lic";
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License();
lic.SetLicense(licensePath);
// Load the source Word file for conversion to HTML
var converter = new GroupDocs.Conversion.Converter("sample.docx");
// Set the convert options for HTML file
var convertOptions = new MarkupConvertOptions();
// Convert and save the DOCX in HTML format
converter.Convert("converted.html", convertOptions);
Console.WriteLine("Done");
}
}
}

We’ve written some sample code to show how to use the C# DOCX to HTML feature to convert documents. Basic document transformation and HTML generation from a Word document have been addressed. You can also use this code to enhance the converted HTML file’s attributes, such as the list of page indexes, the number of pages, the starting page number, the zoom level, and so on.

We developed the feature of converting Excel to PDF in C# in our previous article. If you want to learn more about it, you may read the post on how to convert Excel to PDF in C#.

 Italiano