Heade and footer is missing

Hi,

I am using the below code to generate the word document with header and footer but header and footer is missing in the generated document. Please find the code and the attached generated document and give me a quick solution.

Code:

var sectionCount = 0;
var strFilePath = Server.MapPath("…") + @"\Documents" + Convert.ToString(DateTime.Now.Millisecond) + “.doc”;

var doc = new Document();
var builder = new DocumentBuilder(doc);

doc.Sections.Clear();

for (var i = 0; i < 10; i++)
{
var newSec1 = new Section(doc);
doc.Sections.Add(newSec1);
builder.MoveToSection(sectionCount);
builder.InsertBreak(BreakType.PageBreak);

sectionCount += 1;

builder.InsertHtml("Page " + i);
}

builder.PageSetup.DifferentFirstPageHeaderFooter = true;

builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.ParagraphFormat.TabStops.Add(474.9, TabAlignment.Right, TabLeader.None);
builder.Write("Left header " + ControlChar.Tab + “Right header”);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.TabStops.Add(474.9, TabAlignment.Right, TabLeader.None);
builder.Write("Left header " + ControlChar.Tab + “Right header”);

builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Page “);
builder.InsertField(“PAGE”);
builder.Write(” of ");
builder.InsertField(“NUMPAGES”);

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Page “);
builder.InsertField(“PAGE”);
builder.Write(” of ");
builder.InsertField(“NUMPAGES”);

doc.UpdatePageLayout();
doc.UpdateFields();

doc.Save(strFilePath);

Regards,
Kavi

Hi Kavi,

Thanks
for your inquiry. 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. There can only be one
HeaderFooter or each HeaderFooterType in a Section. You can add and remove HeaderFooter from a section.

Please use the DocumentBuilder.MoveToSection method to move the cursor to the beginning of the body in a specified section. Please check the following highlighted code below.


var sectionCount = 0;

var doc = new Document();

var builder = new DocumentBuilder(doc);

doc.Sections.Clear();

for (var i = 0; i < 10; i++)

{

var newSec1 = new Section(doc);

doc.Sections.Add(newSec1);

builder.MoveToSection(sectionCount);

//builder.InsertBreak(BreakType.PageBreak);

sectionCount += 1;

builder.InsertHtml("Page " + i);

}

builder.MoveToSection(0);

builder.PageSetup.DifferentFirstPageHeaderFooter = true;

builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);

builder.ParagraphFormat.TabStops.Add(474.9, Aspose.Words.TabAlignment.Right, TabLeader.None);

builder.Write("Left header " + ControlChar.Tab + "Right header");

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.ParagraphFormat.TabStops.Add(474.9, Aspose.Words.TabAlignment.Right, TabLeader.None);

builder.Write("Left header " + ControlChar.Tab + "Right header");

builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);

builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;

builder.Write("Page ");

builder.InsertField("PAGE");

builder.Write(" of ");

builder.InsertField("NUMPAGES");

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;

builder.Write("Page ");

builder.InsertField("PAGE");

builder.Write(" of ");

builder.InsertField("NUMPAGES");

doc.UpdatePageLayout();

doc.UpdateFields();

doc.Save(MyDir + "Out.doc");