May 1, 2006

Rendering WordML documents in ASP.NET

Here is one easy way:

  1. Go to xmllab.net, get free eXml Web server control and modified Microsoft's WordML2HTML XSLT stylesheet, version 1.3.
  2. Drop eXml control onto a Web form, assign DocumentSource property (WordML document you want to render), TransformSource property(wordml2html-.NET-script.xslt): <xmllab:eXml ID="EXml1" runat="server" DocumentSource="~/TestDocument.xml" TransformSource="~/wordml2html-.NET-script.xslt"/>
  3. Create new folder to store external images
  4. 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");        
    }
    
Done.

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!

...