@gaurav.budhiraja I do not see a bookmark with name ABSTRACT in your document.
You can use LayoutCollector to get page number where bookmark is placed, NextSibling and PreviousSibling properties to get preceding and following nodes. For example see the following code:
@gaurav.budhiraja What you are asking about is footer text. Also, i in your footer is not actually page number field, this is a simple text.
You can get headers/footers from your document using Section.HeaderFooters collection. Please see structure of your document:
@gaurav.budhiraja You can get header/and footer collection of each section using Section.HeadersFooters collection. Please see our documentation to learn how to work with headers/footers: https://docs.aspose.com/words/net/working-with-headers-and-footers/
You can get text of any node using Node.ToString method.
But you should note that headers/footers are repeated for each page in section and PAGE field is updated accordingly. So visually one header/footer of one section can have different values for each page in the section.
@gaurav.budhiraja As I already mentioned header/footer is applied to section, not to page in MS Word document. Section can have multiple pages. So headers/footers are repeated for each page in section and PAGE field is updated accordingly. So visually one header/footer of one section can have different values for each page in the section.
The value returned by Aspose.Words properly shows the values of Footer on the last page of the section. This is the expected behavior.
In your case you can consider using Document.ExtractPages method to get value of header/footer of each page:
Document doc = new Document(@"C:\Temp\PNSTest_RunOnFootnotes.docx");
for (int i = 0; i < doc.PageCount; i++)
{
Document page = doc.ExtractPages(i, 1);
// Get formated page number. Updating fields in the footer has specifics.
// So use a temporary field in the main moby to get correct value.
DocumentBuilder pageBuilder = new DocumentBuilder(page);
Field pageField = pageBuilder.InsertField(FieldType.FieldPage, true);
string pageValue = pageField.Result;
pageField.Remove();
HeaderFooter footer = page.LastSection.HeadersFooters[HeaderFooterType.FooterPrimary];
List<FieldPage> pageFields = footer.Range.Fields.Where(f => f.Type == FieldType.FieldPage).Cast<FieldPage>().ToList();
foreach (FieldPage p in pageFields)
p.Result = pageValue;
Console.WriteLine(footer.ToString(SaveFormat.Text).Trim());
Console.WriteLine("==========================");
}
@gaurav.budhiraja The code I have provided in my previous answer successfully returns text from footer for each page in your document. Here is the output produced by the provided code:
Sample Left Text PT- 1 -FT Sample Right Text
==========================
Sample Left Text PT- 2 -FT Sample Right Text
==========================
Sample Left Text PT- 3 -FT Sample Right Text
==========================
Sample Left Text PT- 4 -FT Sample Right Text
==========================
In the attached document , bookmark name = “PNT_Main_017_12”
I am getting the footer text= {AK-BRI~1;1} by using above code . But in my document i have content (page number with text = Ab-1) . I am not able to get this text. Can you please let me know how to get it .
Please find the attached document.
@gaurav.budhiraja As I can see the code returns correct footer text from your document.
I have used the following simple code to get page index where the bookmark is located and then get this page’s footer text:
Document doc = new Document(@"C:\Temp\PNSTest_RunOnFootnotes.docx");
LayoutCollector collector = new LayoutCollector(doc);
// Get page index of the bookmark.
Bookmark bk = doc.Range.Bookmarks["PNT_Main_017_12"];
int pageIndex = collector.GetStartPageIndex(bk.BookmarkStart) - 1;
// Get page content.
Document page = doc.ExtractPages(pageIndex, 1);
page.Save(@"C:\Temp\test.docx");
Console.WriteLine(GetPrimaryFooterText(page));
private static string GetPrimaryFooterText(Document doc)
{
// Get formated page number. Updating fields in the footer has specifics.
// So use a temporary field in the main moby to get correct value.
DocumentBuilder pageBuilder = new DocumentBuilder(doc);
Field pageField = pageBuilder.InsertField(FieldType.FieldPage, true);
string pageValue = pageField.Result;
pageField.Remove();
HeaderFooter footer = doc.LastSection.HeadersFooters[HeaderFooterType.FooterPrimary];
List<FieldPage> pageFields = footer.Range.Fields.Where(f => f.Type == FieldType.FieldPage).Cast<FieldPage>().ToList();
foreach (FieldPage p in pageFields)
p.Result = pageValue;
return footer.ToString(SaveFormat.Text).Trim();
}