Converting HTML Content to Word Documents in C#

To begin, you need to add a reference to the Microsoft.Office.Interop.Word library in your project.

Important note: Converting raw HTML markup directly into a Word document will only produce a .doc file, not a .docx file.

Step 1: Creating the Initial DOC File

First, generate a DOC file using file stream operations:

FileStream fileStream = new FileStream(filePath + documentName + ".doc", FileMode.Create);
StreamWriter writer = new StreamWriter(fileStream);
writer.Write("<html>" + htmlContent + "</html>");
writer.Close();
writer.Dispose();
fileStream.Close();
fileStream.Dispose();

ConvertToRealWord(documentName, outputDirectory);

After executing this code, a DOC file will be created containing your HTML content. However, this is only half the battle.

The Problem

If you try to save this DOC file through the "Save As" dialog, you'll notice that the default format selected is HTML. This reveals that the file is essentially still an HTML document with a DOC extension. To make it a genuine Word document, we need to perform a conversion.

Step 2: Converitng to a Real Word Document

The folowing method opens the HTML-based DOC file using Word Interop and resaves it in native Word format:

private string ConvertToRealWord(string fileName, string outputDirectory)
{
    string sourceFile = outputDirectory + fileName + ".doc";
    string temporaryFile = Server.MapPath("~/temp/") + fileName + "_temp.doc";

    object sourcePath = sourceFile;
    object tempPath = temporaryFile;
    
    Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
    object missing = Missing.Value;
    object wordFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
    
    Microsoft.Office.Interop.Word.Document document = wordApplication.Documents.Open(ref sourcePath, false);
    document.Activate();

    wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
    wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdNormalView;

    wordApplication.ActiveWindow.ActivePane.Selection.WholeStory();
    wordApplication.ActiveWindow.ActivePane.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpace1pt5;
    wordApplication.ActiveWindow.ActivePane.Selection.ParagraphFormat.LineUnitAfter = 0.5f;

    document.SaveAs(ref tempPath, ref wordFormat, ref missing, ref missing, ref missing, 
                    ref missing, ref missing, ref missing, ref missing, ref missing, 
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    document.Close(ref missing, ref missing, ref missing);
    wordApplication.Quit(ref missing, ref missing, ref missing);

    File.Delete(sourceFile);
    File.Move(temporaryFile, sourceFile);

    return sourceFile;
}

After processing through this method, the resulting file becomes an authentic Word document. The visual appearance and styling of the content remain consistent with the original HTML.

Bonus: Adding Page Breaks

To insert page breaks at specific locations within the document, add the following HTML tag at the desired position in your source HTML:

<br clear=all style='page-break-before:always'>

Tags: C# word-interop microsoft-office html-to-word document-generation

Posted on Thu, 30 Jul 2026 16:03:28 +0000 by shadow-x