Hi Jeevan,
Thanks for your inquiry. Please note that you can not insert header/footer by using DocumentBuilder.InsertHtml method.
The HeaderFooter class represents a container for the header or footer text of a section. HeaderFooter is a section-level node and can only be a child of Section. So, header and footer repeat on each page of a section. There can only be one HeaderFooter or each HeaderFooterType in a Section.
You can insert header/footer after calling DocumentBuilder.InsertHtml method and insert page number field in footer of a section as shown in following code snippet.
Document doc = new
Document();<o:p></o:p>
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(
"Paragraph
right
" +
"Implicit paragraph left"
+
"Div
center
" +
"Heading 1
left.
");
InsertHeaderFooter(builder.CurrentSection, HeaderFooterType.FooterPrimary);
InsertHeaderFooter(builder.CurrentSection, HeaderFooterType.HeaderFirst);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
//Set start number
builder.PageSetup.PageStartingNumber = 1;
//Page numbers will be restarted for
each section
builder.PageSetup.RestartPageNumbering = true;
//Insert PAGE field into the header
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Page
");
builder.InsertField("PAGE",
string.Empty);
builder.MoveToDocumentEnd();
//Insert page break
builder.InsertBreak(BreakType.PageBreak);
//Insert section break
builder.InsertBreak(BreakType.SectionBreakNewPage);
doc.Save(MyDir + "Out.docx");
private static void InsertHeaderFooter(Section
sect, HeaderFooterType headerType)
{
HeaderFooter header =
sect.HeadersFooters[headerType];
if (header == null)
{
// There is no header of the specified type in the current
section, create it.
header = new HeaderFooter(sect.Document,
headerType);
sect.HeadersFooters.Add(header);
}
}
Section can have one Body and maximum one
HeaderFooter of each
HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section. Please read following documentation link for your kind reference.
Hope this helps you. Please let us know if you have any more queries.