Hello,
I'm using DocIo to generate a table in a pdf file, using a word doc template which contains some bookmarks. So I get the bookmark from the WordDocument and I want to generate the table in that bookmark. The issue is that even though I get the bookmark and its position using BookmarkNavigator, the table is not generated at the bookmark position, is always placed at the end of the doc. What can be the issue? This is my code:
private void GenerateTable(WordDocument document, string bookmarkName, DataTable dataTable, string templateName)
{
try
{
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark(bookmarkName);
if (bookmarkNavigator.CurrentBookmark?.BookmarkStart == null)
{
throw new ApiException($"Bookmark '{bookmarkName}' is invalid or missing its BookmarkStart.", HttpStatusCode.BadRequest);
}
bookmarkNavigator.DeleteBookmarkContent(true);
IWTable table = CreateTableAtBookmark(bookmarkNavigator, document);
if (table == null)
{
throw new ApiException("Failed to create the table.", HttpStatusCode.InternalServerError);
}
ConfigureTableFormat(table);
int columnCount = dataTable.Columns.Count;
float pageWidth = document.LastSection.PageSetup.ClientWidth;
float[] columnWidths = GetDynamicColumnWidths(columnCount, pageWidth, dataTable);
PopulateTableWithData(table, dataTable, document.LastSection.PageSetup.ClientWidth, columnWidths, templateName);
}
catch (Exception ex)
{
throw new ApiException($"Error occurred while generating the table at '{bookmarkName}'.", HttpStatusCode.InternalServerError, ex.Message);
}
}
Thank you.
Hi Ciprian,
Based on the details provided, we suspect that your requirement is to clear the
items in the BookmarkStart
owner paragraph and insert a table after the paragraph. You are trying to add a
table; this will place the table at the end of the owner's text body. If you
wish to insert it at a specific location, use the Insert() method. Kindly refer
to the modified code snippet below
|
private IWTable CreateTableAtBookmark(BookmarksNavigator navigator, WordDocument document) { var bookmarkStart = navigator.CurrentBookmark?.BookmarkStart;
if (bookmarkStart != null) { var paragraph = bookmarkStart.OwnerParagraph;
if (paragraph != null) { if (paragraph.ChildEntities.Count > 0) { paragraph.ChildEntities.Clear(); // Remove existing content but keep the paragraph structure } //Get index of paragraph in the textBody which contains BookmarkStart int index = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph); WTable table = new WTable(document); //Insert table next to that paragraph paragraph.OwnerTextBody.ChildEntities.Insert(index + 1, table); return table; }
// If the bookmark's owner is a text body, add the table there if (bookmarkStart.Owner is IWTextBody textBodyFallback) { return textBodyFallback.AddTable(); } }
return null; } |
If you wish to add table in bookmark, kindly refer the below KB:
How
to add a table in a bookmark location?
This KB illustrates, how to insert a table within bookmark content
For more detailed information about working with bookmarks, kindly refer to
these UG.
Working
with Bookmarks in .NET Word library | Syncfusion®
Feel free to reach out if you have any further questions or require additional
details.
Regards,
Sindhu Ramesh.