wordpress hit counter
Welcome to OpenXML Developer Sign in | Join | Help

Display HTML formatted text as HTML

Last post 07-21-2009, 10:07 AM by prosantak. 5 replies.
Sort Posts: Previous Next
  •  06-10-2009, 3:44 PM 5442

    Display HTML formatted text as HTML

    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.
  •  06-11-2009, 5:48 AM 5482 in reply to 5442

    Re: Display HTML formatted text as HTML

    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)

  •  06-16-2009, 3:44 AM 6239 in reply to 5442

    Re: Display HTML formatted text as HTML

    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);
    }
    }

  •  07-07-2009, 1:26 PM 7118 in reply to 6239

    Re: Display HTML formatted text as HTML


    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

  •  07-16-2009, 11:28 AM 7160 in reply to 7118

    Re: Display HTML formatted text as HTML

    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.
  •  07-21-2009, 10:07 AM 7196 in reply to 7118

    Re: Display HTML formatted text as HTML

    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);

    }
View as RSS news feed in XML