The following code prepends the footer on the first section of a doc. However the footer on section 2 just gets overwritten.
How can I prepend the footer to every section?
private void InsertFooterWatermark(Aspose.Words.Document doc, string text, string statusName, string userName, string timezoneId)
{
bool footerInserted = false;
foreach (Section section in doc)
{
if (section.HeadersFooters.Count == 0)
{
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(doc.Sections.IndexOf(section));
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
footerInserted = InsertFooterText(text, statusName, userName, builder);
}
else
{
foreach (HeaderFooter footer in section.HeadersFooters)
{
if (IsFooter(footer))
{
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(doc.Sections.IndexOf(section));
builder.MoveToHeaderFooter(footer.HeaderFooterType);
footerInserted = InsertFooterText(text, statusName, userName, builder);
}
}
if (!footerInserted)
{
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToSection(doc.Sections.IndexOf(section));
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
footerInserted = InsertFooterText(text, statusName, userName, builder);
}
}
}
}
It calls this method:
private static bool InsertFooterText(string text, string statusName, string userName, DocumentBuilder builder)
{
builder.Font.Name = "Tahoma";
builder.Font.Size = 7;
builder.Font.Bold = true;
builder.Write($"[{text} | {userName} | {statusName}] ");
builder.Font.Bold = false;
return true;
}
I have attached a simple doc. It prepends the first footer but not the second. Link to previous doesn’t help either. word with footer and 2 sections.zip (14.0 KB)