wordpress hit counter
Re: Display HTML formatted text as HTML - WordprocessingML - Formats - OpenXML Developer

Re: Display HTML formatted text as HTML

Formats

Discussions about working with different Open XML Formats

Display HTML formatted text as HTML

  • rated by 0 users
  • This post has 5 Replies |
  • 1 Follower
  • Hi everyone, new to forums, new to OpenXML.  I have an issue where I'm generating a table, the text that I am adding to a cell is HTML text. How can I instruct word that it is to display this as HTML?

    I am building this using C#, so a .NET example would be ideal, but not necessary.

    Using OpenXML 2.0 SDK. , Word 2007. C# 3.5

    Thanks.
  • Hi,

    Sorry that I'm not clear about your question. What do you mean by "display this as HTML"? Do you want to display the content with the tags or just keep the formats?

    Also, could you please tell me your user scenario? are you generating the documents from template with HTML chunk? Where do you get the HTML chunk? How will the final documents be used typically? for print, send, view...? Do you need to convert the documents to other file formats, pdf, odf...? Are you using some other technologies, database, sharepoint, silverlight...?

    I want to understand how the users use the SDK and what solutions are built upon it, thanks:)

     

    Shuangshuang (MS)

  • Hi

    You can use this code

    /// <summary>
    // Insert HTML part
    /// </summary>

    private void InsertHTMLPart(XmlNode oNode, string html)
    {
    if (html.Length > 0)
    {
    string altChunkId = String.Format("AltChunkId_{0}", bookmarkCounter);
    AlternativeFormatImportPart chunk = oMainDocPartGlobal.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId);

    using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
    using (StreamWriter stringStream = new StreamWriter(chunkStream))
    stringStream.Write(html);

    string altChunktemplate = String.Format("<w:altChunk r:id=\"{0}\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" />", altChunkId);

    XmlDocumentFragment oDocFrag = mainDocument.CreateDocumentFragment();
    oDocFrag.InnerXml = altChunktemplate;

    if (oNode.InnerXml.IndexOf("w:tc") > 0)
    {
    XmlNode tcNode = oNode.SelectSingleNode("w:tc//w:p", mainNamespaceMrg);
    if (tcNode != null)
    {
    oDocFrag.InnerXml = oDocFrag.InnerXml +
    "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:r xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:t xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"></w:t></w:r></w:p>";
    tcNode.ParentNode.InsertAfter(oDocFrag, tcNode);
    }
    }
    else
    oNode.ParentNode.InsertAfter(oDocFrag, oNode);
    }
    }


  • Hey there,

    I am interested in this example you posted...however, questions I have:
    • How is the variable, mainNamespaceMrg defined?,
    • How are you defining the bookmarkCounter?,
    • How is the oMainDocPartGlobal get instantiated using the SDK?
    Maybe you can post a full example?

    Our automated process to insert HTML into a DOCX actually embeds an HTML file containing the contents and associates the DOCX to the HTML (rels file). The problem with this is that users using Office 2003 are unable to open this file even with the compatibility pack installed.

    Do you know if going this route (without the physical HTM file reference) works with the compatibility pack and Word 2003 users?

    Thanks,
    TJ

  • I've been able to use Aspose.Words to import HTML, convert it to Word, then grab the converted HTML (now OpenXML), and push it back into my Word file.  I was trying to use AltChunk initially, but I just wasn't getting the conversion working correctly in many situations.  This was a lot faster and more straight-forward.
  • HI TJ

    Here is the example....

    private MainDocumentPart oMainDocPartGlobal = null;
    private int bookmarkCounter = 0;
    XmlNamespaceManager mainNamespaceMrg = null;

    using (WordprocessingDocument wdPackage = WordprocessingDocument.Open(fileNameWithPath, true))
    {
    MainDocumentPart oMainDocPart = wdPackage.MainDocumentPart;

    oMainDocPartGlobal = oMainDocPart;


    // You must manage namespaces to perform XML XPath queries.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
    nsManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");


    if (!(oMainDocPart == null))
    {

    //XmlNode rootNode = null;


    // Load the contents of the custom properties part into an XML document.
    XmlDocument oMainDoc = new XmlDocument(nt);
    oMainDoc.Load(oMainDocPart.GetStream(FileMode.Open, FileAccess.Read));

    mainNamespaceMrg = nsManager;

    //Your HTML insertion goes here

    //save doc
    oMainDoc.Save(oMainDocPart.GetStream(FileMode.Create, FileAccess.Write));
    }

    wdPackage.Validate(null);

    }
Page 1 of 1 (6 items)