Converting HTML to Word documents is essential for seamless document sharing, archiving, and preserving formatting consistency. It simplifies editing, integrates smoothly with other tools, and ensures compliance with industry standards. Additionally, Word documents provide offline access and a professional look, making them ideal for formal submissions. Ultimately, this conversion enhances accessibility and streamlines content management.
This article explores how to use C# and Spire.Doc for .NET to convert HTML files and strings into Word documents, providing clear examples and step-by-step instructions.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert an HTML File to Word in C#
To convert an HTML file to a Word document, you primarily use the Document class from the Spire.Doc library. This class provides methods to load HTML content from a file and save it in various formats.
The following are the key steps to convert an HTML file to a Word document using C# with Spire.Doc:
- Create a Document object.
- Load an HTML file using the Document.LoadFromFile() method.
- Saves the document as a Word file using the Document.SaveToFile() method.
- C#
using Spire.Doc; using Spire.Doc.Documents; namespace ConvertHtmlFileToWord { class Program { static void Main(string[] args) { // Create a Document object Document document = new Document(); // Load an HTML file document.LoadFromFile(@"C:\Users\Administrator\Desktop\MyHtml.html", FileFormat.Html, XHTMLValidationType.None); // Get the first section Section section = document.Sections[0]; // Set the page margins section.PageSetup.Margins.All = 2; // Save the document to a Word file document.SaveToFile("ToWord.docx", FileFormat.Docx); // Dispose resources document.Dispose(); } } }
Append an HTML String to Word in C#
In certain circumustancs, you may need to create or modify HTML content dynamically at runtime, for example, generating HTML strings based on user input. Spire.Doc allows you to render HTML strings into a Word document using the Paragraph.AppendHTML() method.
The following are the key steps to append HTML stings to Word using C# and Sprie.Doc:
- Create a Document object.
- Add a section and a paragraph to the document.
- Read an HTML string from an HTML file or any other data sources.
- Render the HTML string into the document using the Paragraph.AppendHTML() method.
- Saves the document as a Word file using the Document.SaveToFile() method.
- C#
using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; using System.Drawing.Imaging; namespace AppendHtmlStringToWord { class Program { static void Main(string[] args) { // Create a Document object Document document = new Document(); // Add a section to the document Section section = document.AddSection(); // Set the page margins section.PageSetup.Margins.All = 2; // Add a paragraph to the section Paragraph paragraph = section.AddParagraph(); // Read HTML string from a file string htmlFilePath = @"C:\Users\Administrator\Desktop\Html.html"; string htmlString = File.ReadAllText(htmlFilePath, System.Text.Encoding.UTF8); // Append the HTML string to the paragraph paragraph.AppendHTML(htmlString); // Save the document to a Word file document.SaveToFile("AppendHtml.docx", FileFormat.Docx); // Dispose resources document.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.