How to add a table to a, existing named bookmark in a template

1 Answer 67 Views
WordsProcessing
Joost
Top achievements
Rank 1
Joost asked on 17 Jul 2024, 04:36 PM | edited on 17 Jul 2024, 04:36 PM

Hi I am looking for code to add a table to a template with a bookmark:

The first step in my code is to go to the named bookmark, then I generate the table, but the table is not attached to location of the bookmark, but shows at the very end of the document.

var bookmark = bookmarks.FirstOrDefault(b => b.Bookmark.Name == "PricingTable");
if (bookmark is not null)
{
editor.MoveToInlineEnd(bookmark.Bookmark.BookmarkRangeStart);

Table table = document.Sections.AddSection().Blocks.AddTable();
document.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.TableGridStyleId);
table.StyleId = BuiltInStyleNames.TableGridStyleId;
foreach (var pricingTableRow in pricingTable)
{
TableRow row = table.Rows.AddTableRow();
foreach (var property in pricingTableRow.GetType().GetProperties())
{
TableCell cell = row.Cells.AddTableCell();
cell.Blocks.AddParagraph().Inlines.AddRun(property.GetValue(pricingTableRow) is null ? string.Empty : property?.GetValue(pricingTableRow)?.ToString());
}
}
}

}

 

 

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 18 Jul 2024, 09:28 AM

Hello Joost,

You are experiencing unexpected results because the editor is successfully moved to the correct position:

editor.MoveToInlineEnd(bookmark.Bookmark.BookmarkRangeStart);

... but is not utilized because the table is being added via another method which places the table at the end of the document:

Table table = document.Sections.AddSection().Blocks.AddTable();

To utilize the editor and its positioning you should use the editor's InsertTable method which inserts a table at the editor's position and returns a Table instance which you can later modify:

Table table = editor.InsertTable();

I hope this helps.

Regards,
Yoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Joost
Top achievements
Rank 1
commented on 19 Jul 2024, 09:05 AM

Yes it works now. Thank you Yoan
Tags
WordsProcessing
Asked by
Joost
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or