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

Free Support Forum - aspose.com

Preceding Text and Following Text With Page Number of the Bookmark Added in the document

Hi,

I want to get the Preceding text and following text of the bookmark present in the document .

For eg m: in attached document

we have a bookmark ABSTRACT

I want to have the following result
page number = 1
Preceding text = Sc
FollowingAS_Doc1_3Pages.docx (42.0 KB)
Text = PC

Please find the attached document

@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:

Document doc = new Document("C:\\Temp\\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
foreach (Bookmark bk in doc.Range.Bookmarks)
{
    Console.WriteLine("Bookmark {0}", bk.Name);
    Console.WriteLine("Page {0}", collector.GetStartPageIndex(bk.BookmarkStart));
    Console.WriteLine("Precending text: '{0}'", bk.BookmarkStart.PreviousSibling != null ? bk.BookmarkStart.PreviousSibling.ToString(SaveFormat.Text).Trim() : "");
    Console.WriteLine("Following text: '{0}'", bk.BookmarkEnd.NextSibling != null ? bk.BookmarkEnd.NextSibling.ToString(SaveFormat.Text).Trim() : "");
    Console.WriteLine("---------------------------");
}

Please see Aspose.Words Document Object Model for more information.

Hi @alexey.noskov

I want to get the page number preceding text and following text and also the type of numbering

like in below doc page no 2

page number is SC - i - PC

then Preceding Text= SC
Following Text = PC
Page numbering style= low roman
and page numbering text= i;

Please find AS_Doc1_3Pages.docx (41.6 KB)
the attached document

@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:

As you can see Sc- i - PC is a simple text inserted in the first page footer of the second section in your document.

@alexey.noskov : I want to get the footer text . like for eg in attached document it is roman number

ii then output will be ii.

PNSTest_Standard.docx (63.4 KB)

@alexey.noskov

I want to get all the footer text also

please find the sample attached document.
LT_PT_FT_RT.docx (337.9 KB)

@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.

Hi @alexey.noskov,

In the attached document i have the bookmark PNT_Main_100_63 whose footer section contains

I am getting the left text : {AK-BRI~1;1}
and page number formatted text : Add- PAGE 1

I am getting the wrong formatted text

but in document it is : Add-1 (correct one).

and another case

Bookmark Name : PNT_Main_006_5

its page number is in roman style and it is i (attached screenshot)

but i am getting as PAGE vii

Please check and let me know.

Please find the attached document.
PNSTest_RunOnFootnotes.docx (101.1 KB)

Please find the source code below:

foreach (var headerFooterNode in headerFooterNodes)
{
    foreach (var bookmarkName in bookmarkNames)
    {
        var bookmark = section.Range.Bookmarks[bookmarkName];
        if (bookmark != null)
        {
            if (bookmarkName == "PNT_Main_100_63")
            {

            }
            var pageSetup = section.PageSetup;
            var bookmarkEnhancePageNumber = new BookmarkEnhancePageNumber(bookmark.Name)
            {
                BookmarkText = bookmark.Text,
                PageNumberText = Convert.ToString(layoutCollector.GetStartPageIndex(bookmark.BookmarkStart)),
                StyleText = Convert.ToString(pageSetup.PageNumberStyle)
            };

            string[] footerContents = headerFooterNode.Range.Text.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

@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("==========================");
}

HI @alexey.noskov,

if there are multiple text in footer how can we get ?

like for eg : Sample Left Text PT- 1 -FT Sample Right Text

Please fidn the attached document for reference

LT_PT_FT_RT.docx (337.9 KB)

HI @alexey.noskov

Please reply on above query

@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
==========================

Hi @alexey.noskov,

i want to get the each node value in footer seperated by tab key (\r) or (\t )

@gaurav.budhiraja You can parse the returned string to get the separate values. For example you can use String.Split method.

Hi @alexey.noskov,

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.

Please reply on above query ASAP

Please let me know for any query.

PNSTest_RunOnFootnotes.docx (105.7 KB)

Hi @alexey.noskov

Please reply on above query

@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();
}

Here is the output produced by this code:

{AK-BRI~1;1}    Ab-1