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 });
}
}