We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How do I control footer content

I would like to manipulate the footer content on different page.

Example, word document have 5 page. All page footer show only page number, where else
last page of the document we keep the footer as it is.

Below code will remove all the footer content and keep only the last page footer.
Now, how to add page number only on other page? Thanks for sharing.

//Set Footer to last page only
DocumentBuilder builder = new DocumentBuilder(doc);
HeaderFooter hf = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary];
int childCount = hf.ChildNodes.Count;
Paragraph para = new Paragraph(doc);
hf.InsertBefore(para, hf.FirstChild);
builder.MoveTo(para);

// { IF “{PAGE}” = “{NUMPAGES}” “Content Visible on Last Page Only” “” }
Field field = builder.InsertField(“IF “”, null);
builder.MoveTo(field.Start.NextSibling.NextSibling);
builder.InsertField(“PAGE”, null);
builder.Write(”" = “”);
builder.InsertField(“NUMPAGES”, null);

builder.Write("" “”);
BookmarkStart bmStart = builder.StartBookmark(“bm”);
builder.Writeln();
builder.EndBookmark(“bm”);
builder.Write("" “”");

Paragraph bmPara = bmStart.ParentNode as Paragraph;
for (int i = 0; i < childCount; i++)
{
// hf.InsertAfter(hf.LastChild, bmPara);
}
builder.MoveToDocumentEnd();

Hi there,


Thanks for your inquiry. Please check the following highlighted code snippet. Hope this helps you.

If you still face problem, please share your expected output document here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Document doc = new Document(MyDir + “SampleDoc.docx”);<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

HeaderFooter hf = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary];

Node[] nodes = hf.ChildNodes.ToArray();

int childCount = hf.ChildNodes.Count;

Paragraph para = new Paragraph(doc);

hf.InsertBefore(para, hf.FirstChild);

builder.MoveTo(para);

// { IF "{PAGE}" = "{NUMPAGES}" "Content Visible on Last Page Only" "" }

Field field = builder.InsertField("IF \"", null);

builder.MoveTo(field.Start.NextSibling.NextSibling);

builder.InsertField("PAGE", null);

builder.Write("\" = \"");

builder.InsertField("NUMPAGES", null);

builder.Write("\" \"");

BookmarkStart bmStart = builder.StartBookmark("bm");

builder.Writeln();

builder.EndBookmark("bm");

builder.Write("\" \"\"");

Paragraph bmPara = bmStart.ParentNode as Paragraph;

foreach (Node node in nodes)

{

hf.InsertAfter(node, bmPara);

}

builder.MoveToDocumentEnd();

doc.UpdateFields();

doc.Save(MyDir + "17.5.docx");