0

I am using HtmlToOpenXml to create a Word doc from a html site, that part is working fine. but after I created the Word doc, I am trying to add a header to the document, that part isn't working. It is working fine if I use a Word doc created in Word, but when I use the Word doc created in the code below, it isn't working. No header is added to the document.

I can also see if I open the doc created by the code below, the size is 2 kb but if i open it in Word and saving it afterwards the size of the document is 11kb.

            const string filename = "c:/temp/test.docx";
            string html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">                                                                  " +
                           " <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>";

            if (File.Exists(filename)) File.Delete(filename);

            using (MemoryStream generatedDocument = new MemoryStream())
            {
                using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = package.MainDocumentPart;
                    if (mainPart == null)
                    {
                        mainPart = package.AddMainDocumentPart();
                    }


                    new Document(new Body()).Save(mainPart);


                    HtmlConverter converter = new HtmlConverter(mainPart);
                    Body body = mainPart.Document.Body;

                    var paragraphs = converter.Parse(html);
                    for (int i = 0; i < paragraphs.Count; i++)
                    {
                        body.Append(paragraphs[i]);
                    }



                    mainPart.Document.Save();
                }

                File.WriteAllBytes(filename, generatedDocument.ToArray());

                string filepathFrom = @"C:\temp\peter.docx";

                // Replace header in target document with header of source document.
                using (WordprocessingDocument
                    wdDoc = WordprocessingDocument.Open(filename, true))
                {
                    MainDocumentPart mainPart = wdDoc.MainDocumentPart;

                    // Delete the existing header part.
                    mainPart.DeleteParts(mainPart.HeaderParts);

                    // Create a new header part.
                    DocumentFormat.OpenXml.Packaging.HeaderPart headerPart =
                mainPart.AddNewPart<HeaderPart>();

                    // Get Id of the headerPart.
                    string rId = mainPart.GetIdOfPart(headerPart);

                    // Feed target headerPart with source headerPart.
                    using (WordprocessingDocument wdDocSource =
                        WordprocessingDocument.Open(filepathFrom, true))
                    {
                        DocumentFormat.OpenXml.Packaging.HeaderPart firstHeader =
                wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();

                        wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();

                        if (firstHeader != null)
                        {
                            headerPart.FeedData(firstHeader.GetStream());
                        }
                    }

                    // Get SectionProperties and Replace HeaderReference with new Id.
                    IEnumerable<DocumentFormat.OpenXml.Wordprocessing.SectionProperties> sectPrs =
                mainPart.Document.Body.Elements<SectionProperties>();
                    foreach (var sectPr in sectPrs)
                    {
                        // Delete existing references to headers.
                        sectPr.RemoveAllChildren<HeaderReference>();

                        // Create the new header reference node.
                        sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId });
                    }
                }
PNR
  • 555
  • 3
  • 12
  • 26

1 Answers1

0

I got this link and you should be able to add header and footer that way it has been described here easily.

http://msdn.microsoft.com/en-us/library/ee355228(v=office.12).aspx

codebased
  • 6,945
  • 9
  • 50
  • 84
  • Thanks very much for your reply! It was almost what i needed. But i need to just add one header and a footer for the Whole document, and then add some content, that can fill several pages. So I don't have to manage each page. How is that possible? Thanks very much in advance! – PNR Sep 12 '14 at 16:59