Hi everyone! I’m having trouble generating a table using Aspose. I’ve tried several different approaches but it’s not working. I’m out of ideas. Please help! I have the following code:
var bookmarkName = "orders";
Bookmark bookmark = doc.Range.Bookmarks[bookmarkName];
if (bookmark == null)
return new Tuple<Document, bool>(doc, false);
DocumentBuilder builder = new DocumentBuilder(doc);
Node currentNode = bookmark.BookmarkStart.NextSibling;
while (currentNode != null && !currentNode.Equals(bookmark.BookmarkEnd))
{
Node nextNode = currentNode.NextSibling;
currentNode.Remove();
currentNode = nextNode;
}
builder.MoveTo(bookmark.BookmarkEnd);
builder.StartBookmark(bookmarkName);
builder.StartTable();
// Building table cells here...
builder.EndTable();
builder.EndBookmark(bookmarkName);
return new Tuple<Document, bool>(doc, true);
And the following docx: https://file.io/GbIFKrHKfcWU (The bookmark is located below the “Pedidos” heading)
My goal is to insert the table inside a bookmark, ensuring that I can consistently locate it by solely searching for the bookmark and using BookmarkStart.NextSibling. Despite trying this both with and without builder.StartBookmark and builder.EndBookmark following the documentation, I’ve encountered issues.
I’ve also experimented with adding a bookmark to the initial cell of the table, but that approach proved ineffective. The problem is that Aspose successfully creates the table, but when I attempt to remove an existing one, it fails, resulting in a stack of multiple tables. Debugging reveals that BookmarkStart.NextSibling consistently returns null, and the table, while visually positioned after the bookmark, isn’t actually situated between BookmarkStart and BookmarkEnd.
@majestic001
It seems you’re encountering issues with inserting a table between a bookmark’s start and end in your document using Aspose.Words. Based on your description, it appears that the table is being created, but it is not positioned correctly between the BookmarkStart
and BookmarkEnd
, which is causing problems when you try to remove existing tables.
Here are some suggestions to help you resolve this issue:
-
Ensure Proper Bookmark Usage: When you create a bookmark, make sure to use the same name for both StartBookmark
and EndBookmark
. This is crucial for the bookmark to function correctly.
-
Move to the Bookmark: Instead of using bookmark.BookmarkEnd
directly, you might want to move to the BookmarkStart
first and then insert the table. This ensures that the table is inserted right after the bookmark start.
-
Clearing Existing Content: Before inserting the new table, ensure that you are correctly removing any existing nodes between the bookmark’s start and end. You can use a loop to remove nodes, but make sure to check that you are not inadvertently skipping nodes or removing the bookmark itself.
-
Example Code: Here’s a modified version of your code that ensures the table is inserted correctly:
var bookmarkName = "orders";
Bookmark bookmark = doc.Range.Bookmarks[bookmarkName];
if (bookmark == null)
return new Tuple<Document, bool>(doc, false);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(bookmark.BookmarkStart);
// Clear existing content between the bookmark start and end
Node currentNode = bookmark.BookmarkStart.NextSibling;
while (currentNode != null && !currentNode.Equals(bookmark.BookmarkEnd))
{
Node nextNode = currentNode.NextSibling;
currentNode.Remove();
currentNode = nextNode;
}
// Insert the table
builder.StartTable();
// Add table cells here...
builder.EndTable();
return new Tuple<Document, bool>(doc, true);
-
Debugging: If BookmarkStart.NextSibling
is returning null, ensure that there are nodes present between the bookmark start and end. You can add some debug statements to check the nodes in that range.
By following these steps, you should be able to insert the table correctly within the specified bookmark. If you continue to face issues, consider checking the structure of your document to ensure that the bookmarks are set up as expected.
If you need further assistance, feel free to ask!
@majestic001 You can use the following code to insert a table into a bookmark:
string bookmarkName = "orders";
Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Bookmark bookmark = doc.Range.Bookmarks[bookmarkName];
// Remove old bookmark content.
bookmark.Text = "";
// Move DocumentBuilder's cursor inside bookmark.
builder.MoveToBookmark(bookmarkName, true, true);
builder.Writeln();
// Build table.
builder.StartTable();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
builder.InsertCell();
builder.Write($"cell_{i}_{j}");
}
builder.EndRow();
}
builder.EndTable();
doc.Save(@"C:\Temp\out.docx");
Then, you can set Bookmark.Text
to empty string to remove the inserted table.
1 Like
Thank you so much @alexey.noskov! This solved the problem in minutes. I had been stuck for days, and your help was essential.
1 Like