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

Free Support Forum - aspose.com

Finding out the position on a page within a Word Document

I am using Aspose.Words for Net 13.5.

I have a Word document that contains multiple bookmarks. I want to go thru each bookmark in the document and find out the position of that bookmark on the page. If its at a certain position, I want to force a page break.

I heard that ver 13.5 has some LayoutEnumerator class but I have not figured out how to do this.

Any help would be appreciated.

Hi Howard,


Thanks for your inquiry. Microsoft Word document is a flow document and it does not contain any information about its layout into lines and pages. So there is no way to determine where page starts or ends. The page numbers are represented by a PAGE field in MS Word documents and this field is related to the page layout algorithms in Aspose.Words. When you open a document with MS Word, it calculates the numbers on the fly.

However, you can achieve your requirement by using the utility method available in the attached ‘PageNumberFinder’ class.


Document doc = new
Document(MyDir + “in.docx”);<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);<o:p></o:p>

PageNumberFinder finder = new
PageNumberFinder(doc);<o:p></o:p>

foreach (Bookmark bm in doc.Range.Bookmarks)<o:p></o:p>

{<o:p></o:p>


builder.MoveToBookmark(bm.Name);<o:p></o:p>

int page = finder.GetPage(builder.CurrentNode);<o:p></o:p>

Console.WriteLine(page);<o:p></o:p>

}


Moreover, please check using the DocumentLayoutHelper sample from the offline samples pack. This sample demonstrates how to easily work with the layout elements of a document and access the pages, lines, spans etc.


Hope this helps you. Please let us know if you have any more queries.