I'm generating a word docx file from a html string and using various articles I've found how to add simple headers and footers to that document. What I now need to do is generate the header from a html string too but I'm struggling to find an example of how this is done. The author of this question: Export docx/doc First Header and Footer as docx File Using openXML says he has done it but unfortunately he hasn't posted an example.
My code below successfully adds a header but as you can see from the code it will only add the html as text in the header, whereas I want to pass in a html string and that to appear in the header as formatted html.
Has anyone done this?
static void AddHeaderPart(MainDocumentPart mainPart, string headerHtml, Encoding encoding)
{
if (mainPart == null || string.IsNullOrEmpty(headerHtml))
{return;}
// Create a new header part.
HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
// Get Id of the headerPart.
string rId = mainPart.GetIdOfPart(headerPart);
// Call GenerateHeaderPartContent
GenerateHeaderPartContent(mainPart, headerPart, headerHtml, encoding);
// Get SectionProperties and Replace HeaderReference with new Id.
IEnumerable<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, Type = HeaderFooterValues.Default });
}
}
static void GenerateHeaderPartContent(MainDocumentPart mainPart, HeaderPart headerPart, string headerHtml, Encoding encoding)
{
Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
//This adds the headerHtml as text - how to add it as html?
Run run1 = new Run();
Text text1 = new Text();
text1.Text = headerHtml;
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
header1.Append(paragraph1);
headerPart.Header = header1;
}
So the code below uses an altChunk to add html into the header. It's not perfect as I get validation errors when I use the OpenXmlValidator to validate the generated document although the doc still opens ok in word but I'm posting it here as an answer as it does do what I asked above & it might help others:
static void GenerateHeaderPartContent(MainDocumentPart mainPart, HeaderPart headerPart, string headerHtml)
{
Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
paragraph1.Append(paragraphProperties1);
string altChunkId = "AltChunkId1";
AlternativeFormatImportPart chunk = headerPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, altChunkId);
// Note that headerHtml should be full html, not just a snippet
// eg. <html><h1>My Header</h1></html> is OK
// <h1>My Header</h1> is not OK
MemoryStream ms = new MemoryStream(new UTF8Encoding(true).GetPreamble().Concat(Encoding.UTF8.GetBytes(headerHtml)).ToArray());
chunk.FeedData(ms);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
Run run1 = new Run();
RunProperties runProps = new RunProperties();
NoProof noProof1 = new NoProof();
runProps.Append(noProof1);
run1.Append(runProps);
run1.Append(altChunk);
paragraph1.Append(run1);
header1.Append(paragraph1);
headerPart.Header = header1;
}