Convert Word Document to HTML and Display in Browser using ASP.NET

This is more a code snippet rather than an article or tutorial as it will have very less explanation as the code is simply ready to use. The following snippet allows you to Convert a Microsoft Word document to HTML and display the same in browser using C# and VB.NET in ASP.NET.

HTML Markup
The html markup is very simple it simply has a ASP.Net FileUpload Control and a Upload button. On the click of the Upload button I am processing the uploaded word document and displaying the same in the browser.


<form id="form1" runat="server">

<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>

<asp:Button ID="btnUpload" OnClick="Upload" runat="server" Text="Upload"></asp:Button>

</form>


Since this application only targets Microsoft Word documents. You might want to have some validations so that only word documents can be uploaded hence for that you can read the following article:

Filtering Files based on Extensions in ASP.Net FileUpload Control




Adding Reference
In order to convert the Word Documents to HTML you will need Microsoft Word Object Library. You will need to add its reference to your project in the following way.

  • On the Project menu, click Add Reference.
  • On the COM tab, locate Microsoft Word Object Library, and then click Select

Note: Version is not important. The version of object library may vary depending on the version of Word installed on your computer.

Namespaces
You will need to add the following namespaces in order to the run the application

C#

using Microsoft.Office;
using Microsoft.Office.Interop.Word;
using System.IO;


VB.NET

Imports Microsoft.Office
Imports Microsoft.Office.Interop.Word
Imports System.IO


Word to Html Conversion
As discussed above I am converting the uploaded Word document to HTML on the upload event of the Button control.

C#

protected void Upload(object sender, EventArgs e)
{
    object missingType = Type.Missing;
    object readOnly = true;
    object isVisible = false;
    object documentFormat = 8;
    string randomName = DateTime.Now.Ticks.ToString();
    object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
    string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";

    //Upload the word document and save to Temp folder
    FileUpload1.SaveAs(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
    object fileName = FileUpload1.PostedFile.FileName;

    //Open the word document in background
    ApplicationClass applicationclass = new ApplicationClass();
    applicationclass.Documents.Open(ref fileName,
                                    ref readOnly,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType, ref isVisible,
                                    ref missingType, ref missingType, ref missingType,
                                    ref missingType, ref missingType);
    applicationclass.Visible = false;
    Document document = applicationclass.ActiveDocument;

    //Save the word document as HTML file
    document.SaveAs(ref htmlFilePath, ref documentFormat, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType, ref missingType, ref missingType,
                    ref missingType);

    //Close the word document
    document.Close(ref missingType, ref missingType, ref missingType);

    //Delete the Uploaded Word File
    File.Delete(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));

    //Read the Html File as Byte Array and Display it on browser
    byte[] bytes;
    using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
    {
        BinaryReader reader = new BinaryReader(fs);
        bytes = reader.ReadBytes((int)fs.Length);
        fs.Close();
    }
    Response.BinaryWrite(bytes);
    Response.Flush();

    //Delete the Html File
    File.Delete(htmlFilePath.ToString());
    foreach (string file in Directory.GetFiles(directoryPath))
    {
        File.Delete(file);
    }
    Directory.Delete(directoryPath);
    Response.End();
}


VB.NET

    Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
        Dim missingType As Object = Type.Missing
        Dim readOnlyObject As Object = True
        Dim isVisible As Object = False
        Dim documentFormat As Object = 8
        Dim randomName As String = DateTime.Now.Ticks.ToString
        Dim htmlFilePath As Object = (Server.MapPath("~/Temp/") _
                    & (randomName + ".htm"))
        Dim directoryPath As String = (Server.MapPath("~/Temp/") _
                    & (randomName + "_files"))

        'Upload the word document and save to Temp folder
        FileUpload1.SaveAs((Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName)))
        Dim fileName As Object = FileUpload1.PostedFile.FileName
        Dim applicationclass As ApplicationClass = New ApplicationClass
        applicationclass.Documents.Open(fileName, readOnlyObject, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType, isVisible, missingType, missingType, missingType, missingType, missingType)
        applicationclass.Visible = False
        Dim document As Document = applicationclass.ActiveDocument

        'Save the word document as HTML file
        document.SaveAs(htmlFilePath, documentFormat, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType, missingType)

        'Close the word document
        document.Close(missingType, missingType, missingType)

        'Delete the Uploaded Word File
        File.Delete((Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName)))
        Dim bytes() As Byte
        Dim fs As FileStream = New FileStream(htmlFilePath.ToString, FileMode.Open, FileAccess.Read)
        Dim reader As BinaryReader = New BinaryReader(fs)
        bytes = reader.ReadBytes(CType(fs.Length, Integer))
        fs.Close()
        Response.BinaryWrite(bytes)
        Response.Flush()
        System.IO.File.Delete(htmlFilePath.ToString)
        Response.End()
    End Sub



Thats it!

Happy Coding
Happy Shoding

Popular Posts