Here is one easy way:
- Go to xmllab.net, get free eXml Web server control and modified Microsoft's WordML2HTML XSLT stylesheet, version 1.3.
- Drop eXml control onto a Web form, assign DocumentSource property (WordML document you want to render), TransformSource property(wordml2html-.NET-script.xslt):
- Create new folder to store external images
- In code behind allow XSLT scripting and pass couple XSLT parameters - real path to above image directory and its virtual name:
protected void Page_Load(object sender, EventArgs e) { EXml1.XsltSettings = System.Xml.Xsl.XsltSettings.TrustedXslt; EXml1.TransformArgumentList = new System.Xml.Xsl.XsltArgumentList(); EXml1.TransformArgumentList.AddParam( "base-dir-for-images", "", MapPathSecure("~/images")); EXml1.TransformArgumentList.AddParam( "base-virtual-dir-for-images", "", "images"); }
I had to add these two parameters so the WordML2HTML stylesheet could export images there and then refer to exported images in HTML. If you don't pass these parameters images will be exported into current directory - while that's ok when running WordML2HTML transformation in a command line, that's bad idea for ASP.NET environment.
Enjoy!
We're using the method described here and it works great! However, the transform doesn't seem to work for XML documents created from Word 2007. Is there a newer version of the XSLT available, or a modification that we can make to get this working?
Thanks,
Dan
Leif, you should pass parameters named "base-dir-for-images" and "base-virtual-dir-for-images", not "realpath" and "~/Images" (latter one causes the problem - XSLT parameter name cannot contain ~.
EXml1.XsltSettings = System.Xml.Xsl.XsltSettings.TrustedXslt;
EXml1.TransformArgumentList = new System.Xml.Xsl.XsltArgumentList();
string realpath = @"d:\Documents and Settings\Leif.LEIF-YICID5TUMG\my documents\visual studio 2005\WebPractise\Images";
EXml1.TransformArgumentList.AddParam("realpath", "", MapPathSecure("~/Images"));
EXml1.TransformArgumentList.AddParam("~/Images", "", "Images");
------------------
Hi Oleg
The work you have done in this area is quite impressive. The xslt script is something that has incredible potential. I am currently developing a dynamic document management system and pulling word xml (effectively claues) in from sql server 2005 into ASP.Net. All documents work fine but for one with a 'draft' image embedded in the header.
Whenever I use this code, I get the following error:
System.Xml.XmlException: The '~' character, hexadecimal value 0x7E, cannot be included in a name.
Also, the image is placed as a hidden object outside all other containers on the web page. Any thoughts on what is going wrong?
Geroge, taker a look at Microsoft VSTO - akaik it allows you to deal with Word programmatically, e.g. look at http://www.intranetjournal.com/articles/200507/ij_07_20_05a.html
Hi Oleg,
Thank you very much for responding!
Sorry that I should write clearly about why I need both direction conversions.
I have a project that need to be able to edit the report content and save it back to server. On server the report need to be wordML format stored in database (reason is easy for printing and image). So I try to find a solution. On the market there are a lot of web based HTML editor but no any web based WordML editor. If I use html editor I need to convert wordML from database to HTML format and display it on HTML editor on browser (I have done this conversion from your code). But after editing by user on browser I need to transfer back to wordML and store in the database. This is why I need to convert both directions in C# code.
I do find some Web based XML editor but they don't support the WordML.
Thank you very much for your time again!
Regards!
George
George, you can get resulting HTML by running transformation in code. Download WordML2HTML XSLT stylesheet and run it against your WordML document using XslCompiledTransform class.
Talking about backward conversion - are you sure you need it? Word can open HTML just fine actually. If you do need it - take a look at Microsoft VSTO - probably you can open HTML and save it as WordML via Word API.
Hi all,
I have used eXml Web server control to convert the WordMl to HTML and it works fine. It generate the HTML code on the browser. But I can't get the document content in C# code from this control and it is empty. I need this document content to edit by user in the HTML editor on browser. Can any body tell me how to get the document content from this eXml control?
Also I would like to know how to convert from HTML to WordML? Any idea? If so we can use HTML editor to edit the WordML on the web browser? How exciting?
many thanks!
George
George.wang@axegroup.com.au