How to insert a footer into a document

Hello Roman,

We are evaluating your product now. So far…Great product! Can you please provide an example of how to insert a footer into a document?

One way to do this is using the DocumentBuilder.MoveToHeaderFooter method, see the API doc for it here https://reference.aspose.com/words/net/aspose.words/documentbuilder/methods/movetoheaderfooter

Another way is to create required nodes in the document directly:

Document doc = new Document();
Section sect = doc.Sections[0];
HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
sect.HeadersFooters.Add(footer);

//Now you can add some paragraphs or whatever to the footer:
Paragraph para = new Paragraph(doc);
para.AppendChild(new Run(doc, "My Text"));
footer.AppendChild(para);

Hi Roman

This has worked for me - i didnt want a footer on my first page, but the same footer on every other page. This is fine since all of the footer content is replaced at runtime.

Now what i want, on all other pages bar the first page, is to selectively replace text in just one part of the header (which is made up of an image and some text). Do this mean MovingToSection for every page and then MovingToBookmark? Any ideas for the best method for this.

Thanks for any advice

This is fine - i didnt realise that each page has its own header (unlike the footer which is a document footer), so I’ll use a bookmark and replace each header’s text individually.

If I got you right you want to replace some part of the text in the header with a new text. This operation can actualy be done with one line of code:

((HeaderFooter)((Section)doc.Sections[0]).HeadersFooters[HeaderFooterType.HeaderPrimary]).Range.Replace("OldText", "NewText", true, true);

This code replaces all occurences of “OldText” inthe primary header of the document with “NewText”. Two boolean values at the end control whether the search would be case sensitive and whole word only.

Headers are not individual, they are the same as footers, i.e. applied to all pages in section.