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

Free Support Forum - aspose.com

Bookmark Table Issue

using Aspose.Words version 14.1. After many painful hours of troubleshooting, I came to discover that if you execute DocumentBuilder.StartBookmark and then immediate execute DocumentBuilder.StartTable before first writing out other content, the bookmark does not get created even after executing the DocumentBuilder.EndBookmark method. Even inserting a continuous section break resolves the issue.

I didn’t know if this was a bug, or an undocumented feature…

Thanks!

Matt Wickless

Hi Matt,

Thanks for your inquiry. Please call the DocumentBuilder.StartBookmark after InsertCell method as shown below to get the required output.

Please let us know if you have any more queries.


Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();

// Insert a cell

builder.InsertCell();

builder.StartBookmark("BM");

builder.Write("This is row 1 cell 1");

// Insert a cell

builder.InsertCell();

builder.Write("This is row 1 cell 2");

builder.EndRow();

// Insert a cell

builder.InsertCell();

builder.Writeln("This is row 2 cell 1");

// Insert a cell

builder.InsertCell();

builder.Writeln("This is row 2 cell 2");

builder.EndRow();

builder.EndTable();

builder.EndBookmark("BM");

doc.Save(MyDir + "Out.docx");


As I mentioned, my practice of Starting the bookmark, and then inserting a continuous section break or even just a single space before starting the table also worked to resolve the problem. Both of our workarounds seem counter-intuitive though, unless I am not understanding something about bookmarks. I guess it would be helpful to me to understand why my solution and your solution both work, but starting the table immediately after starting the bookmark does not work. As I mentioned earlier, is it a bug, or do I need to understand the concept better?


For my purposes, I need to encapsulate the entire table in the bookmark so I it can be removed later programmatically if needed.

Thanks much,

Matt Wickless

Hi Matt,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you repeat the same scenarios using MS Word, you will get the same output.

Scenario 1 : Insert space before starting the table. In this case, there will be one empty paragraph before the table. So bookmark will contain one empty paragraph and a table. Please check Dom-space before table.png image for detail.

Scenario 2 : Insert section break before starting the table. In this case, bookmark will contain one empty paragraph with section break and a table. Please check Dom-Bookmark with section break.png image for detail.

Scenario 3 : Calling StartBookmark method after InsertCell method. In this case bookmark will contain only table. Please check the Dom-bookmarkstart inside cell.png for detail.

DocumentBuilder.StartTable does not create the table. This method starts a table in the document. If you want to bookmark the table only. Please use the code shared in my previous post. In scenario 1 and 2, the bookmark will contain extra empty paragraph before table. The MS Word does the same for scenario 3. Please check MSWord behaviour.png for detail.

Hope this answers your query. Please let us know if you have any more queries.

I don’t think we’re on the same page. In this scenario:


Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartBookmark(“BM”);

builder.StartTable();

// Insert a cell

builder.InsertCell();


builder.Write(“This is row 1 cell 1”);

// Insert a cell

builder.InsertCell();

builder.Write(“This is row 1 cell 2”);

builder.EndRow();

// Insert a cell

builder.InsertCell();

builder.Writeln(“This is row 2 cell 1”);

// Insert a cell

builder.InsertCell();

builder.Writeln(“This is row 2 cell 2”);

builder.EndRow();

builder.EndTable();

builder.EndBookmark(“BM”);

doc.Save(MyDir + “Out.docx”);


If you open the document then, Bookmark “BM” does not exist. However, if you insert anything between two, eg


builder.StartBookmark(“BM”);

builder.Write(" ");

builder.StartTable();


Bookmark BM Will exist in the document.

That is the point I’m trying to make. That makes me question whether it is intended functionality.


Thanks,


Matt

Hi Matt,

Thanks for your inquiry.

The code which you are using is incorrect. See the following code snippet. Calling the StartBookmark method before DocumentBuilder.StartTable is incorrect.

DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartBookmark(“BM”);

builder.StartTable();

// Insert a cell

builder.InsertCell();



Please call StartBookmark method after InsertCell method to get the required output. See the correct code snippet below.


DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();

// Insert a cell

builder.InsertCell();

builder.StartBookmark("BM");

builder.Write("This is row 1 cell 1");


The three scenarios explained in my previous post works without any issue. When you insert space or section break after calling StartBookmark and before StartTable method, the contents of bookmark will be Empty Paragraph and Table.

However, when you call StartBookmark after InsertCell method, the contents of bookmark will be only Table.

Please bookmark a table using MS Word. The bookmark start will be inside table's first cell. See the attached image for detail. I have highlighted the bookmark start and end in attached image.

Please let us know if you have any more queries.

As I mentioned I did resolve the issue myself prior to posting by starting the bookmark with a continuous section break before the table. Being that I have a solution. My query was whether or not it was the intended functionality that the component behave that way.


You have told me the code I have is incorrect. I did not find any documentation for your component stating that if you start a table immediate after starting a bookmark, the bookmark won’t actually get created after the endbookmark method is called. While it’s nice to have a solution, the solutions you describe seem to be counter-intuitive from a development standpoint. I understand what you are saying, Not trying to be argumentative, just trying to understand.

So, if nothing else, I guess my post becomes a feature request that calling the StartTable method does not destroy a previously started bookmark. or if nothing else, that the documentation better describes what you have described here.

Thanks,

Matt

Hi Matt,

Please accept my apologies for your inconvenience. We will update the documentation about the issue shared in this forum thread.

Bookmarks are supported only at the inline-level, that is inside Paragraph, but bookmark start and bookmark end can be in different paragraphs. However, the Table is Block-level node. So, calling the StartBookmark before StartTable as shown below will be ignored. Please note that badly formed bookmarks or
bookmarks with duplicate names will be ignored when the document is
saved.

DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartBookmark(“BM”);

builder.StartTable();

// Insert a cell

builder.InsertCell();



Please read the following documentation link for your kind reference.
http://www.aspose.com/docs/display/wordsnet/Composition+Diagrams