In this article I will explain with an example, how to display
Word document (DOC and DOCX) files in
Web Page (View) using
Interop Word in
ASP.Net MVC.
The
Word document will be first uploaded, then it will be converted to
HTML using
Microsoft Office Interop Library and finally the converted
HTML it will be displayed in View in
ASP.Net MVC.
Adding reference of Microsoft Office Interop Library
In order to add reference of the Microsoft Office Interop Library, right click on the Project in Solution Explorer and click Add Reference.
Then from the following Window, please select Microsoft Office Interop.Word assembly.
Namespaces
You will need to import the following namespaces.
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action method gets called when a
Word Document file is selected and the
Submit Button is clicked, and it gets the
uploaded file in the
HttpPostedFileBase parameter.
The uploaded
Word Document file is first saved into a Folder named
Temp within the Project
Folder and then the
Word Document file is converted into
HTML string using
Microsoft Office Interop Library.
Finally the
HTML string will be assigned to a
ViewBag object.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
object documentFormat = 8;
string randomName = DateTime.Now.Ticks.ToString();
object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
object fileSavePath = Server.MapPath("~/Temp/") + Path.GetFileName(postedFile.FileName);
//If Directory not present, create it.
if (!Directory.Exists(Server.MapPath("~/Temp/")))
{
Directory.CreateDirectory(Server.MapPath("~/Temp/"));
}
//Upload the word document and save to Temp folder.
postedFile.SaveAs(fileSavePath.ToString());
//Open the word document in background.
_Application applicationclass = new Application();
applicationclass.Documents.Open(ref fileSavePath);
applicationclass.Visible = false;
Document document = applicationclass.ActiveDocument;
//Save the word document as HTML file.
document.SaveAs(ref htmlFilePath, ref documentFormat);
//Close the word document.
document.Close();
//Read the saved Html File.
string wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());
//Loop and replace the Image Path.
foreach (Match match in Regex.Matches(wordHTML, "<v:imagedata.+?src= [\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase))
{
wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);
}
//Delete the Uploaded Word File.
System.IO.File.Delete(fileSavePath.ToString());
ViewBag.WordHtml = wordHTML;
return View();
}
}
View
Form for Uploading the Word Document file
This
Form consists of an
HTML FileUpload and a Submit
Button. When the Button is clicked, the
Index Action method for handling
POST operation will be called.
Displaying the Word Document in View
The
ViewBag object is displayed using the
Html.Raw Helper function.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="postedFile" />
<input type="submit" value="submit" />
}
<hr />
<div>@Html.Raw(ViewBag.WordHtml)</div>
</body>
</html>
Screenshot
Downloads