none
Adding a html content to the body of the word docunemt using openxml sdk RRS feed

  • Question

  • Hi,

        I want to append a sample html content to the body section of the word document using openxml.Please find the sample content for reference

    "<html> <head> <title></title>"

     

    +

     

    "</head> <body> Looks how cool is <font size='x-large'><b>Open Xml</b></font>. Now with <font color='red'><u>HtmlToOpenXml</u></font>,"

    +

     

    "it nevers been so easy to convert html. <p> If you like it, add me a rating on <a href='http://notesforhtml2openxml.codeplex.com'>codeplex</a>"

    +

     

    "</p> <hr> </body> </html>"

    when i tried to add the content,it displayed as a plain text and not as html content.Can someone provide a solution or code for this.

     

    Regards

    R.Rajesh

    Thursday, January 12, 2012 7:40 AM

All replies

  • Hi Rajesh

    The HTML needs to be converted or transformed to the Word Open XML vocabulary then inserted into the correct place in the document.xml file. I imagine there are third-party tools available (from AltSoft, for example).

    In the OpenXMLDeveloper forum I find a discussion that dates back a couple of years about a tool on codeplex:
    http://openxmldeveloper.org/discussions/formats/f/13/p/2466/8169.aspx#8169

    It appears development on the tool as continued and that it works.


    Cindy Meister, VSTO/Word MVP
    Thursday, January 12, 2012 3:57 PM
    Moderator
  • For a Word document, you can add your html in an AlternativeFormatImportPart.
    Thursday, January 12, 2012 8:51 PM
  • Can you please provide the code using AlternativeFormatImport to support word document generation in both 2003 and 2007 using openxml,Currentll i am using one which support only 2007 format ,when trying to create in 2003 ,i am getting an error.

    Regards

    R.Rajesh

    Friday, January 13, 2012 4:53 AM
  • I don't believe it is supported in Word 2003, just 2007 and later. You can check the OpenXML documentation, which includes sample code.
    Friday, January 13, 2012 10:58 AM
  • Hi,

       Can you please provide the code using AlternativeFormatImport to support word document generation in both ms office 2003(msoffice 2003 and 2007 compatibile pack installed) and 2007 using openxml.

    altchunk class is generating the error.why this is not supported in msoffice 2003 machine(i.e 2007 compatibilty pack also installed)

    This is the piece of code i am using to append the html content to word document

    void

     

    AppendAltChunk(WordprocessingDocument doc, string altChunkId, string

    html)

    {

     

    MainDocumentPart mainPart = doc.MainDocumentPart;

     

    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId);

    using (System.IO.Stream chunkStream = chunk.GetStream(System.IO.FileMode.Create, System.IO.FileAccess

    .Write))

     

    using (System.IO.StreamWriter stringStream = new System.IO.StreamWriter

    (chunkStream))

    stringStream.Write(html);

     

    AltChunk altChunk = new AltChunk

    ();

    altChunk.Id = altChunkId;

     

    OpenXmlElement

    last = doc.MainDocumentPart.Document

    .Body

    .Elements()

    .Last()

    .LastOrDefault(e => e

    is Paragraph || e is AltChunk

    );

     

    if (last == null

    )

    doc.MainDocumentPart.Document.Body.InsertAt(altChunk, 0);

     

    else

    last.InsertAfterSelf(altChunk);

    }

     Can any one figure out the solution.

    Regards

    R.Rajesh

     

     

    Monday, January 16, 2012 9:57 AM
  • This code successfully incorporates html code from an html document into a Word 2010 document. If you're still trying to get it working in Word 2003, check the documentation because I don't think it is supported.
    <pre>        protected string HtmlFragment { get; set; }
            protected string HtmlDocument
            {
                get
                {
                    if (this.HtmlFragment.StartsWith("<html>", StringComparison.CurrentCultureIgnoreCase))
                        return this.HtmlFragment;
                    else
                        return string.Format(@"<html><head/><body>{0}</body></html>", this.HtmlFragment);
                }
            }
    
    

    public void AppendToWordDocument(string documentFullPathAndName, int altChunkIdNumber) { using (WordprocessingDocument myDoc = WordprocessingDocument.Open(documentFullPathAndName, true)) { string altChunkId = string.Format("AltChunkId{0}", altChunkIdNumber); MainDocumentPart mainPart = myDoc.MainDocumentPart; AlternativeFormatImportPart chunk = mainPart .AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId); using (StreamWriter stringStream = new StreamWriter(chunk.GetStream(FileMode.Create, FileAccess.Write))) stringStream.Write(this.HtmlDocument); XElement altChunk = new XElement(w + "altChunk", new XAttribute(r + "id", altChunkId)); XDocument mainDocumentXDoc = GetXDocument(myDoc); mainDocumentXDoc.Root .Element(w + "body") .Elements(w + "p") .Last() .AddAfterSelf(altChunk); SaveXDocument(myDoc, mainDocumentXDoc); } }
    Tuesday, January 17, 2012 9:38 PM