Converting HTML Content to DOCX Documents in C#

Introduction

Applications frequently store articles, descriptions, and templates as HTML but later need to edit them in a text processing program such as Microsoft Word. NOV Rich Text Editor can import and export both HTML and DOCX formats. Importing HTML properly is very difficult, mainly because older versions of HTML did not impose strict XML on the content, leading to the so-called "tag soup". Another big obstacle during the conversion is CSS. CSS styling in HTML is fundamentally different from the styling found in MS Word, and parsing and applying all specified styles to the content is also challenging.

NOV Rich Text Editor for .NET addresses those challenges by fully implementing the entire CSS specification as well as all the HTML parsing rules. This leads to quality HTML import functionality inside the editor that is comparable to a modern browser. All this is implemented in managed code, so attacks on the conversion process from malicious HTML are not possible. The fact that the control does not process JavaScript is a drawback in certain scenarios, but it also avoids any malicious JavaScript execution risks.

Overall, the HTML importer supports modern and legacy HTML, CSS styles, character references, external and embedded style sheets, and embedded images.

Basic HTML-to-DOCX Conversion

The basic HTML-to-DOCX conversion process is really quite simple - all you have to do is to install NOV from the NuGet package and then execute the following code:
									NRichTextView richTextView = new NRichTextView();

									richTextView.LoadFromLocalFile(@"C:\Documents\Article.html");
									richTextView.SaveToLocalFile(@"C:\Documents\Article.docx");
								

You can also convert the documents in memory if the code is part of a processing service. For example, to get a BLOB in DOCX format from the loaded HTML, you need to write:
									MemoryStream memoryStream = new MemoryStream();
									view.SaveToStream(memoryStream, NTextFormat.Docx);
									byte[] byteArrayContainingDocx = memoryStream.ToArray();
								

The Conversion Pipeline

So what happens inside the editor during the conversion process?

During import:
  1. Parse the source HTML and generate an HTML content tree.
  2. Parse the CSS inside the HTML and apply the styling rules to the HTML content tree.
  3. Evaluate the CSS styling to get the effective values for each node in the HTML content tree.
  4. Convert the HTML content tree to the NOV Rich Text Editor DOM.
  5. Export the document structure to DOCX.
During export:
  1. Convert the NOV Rich Text Editor DOM to a DOCX XML tree.
  2. Serialize the exported DOCX XML tree in memory.
  3. Compress the result using standard ZIP compression.

Modify Imported HTML Before Export

You can, of course, modify the imported HTML before exporting it to DOCX - the following code shows how to add a paragraph at the end of the document:
									NSection section = richTextView.Content.Sections[0];
									section.Blocks.Add(new NParagraph(
									"Exported as a Word-compatible document."));

									richTextView.SaveToLocalFile(@"C:\Documents\Article.docx");
								

HTML Conversion Considerations

  • Use well-structured HTML even though the importer tolerates malformed markup.
  • The importer does not process JavaScript, so any content that should be visible because of JavaScript code will not appear in the result.
  • Verify external CSS and image resource accessibility.

Conclusion

NOV Rich Text Editor provides a simple way to convert HTML content to DOCX. It also allows you to modify the document during the conversion process using the control's DOM. The HTML import process has its limitations - mainly, it does not run JavaScript code, and it may not process all content as a real browser would. However, the import is sufficiently good for most conversion scenarios as well as other applications like indexing, validation, etc., where security is important.

About Nevron Software

Founded in 1998, Nevron Software is a component vendor specializing in the development of premium presentation layer solutions for .NET-based technologies. Today, Nevron has established itself as a trusted partner worldwide for .NET LOB applications, SharePoint portals, and reporting solutions. Nevron technology is used by many Fortune 500 companies, large financial institutions, global IT consultancies, academic institutions, governments, and non-profits.
For more information, visit: www.nevron.com.
  
Our internal customer was asking for a Ratings site that included gauge metrics as well as some data in charts. The Nevron controls looked like a great fit and provided many functionality options as well as the ones that met our demands. The support team, as well as code examples and forums, helped us to pinpoint the specific properties we needed to adjust.
  

Ivan Orozco, Systems Analyst and Developer
The Chamberlain Group