Using OpenXML SDK, I want to insert basic HTML snippets into a Word document.
How would you do this:
Moreover, C# or VB examples are more than welcome :)
Here is another (relatively new) alternative
http://notesforhtml2openxml.codeplex.com/
Well, hard to give general advice, because it depends strongly on your input what is best.
Here's a simple example inserting a paragraph into a DOCX document for each paragraph in an (X)HTML document using OpenXML SDK v2.0 and an XPathDocument:
    void ConvertHTML(string htmlFileName, string docFileName)
    {
        // Create a Wordprocessing document. 
        using (WordprocessingDocument package = WordprocessingDocument.Create(docFileName, WordprocessingDocumentType.Document))
        {
            // Add a new main document part. 
            package.AddMainDocumentPart();
            // Create the Document DOM. 
            package.MainDocumentPart.Document = new Document(new Body());
            Body body = package.MainDocumentPart.Document.Body;
            XPathDocument htmlDoc = new XPathDocument(htmlFileName);
            XPathNavigator navigator = htmlDoc.CreateNavigator();
            XmlNamespaceManager mngr = new XmlNamespaceManager(navigator.NameTable);
            mngr.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
            XPathNodeIterator ni = navigator.Select("//xhtml:p", mngr);
            while (ni.MoveNext())
            {
                body.AppendChild<Paragraph>(new Paragraph(new Run(new Text(ni.Current.Value))));
            }
            // Save changes to the main document part. 
            package.MainDocumentPart.Document.Save();
        }
    }
The example requires your input to be valid XML, otherwise you will get an exception when creating the XPathDocument.
Please note that this is a very basic example not taking any formatting, headings, lists etc into account.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With