Hi,
I want to give my users a template document, that they can change and style, and then read the template, and enhance it with DocIO.
There are a number of bookmarks in the document. The bookmarks contain
- A header
- Some text
- A table
- Some more text
Base on the data, the goal is for each individual bookmark part in the document:
- Either to change the content of the table.
- Or, to delete the whole part of header, text, and table.
My code fails at the first task:
if (string.IsNullOrEmpty(bookmarkName)) return;
//Move to the virtual cursor before the bookmark end location of the bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark(bookmarkName);
if (bookmarkNavigator == null) return;
//Gets the content for the current bookmark
TextBodyPart textBodyPart = bookmarkNavigator.GetBookmarkContent();
WTable table = null;
var index = 0;
foreach (Entity entity in textBodyPart.BodyItems)
{
if (entity is WTable)
{
table = entity as WTable;
break;
}
index++;
}
if (table == null) return;
... process the table, add rows, columns, and change a number of cells
textBodyPart.BodyItems.Insert(index, table); //add the new table
bookmarkNavigator.ReplaceBookmarkContent(textBodyPart); //change the document
However, this leaves the document unchanged. I managed to do it with a second bookmark for just the table, but that complicates the template a lot by requiring all these extra bookmarks for each table. In that case, I used :
textBodyPart.BodyItems.Add(table);
instead of the Insert, but this adds the table at the end of the bookmark, which is not what I want.
I hope you can help, because I am stuck.