Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert HTML into OpenXML Word Document (.Net)

Using OpenXML SDK, I want to insert basic HTML snippets into a Word document.

How would you do this:

  • Manipulating XML directly ?
  • Using an XSLT ?
  • using AltChunk ?

Moreover, C# or VB examples are more than welcome :)

like image 671
Nico Avatar asked Oct 09 '08 14:10

Nico


2 Answers

Here is another (relatively new) alternative

http://notesforhtml2openxml.codeplex.com/

like image 136
Riko Avatar answered Sep 22 '22 16:09

Riko


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.

like image 34
Dirk Vollmar Avatar answered Sep 19 '22 16:09

Dirk Vollmar