How to replace text in document's footer

Hi Aspose Team,

I need to replace footer content into document which contains around 120-150 pages without performance issue to generate a document

So, I preferred in my application to replace a text in footer content while using find and replace document footer using get document.range - replace method.

Is this replace method approach in a sequential process?
how this find and replace works internally in document?

Thanks & Regards
Shyamala

@Shyamu If you need to replace text only in the document’s footer, you do not need to use Document.Range, you can replace text only in the document’s footer range. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
NodeCollection headerFooterCollection = doc.GetChildNodes(NodeType.HeaderFooter, true);
foreach (HeaderFooter hf in headerFooterCollection)
{
    // Here you can also check HeaderFooter type to replace text only in a specific header or footer.
    hf.Range.Replace("old text", "new cool text");
}
doc.Save(@"C:\Temp\out.docx");

Thanks Alexey !!

In this approach, Document footer range should replace a new text instead of old text in a sequential process as updating a footer in document page one by one.

is there any possibility to replace an expected text in document footer at one shot without streaming document pages one after another?

if not, please provide explanation for “why?”

Regards,
Shyamala

@Shyamu Header and footer are set per section not per page. If your document contains only one section, the code will process maximum 6 nodes, if all types of headers footers present in the document.
Please see the following article to learn more about Aspose.Words Document Object Model. Also, you can use DocumentExplorer demo project to investigate node structure of your document.

Thanks Alexey !!

Footer Text Replace is not working using pattern.compile.

Please refer a sample test package to debug this issue.

Even, I have coded two approaches 1 & 2 to replace footer text. Please suggest me for which approach is fastest one to handle footer text replace more than 50 pages documents?

Regards,
Shyamala B
Footer_Pattern_Test_Package.zip (17.2 KB)

@Shyamu Please modify your pattern like this to make it work:

Pattern.compile("[a-zA-Z]{3}\\s\\d{2},(\\s\\d{4})")

Both approaches will work, but the first approach will work only for the first section of the document. If your document contains only one section, then you can use this approach since it process only one node. For multi-section documents you can either use the second approach or use code like this:

for(Section sect : doc.getSections())
{
    HeaderFooter footer = sect.getHeadersFooters().get(HeaderFooterType.FOOTER_PRIMARY);
    footer.getRange().replace(Pattern.compile("[a-zA-Z]{3}\\s\\d{2},(\\s\\d{4})"), "Remove", options);
}