Retrieving value of Bookmark within Table Cell returns entire Cell content and not content within range of Bookmark

Good afternoon,

I have attached a Word document which should allow you to recreate the issue I am experiencing.

I have written code to build a list of all Bookmarks within a Word document and to then cycle through these and retrieve the text within the Range of the Bookmark.  As you can see below these Bookmarks span text content, appear in Table Cells and, in many cases, a single Cell contains 2 Bookmarks:

   
 
The problem I'm experiencing is that, taking the "Unit Shipping Weight" [value] cell as an example, the text retrieved for each Bookmark is "2413 kg" whereas one of the two Bookmarks should return "2413" and the other should return "kg".

Could you review my code and suggest a modification that would allow just the content within a specific Bookmark to be returned for that Bookmark please?

public List<BookmarksAndValues> GetWordDocumentBookmarksAndValues(ref WordDocument wordDoc)
{
List<BookmarksAndValues> bookmarksAndValues = new List<BookmarksAndValues>();
Bookmark bookmark;
BookmarksNavigator bkNavigator = new BookmarksNavigator(wordDoc);
IEnumerable<string> bookmarkNamesList = GetWordDocumentBookmarkNamesList(ref wordDoc);

foreach (string sBookmarkName in bookmarkNamesList)
{
bookmark = wordDoc.Bookmarks.FindByName(sBookmarkName);

if (bookmark != null)
{
try
{
bkNavigator.MoveToBookmark(sBookmarkName, true, false);
try
{
TextBodyPart bookmarkContent = bkNavigator.GetBookmarkContent();

string bookmarkText = ((Syncfusion.DocIO.DLS.WParagraph)bookmarkContent.BodyItems.FirstItem).Text;
bookmarksAndValues.Add(new BookmarksAndValues(sBookmarkName, bookmarkText));
}
catch
{
//Continue to next Bookmark.
}
}
catch (Exception ex)
{
throw new Exception("The bookmark " + sBookmarkName + " has an error.");
}
}
}

return bookmarksAndValues;
}

Thank you,

Jason.

Attachment: Sample_7235fd3b.zip

3 Replies

MR Manikandan Ravichandran Syncfusion Team March 6, 2020 01:27 PM UTC

Hi Feded,

Thank you for contacting Syncfusion support.

On further analyzing the given details, we found that, you have retrieved the text from the paragraph and so, the entire text in the paragraph will returns in your case. To achieve your requirement with “To get bookmark content alone”, we suggest you to retrieve the text from the text ranges of paragraph.

Based on this we have prepared the sample with your code example which can be downloaded from the following link.
https://www.syncfusion.com/downloads/support/forum/152205/ze/WinFormsAPP914149129

Please let us know if you have any other questions.

Regards
,
Manikandan Ravichandran


FE feded March 6, 2020 06:38 PM UTC

Hi Manikandan

Thank you very much - the solution you provided works perfectly.

Thank you for taking the time to construct my object classes and bookmark retrieval code in order to provide a working solution, that's first class support.

Regards,

Jason.


VA Vijayasurya Anandhan Syncfusion Team March 9, 2020 07:18 AM UTC

Hi Feded,

Thank you for your update and confirmation.

 
We are glad to know that the provided solution meets your requirement. Please let us know if you need further assistance in this. As always, we will be happy to assist you. 

Regards,
Vijayasurya A


Loader.
Up arrow icon