Error Trying to Read Bookmarked Field In a Table Cell: Any Ideas?

When I attempt to read a bookmarked table cell, I get the message below. Does anyone have any ideas? I am evaluating the product and would like to purchase for a customer, however reading bookmarked fields (from word doc templates) is the sole reason for this purchase.

java.lang.IllegalArgumentException: Start and end node should have the same grand parent.
error:java.lang.IllegalArgumentException: Start and end node should have the same grand parent.
at com.aspose.words.CompositeNode.a(Unknown Source)
at com.aspose.words.CompositeNode.a(Unknown Source)
at com.aspose.words.Bookmark.getText(Unknown Source)

This is the snippet of code that I am using to test reading bookmarked fields:

try
{
    Document doc = new Document("c:\\temp\\test2.doc");
    Bookmarks bookmarks = doc.getRange().getBookmarks();
    int bookmarkCount = bookmarks.getCount();
    Bookmark bookmark = null;
    for (int i = 0; i < bookmarkCount; i++)
    {
        bookmark = bookmarks.get(i);
        System.out.println("Bookmark Name:" + bookmark.getName());
        System.out.println("Bookmark Text:" + bookmark.getText());
    }
}

Hi
Thanks for your request. Please, attach your document. Then I will investigate this problem and try to help you.
Best regards.

Here is an example file.

Thanks!

Hi
Please reattach your document.
Best regards.

I’m not sure why it didn’t upload the first time. Here it is again.

Hi
Thanks for additional information. I have logged this problem to our defect database as issue # 3816. Please expect a reply before the next hotfix (within 2-3 weeks). We might just fix it by then or provide more information
This error occurs because BookmarkStart and BookmarkEnd have different ParentNode. Here is workaround for you.

Document doc = new Document(@"234_96714_tfassett\test2.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection starts = doc.GetChildNodes(NodeType.BookmarkStart, true);
NodeCollection ends = doc.GetChildNodes(NodeType.BookmarkEnd, true);
for (int i = 0; i < starts.Count; i++)
{
    builder.MoveTo(starts[i].ParentNode);
    builder.InsertNode(ends[i]);
}
Bookmarks bookmarks = doc.Range.Bookmarks;
int bookmarkCount = bookmarks.Count;
Bookmark bookmark = null;
for (int i = 0; i < bookmarkCount; i++)
{
    bookmark = bookmarks[i];
    string name = bookmark.Name;
    string text = bookmark.Text;
}

Best regards.

First, thanks for you assistance. Next, I wasn’t able to get your code above to work due to syntax errors, however I did get the following to work (which I believe matches yours with the syntax corrections.). It no longer throws an error, however it does not return the correct bookmark text. For example, in the document there is a bookmark called “test2” which has a value of “Tim”. The value printed out via the code below shows “Personal Information” which is a value of a parent node. Any ideas?

try
{
    Document doc = new Document("c:\temp\test2.doc");
    DocumentBuilder builder = new DocumentBuilder(doc);
    NodeCollection starts = doc.getChildNodes(NodeType.BOOKMARK_START, true);
    NodeCollection ends = doc.getChildNodes(NodeType.BOOKMARK_END, true);

    for (int i = 0; i < starts.getCount(); i++)
    {
        builder.moveTo(starts.get(i).getParentNode());
        builder.insertNode(ends.get(i));
    }

    Bookmarks bookmarks = doc.getRange().getBookmarks();
    int bookmarkCount = bookmarks.getCount();
    Bookmark bookmark = null;
    for (int i = 0; i < bookmarkCount; i++)
    {
        bookmark = bookmarks.get(i);
        String name = bookmark.getName();
        String text = bookmark.getText();
        System.out.println("Bookmark Name:" + bookmark.getName());
        System.out.println("Bookmark Text:" + bookmark.getText());
    }
}
catch (Exception e)
{
    e.printStackTrace();
    System.out.println("error:" + e);
}

Hi
My code example is C# code. Sorry for this. The bookmark’s text is the text between BookmarrStart and BookmarkEnd node. The code moves BookmarkEnd node to the end of the parent node of BookmarkStart. That’s why you get this text.
Best regards.

Hmm. Maybe it’s because it’s Monday or maybe I’m slow but I’m stuck. What should the code look like so the correct text/value for the bookmarks is “printed”?

Here is structure of your document before and after processing.

How you can see we have moved the BookmarkEnd node.
Best regards.

First, thanks for your assistance. I want to purchase this tool as I think it will do the job, however I still don’t see how I can get the bookmark value that I am looking for. I’m not familiar with “nodes” in the context of a MS Word Table, as the document to me is just a table with cells.
The code you provided (the work around) prints out the value from another cell (i.e. not what I bookmarked). How can I get the value of the bookmarked cell so it contains the actual bookmarked cell rather than the value of the parent cell?
Thanks.

Hi
If you want to extract data from table cells then I think that you should use form fields. You can get values of form fields using the following code.

int fieldsCount = doc.getRange().getFormFields().getCount();
for (int i = 0; i < fieldsCount; i++)
{
    System.out.println("Bookmark Name:" + doc.getRange().getFormFields().get(i).getName());
    System.out.println("Bookmark Text:" + doc.getRange().FormFields().get(i).getResult());
}

Also see attached document.
Best regards.

That should work perfectly. Thanks for your assistance.

This is a known issue, I’ve merged this issue into #502.
Not all bookmarks that you can create in MS Word can be fully accessible in Aspose.Words.
In Aspose.Words the document is a tree of nodes. So far bookmarks with the same parent or grand parent node are supported. Examples are:

  • bookmark starts and ends in the same paragraph (parent is the paragraph) or
  • bookmark starts and ends in different paragraphs, but in the same section (the parent is the body node of the section).

Some bookmarks are not supported fully. I mean they will be opened and saved okay, but unfortunately you cannot work with them programmatically. Examples of such bookmarks are:

  • Bookmark starts in one row of a table and ends in another row of a table. In this case the chain is bookmark->paragraph->cell->row->table which is actually 4 levels of parents. This is what happens in your case.
  • Bookmark starts in one section and ends in another section. The chain is bookmark->paragraph->body->section->document which is four levels of parents.
  • Some other cases similar to the above are possible too.

We will improve ASpose.Words to work with such bookmarks in the future, but I cannot give a date at this stage.

The issues you have found earlier (filed as 502) have been fixed in this update.

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