Free Support Forum - aspose.com

Getting the bookmark and page number

Hi,

I am currently evaluating the product to see if it meets our need. I am using temporary license, so do have access to full feature. I have a question on Aspose.word bookmark feature. I found good amount of information on Aspose.pdf bookmark feature but didn’t find much on Word.

1. If I have a word document which have multiple bookmark across several pages. Is there a way I can extract all the bookmarks. I also need to know the Page Number of each bookmark.
For Aspose.Pdf I think this is possible using following code. But not sure for Aspose.word.
PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor();

//open PDF file
bookmarkEditor.BindPdf(fileName);

Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks();

foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks)
{
// bookmark.PageNumber;
//bookmark.Title
}
Please let me know.
Thank you.

Hi Ashish,


Thanks for your inquiry. Firs of all, please read the following article that outlines working with Bookmarks in Aspose.Words:
http://www.aspose.com/docs/display/wordsnet/Bookmarks+in+Aspose.Words

Secondly, 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)

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

// Create a DocumentBuilder and move it to the bookmark.<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder((Document)bookmark.BookmarkStart.Document);<o:p></o:p>

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

// Insert a PAGE field and update it.<o:p></o:p>

Field page = builder.InsertField(“PAGE”);<o:p></o:p>

page.Update();<o:p></o:p>

int pageNumber = int.Parse(page.Result);<o:p></o:p>

// Remove PAGE field.<o:p></o:p>

page.Remove();<o:p></o:p>

return pageNumber;<o:p></o:p>

}


I hope, this will help.

Best Regards,

Thank you. That does help to get the page number.

However, when I passed a list of bookmark to this method so that it returns a list of page number, it gives an error on getting page.Result value.

At line - int pageNumber = int.Parse(page.Result); //gives error

Below is the modified code -
List<Aspose.Words.Bookmark> bookmarks = new List();
List bookmarkPageNumbers = new List();

for (int i =0; i <inputDocument.Range.Bookmarks.Count ; i++)
{
bookmarks.Add(inputDocument.Range.Bookmarks[i]);

}

bookmarkPageNumbers = GetBookmarkPage(bookmarks);


private static List GetBookmarkPage(List<Aspose.Words.Bookmark> bookmarks)
{
List bookMarkPageNumbers = new List();
foreach (var bookmark in bookmarks)
{
// Create a DocumentBuilder and move it to the bookmark.
Aspose.Words.DocumentBuilder builder = new DocumentBuilder((Aspose.Words.Document)bookmark.BookmarkStart.Document);
builder.MoveToBookmark(bookmark.Name);

// Insert a PAGE field and update it.
Field page = builder.InsertField(“PAGE”);
page.Update();

int pageNumber = int.Parse(page.Result);

bookMarkPageNumbers.Add(pageNumber);

// Remove PAGE field.
page.Remove();
}

return bookMarkPageNumbers;
}

Please let me know. Thanks

Hi Ashish,


Thanks for your inquiry. Please format your document into pages and update the page number related fields by calling the Document.UpdatePageLayout method as follows:

private static List<int>
GetBookmarkPage(List<Aspose.Words.Bookmark> bookmarks)<o:p></o:p>

{

List<int> bookMarkPageNumbers = new List<int>();

foreach (var bookmark in bookmarks)

{

// Create a DocumentBuilder and move it to the bookmark.

Aspose.Words.DocumentBuilder builder = new DocumentBuilder((Aspose.Words.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);

bookMarkPageNumbers.Add(pageNumber);

// Remove PAGE field.

page.Remove();

}

return bookMarkPageNumbers;

}


I hope, this helps.

Best Regards,

Thanks. That does help to get the page number. I am now able to get the page number for the list of bookmark, but I am having problem extracting the bookmarks.

What I mean by that is, I have a word document with bookmarks on page 1 and page 8.
So the code as listed below is only returning the bookmark on page 1 of word document. It’s for some reason not returning the bookmark info that is on page 8.

List<Aspose.Words.Bookmark> bookmarks = new List();

for (int i =0; i <inputDocument.Range.Bookmarks.Count ; i++)
{
bookmarks.Add(inputDocument.Range.Bookmarks[i]);

}

I used the openxml to verify the bookmark information for the document I am working on and it does show the bookmark on page 8. Not sure what needs to be done here

Please suggest.

Thanks

Hi Ashish,


Thanks for your inquiry. I see you have reported the same problem in a separate thread. I would suggest you please follow that thread for further proceedings. We would be requiring your input Word document for testing. Once we have your document, we will investigate the issue on our end and provide you more information.

Best regards,

Sure…Thanks!

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.
(19)