向使用HtmlToOpenXml创建的Word文档添加标题不工作
本文关键字:添加 文档 标题 工作 Word HtmlToOpenXml 创建 | 更新日期: 2023-09-27 18:19:19
我使用HtmlToOpenXml从html网站创建Word文档,该部分工作正常。但在我创建Word文档后,我试图向文档添加标题,这部分不起作用。它工作正常,如果我使用Word文档中创建的词,但当我使用下面的代码中创建的Word文档,它不工作。没有向文档添加标题。
我还可以看到如果我打开下面代码创建的文档,大小是2kb,但是如果我在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 });
}
}
我得到了这个链接,你应该能够添加页眉和页脚的方式,它已经很容易在这里描述。
http://msdn.microsoft.com/en-us/library/ee355228 (v = office.12) . aspx