<p>Word 2007 saves its documents in .docx format which is really a zip file with a bunch of stuff in it including an xml file with the document.</p>

<p>I want to be able to take a .docx file and drop it into a folder in my asp.net web app and have the code open the .docx file and render the (xml part of the) document as a web page.</p>

<p>I've been searching the web for more information on this but so far haven't found much. My questions are:</p>

<ol>
<li>Would you (a) use XSLT to transform the XML to HTML, or (b) use xml manipulation libraries in .net (such as XDocument and XElement in 3.5) to convert to HTML or (c) other?</li>
<li>Do you know of any open source libraries/projects that have done this that I could use as a starting point? </li>
</ol>

<p>Thanks!</p>

[{'answer_id': 55117, 'author': 'Adam Lerman', 'author_id': 673, 'author_profile': 'https://Stackoverflow.com/users/673', 'pm_score': 3, 'selected': True, 'text': '<p>Try this <a href="http://blog.maartenballiauw.be/post/2008/01/11/Preview-Word-files-(docx)-in-HTML-using-ASPNET-OpenXML-and-LINQ-to-XML.aspx" rel="nofollow noreferrer">post</a>? I don\'t know but might be what you are looking for.</p>\n'}, {'answer_id': 55121, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 2, 'selected': False, 'text': '<p>Word 2007 has an API that you can use to convert to HTML. Here\'s a post that talks about it <a href="http://msdn.microsoft.com/en-us/magazine/cc163526.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/cc163526.aspx</a>. You can find documentation around the API, but I remember that there is a convert to HTML function in the API.</p>\n'}, {'answer_id': 12257270, 'author': 'raghava', 'author_id': 1645149, 'author_profile': 'https://Stackoverflow.com/users/1645149', 'pm_score': 1, 'selected': False, 'text': '<p>This code will helps to convert <code>.docx</code> file to text</p>\n\n<pre><code>function read_file_docx($filename){\n\n    $striped_content = \'\';\n    $content = \'\';\n\n    if(!$filename || !file_exists($filename)) { echo "sucess";}else{ echo "not sucess";}\n\n    $zip = zip_open($filename);\n\n    if (!$zip || is_numeric($zip)) return false;\n\n    while ($zip_entry = zip_read($zip)) {\n\n        if (zip_entry_open($zip, $zip_entry) == FALSE) continue;\n\n        if (zip_entry_name($zip_entry) != "word/document.xml") continue;\n\n        $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));\n\n        zip_entry_close($zip_entry);\n    }// end while\n\n    zip_close($zip);\n\n    //echo $content;\n    //echo "&lt;hr&gt;";\n    //file_put_contents(\'1.xml\', $content);     \n\n    $content = str_replace(\'&lt;/w:r&gt;&lt;/w:p&gt;&lt;/w:tc&gt;&lt;w:tc&gt;\', " ", $content);\n    $content = str_replace(\'&lt;/w:r&gt;&lt;/w:p&gt;\', "\\r\\n", $content);\n     //header("Content-Type: plain/text");\n\n\n    $striped_content = strip_tags($content);\n\n\n      $striped_content = preg_replace("/[^a-zA-Z0-9\\s\\,\\.\\-\\n\\r\\t@\\/\\_\\(\\)]/","",$striped_content);\n\n    echo nl2br($striped_content); \n}\n</code></pre>\n'}, {'answer_id': 27985333, 'author': 'messed-up', 'author_id': 4421126, 'author_profile': 'https://Stackoverflow.com/users/4421126', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m using Interop. It is somewhat problamatic but works fine in most of the case.</p>\n\n<pre><code>using System.Runtime.InteropServices;\nusing Microsoft.Office.Interop.Word;\n</code></pre>\n\n<p>This one returns the list of html converted documents\' path</p>\n\n<pre><code>public List&lt;string&gt; GetHelpDocuments()\n    {\n\n        List&lt;string&gt; lstHtmlDocuments = new List&lt;string&gt;();\n        foreach (string _sourceFilePath in Directory.GetFiles(""))\n        {\n            string[] validextentions = { ".doc", ".docx" };\n            if (validextentions.Contains(System.IO.Path.GetExtension(_sourceFilePath)))\n            {\n                sourceFilePath = _sourceFilePath;\n                destinationFilePath = _sourceFilePath.Replace(System.IO.Path.GetExtension(_sourceFilePath), ".html");\n                if (System.IO.File.Exists(sourceFilePath))\n                {\n                    //checking if the HTML format of the file already exists. if it does then is it the latest one?\n                    if (System.IO.File.Exists(destinationFilePath))\n                    {\n                        if (System.IO.File.GetCreationTime(destinationFilePath) != System.IO.File.GetCreationTime(sourceFilePath))\n                        {\n                            System.IO.File.Delete(destinationFilePath);\n                            ConvertToHTML();\n                        }\n                    }\n                    else\n                    {\n                        ConvertToHTML();\n                    }\n\n                    lstHtmlDocuments.Add(destinationFilePath);\n                }\n            }\n\n\n        }\n        return lstHtmlDocuments;\n    }\n</code></pre>\n\n<p>And this one to convert doc to html.</p>\n\n<pre><code>private void ConvertToHtml()\n    {\n        IsError = false;\n        if (System.IO.File.Exists(sourceFilePath))\n        {\n            Microsoft.Office.Interop.Word.Application docApp = null;\n            string strExtension = System.IO.Path.GetExtension(sourceFilePath);\n            try\n            {\n                docApp = new Microsoft.Office.Interop.Word.Application();\n                docApp.Visible = true;\n\n                docApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;\n                object fileFormat = WdSaveFormat.wdFormatHTML;\n                docApp.Application.Visible = true;\n                var doc = docApp.Documents.Open(sourceFilePath);\n                doc.SaveAs2(destinationFilePath, fileFormat);\n            }\n            catch\n            {\n                IsError = true;\n            }\n            finally\n            {\n                try\n                {\n                    docApp.Quit(SaveChanges: false);\n\n                }\n                catch { }\n                finally\n                {\n                    Process[] wProcess = Process.GetProcessesByName("WINWORD");\n                    foreach (Process p in wProcess)\n                    {\n                        p.Kill();\n                    }\n                }\n                Marshal.ReleaseComObject(docApp);\n                docApp = null;\n                GC.Collect();\n            }\n        }\n    }\n</code></pre>\n\n<p>The killing of the word is not fun, but can\'t let it hanging there and block others, right?</p>\n\n<p>In the web/html i render html to a iframe.</p>\n\n<p>There is a dropdown which contains the list of help documents. Value is the path to the html version of it and text is name of the document.</p>\n\n<pre><code>private void BindHelpContents()\n    {\n        List&lt;string&gt; lstHelpDocuments = new List&lt;string&gt;();\n        HelpDocuments hDoc = new HelpDocuments(Server.MapPath("~/HelpDocx/docx/"));\n        lstHelpDocuments = hDoc.GetHelpDocuments();\n        int index = 1;\n        ddlHelpDocuments.Items.Insert(0, new ListItem { Value = "0", Text = "---Select Document---", Selected = true });\n\n        foreach (string strHelpDocument in lstHelpDocuments)\n        {\n            ddlHelpDocuments.Items.Insert(index, new ListItem { Value = strHelpDocument, Text = strHelpDocument.Split(\'\\\\\')[strHelpDocument.Split(\'\\\\\').Length - 1].Replace(".html", "") });\n            index++;\n        }\n        FetchDocuments();\n\n    }\n</code></pre>\n\n<p>on selected index changed, it is renedred to frame</p>\n\n<pre><code>    protected void RenderHelpContents(object sender, EventArgs e)\n    {\n        try\n        {\n            if (ddlHelpDocuments.SelectedValue == "0") return;\n            string strHtml = ddlHelpDocuments.SelectedValue;\n            string newaspxpage = strHtml.Replace(Server.MapPath("~/"), "~/");\n            string pageVirtualPath = VirtualPathUtility.ToAbsolute(newaspxpage);// \n            documentholder.Attributes["src"] = pageVirtualPath;\n        }\n        catch\n        {\n            lblGError.Text = "Selected document doesn\'t exist, please refresh the page and try again. If that doesn\'t help, please contact Support";\n        }\n    }\n</code></pre>\n'}, {'answer_id': 30153764, 'author': 'Michael Williamson', 'author_id': 49381, 'author_profile': 'https://Stackoverflow.com/users/49381', 'pm_score': 2, 'selected': False, 'text': '<p>I wrote <a href="https://github.com/mwilliamson/mammoth.js" rel="nofollow">mammoth.js</a>, which is a JavaScript library that converts docx files to HTML. If you want to do the rendering server-side in .NET, there is also a .NET version of Mammoth <a href="https://www.nuget.org/packages/Mammoth" rel="nofollow">available on NuGet</a>.</p>\n\n<p>Mammoth tries to produce clean HTML by looking at semantic information -- for instance, mapping paragraph styles in Word (such as <code>Heading 1</code>) to appropriate tags and style in HTML/CSS (such as <code>&lt;h1&gt;</code>). If you want something that produces an exact visual copy, then Mammoth probably isn\'t for you. If you have something that\'s already well-structured and want to convert that to tidy HTML, Mammoth might do the trick.</p>\n'}]
['https://Stackoverflow.com/questions/55113', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1463/']