Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active September 29, 2022 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/fc29225f1166062a913ac7b5ffbda876 to your computer and use it in GitHub Desktop.
Save conholdate-gists/fc29225f1166062a913ac7b5ffbda876 to your computer and use it in GitHub Desktop.
Convert HTML to Word Document using C#
// This code example demonstrates how to convert HTML file to a Word document using C#.
// Load HTML file using Document class
Document document = new Document(@"C:\Files\sample.html");
// Convert HTML file to Word DOCX format
document.Save(@"C:\Files\output.docx", SaveFormat.Docx);
// This code example demonstrates how to generate a Word document from an HTML string using C#.
// Create a new document
Document document = new Document();
// Create a document builder
DocumentBuilder builder = new DocumentBuilder(document);
// Insert HTML
builder.InsertHtml("<ul>\r\n" +
"<li>Item1</li>\r\n" +
"<li>Item2</li>\r\n" +
"</ul>");
// Save as DOCX
document.Save(@"C:\Files\html-string-as-word.docx", SaveFormat.Docx);
// This code example demonstrates how to save HTML web page directly from a live URL to a Word document using C#.
// The URL
string Url = "https://en.wikipedia.org/wiki/Aspose.Words";
// Define HTML load options
HtmlLoadOptions options = new HtmlLoadOptions();
byte[] imageData = null;
// Download content from URL as Byte array
using (var wc = new System.Net.WebClient())
imageData = wc.DownloadData(Url);
// Convert Byte array to stream
var urlStream = new MemoryStream(imageData);
// Create an instance of Document object
Document document = new Document(urlStream, options);
// Save as DOCX
document.Save(@"C:\Files\output_url.docx", SaveFormat.Docx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment