向使用HtmlToOpenXml创建的Word文档添加标题不起作用

时间:2014-08-23 07:33:35

标签: c# .net openxml-sdk

我正在使用HtmlToOpenXml从html网站创建Word文档,该部分工作正常。但在我创建Word文档后,我正在尝试向文档添加标题,该部分无效。如果我使用在Word中创建的Word文档,它工作正常,但是当我使用在下面的代码中创建的Word文档时,它不起作用。没有标题添加到文档中。

我还可以看看我是否打开下面代码创建的文档,大小是2 kb,但是如果我在Word中打开它并保存它,那么文档的大小是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 });
                    }
                }

1 个答案:

答案 0 :(得分:0)

我收到了这个链接,您应该能够轻松地添加页眉和页脚。

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