Page number from bookmark

I’d like to know if there is a way to get the page number of a bookmark in a generated Word document. According to this thread there was a pagination system in development some time ago. Has this been released?
I don’t need perfect accuracy, and since I’m building the document by appending blocks of text it would be enough to just know how many pages the document has at the moment before inserting a new block.

Hi
Thanks for your request. There is no direct method to get number of page where bookmark is located. However, you can try using code like the following to achieve this:

private int GetBookmarkPage(Bookmark bookmark)
{
    // Create a DocumentBuilder and move it to the bookmark.
    DocumentBuilder builder = new DocumentBuilder((Document) bookmark.BookmarkStart.Document);
    builder.MoveToBookmark(bookmark.Name);
    // Insert a PAGE field and update it.
    Field page = builder.InsertField("PAGE");
    builder.Document.UpdatePageLayout();
    page.Update();
    int pageNumber = int.Parse(page.Result);
    // Remove PAGE field.
    page.Remove();
    return pageNumber;
}

Hope this helps.
Best regards,

Thank you, this works perfectly. I’m just curious as to why it works, when I know that Aspose is not a rendering engine. It seems accurate from my tests but I just want to know how much I can trust it.

Hi
Thanks for your inquiry. Actually, Aspose.Words has its own rendering engine that is used to layout document into pages upon rendering to various “fixed-page” formats, like PDF, XPS, SWF, besides the same rendering engine is used upon updating fields. Please follow the link for more information:
https://docs.aspose.com/words/java/rendering/
Best regards,

The issues you have found earlier (filed as WORDSNET-2978) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)