Replace Bookmark with a Table in Word Documents in C#, VB.NET

Bookmarks are a great way to specify important locations on a Word document. Spire.Doc supports to access the bookmarks within the document and insert objects such as text, image and table at the bookmark location. This article will show you how to access a specific bookmark and replace the current bookmark content with a table.

Step 1: Load a template Word document.

Document doc = new Document();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\employee.docx");

Step 2: Create a Table object.

Table table = new Table(doc,true);

Step 3: Fill the table with sample data.

DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("job", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("salary", typeof(string));
dt.Rows.Add(new string[] { "Emp_ID", "Name", "Job", "E-mail", "Salary" });
dt.Rows.Add(new string[] { "0241","Andrews", "Engineer", "andrews@outlook.com" ,"3.8K"});
dt.Rows.Add(new string[] { "0242","White", "Manager", "white@outlook.com","4.2K" });
dt.Rows.Add(new string[] { "0243","Martin", "Secretary", "martin@gmail.com", "3.5K" });
table.ResetCells(dt.Rows.Count, dt.Columns.Count);          
for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
    }
}

Step 4: Get the specific bookmark by its name.

BookmarksNavigator navigator = new BookmarksNavigator(doc);
navigator.MoveToBookmark("bookmark_employee");

Step 5: Create a TextBodyPart instance and add the table to it.

TextBodyPart part = new TextBodyPart(doc);
part.BodyItems.Add(table);

Step 6: Replace the current bookmark content with the TextBodyPart object.

navigator.ReplaceBookmarkContent(part);

Step 7: Save the file.

doc.SaveToFile("output.docx", FileFormat.Docx2013);

Result:

Replace Bookmark with a Table in Word Documents in C#, VB.NET

Full Code:

[C#]
Document doc = new Document();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\employee.docx");

Table table = new Table(doc,true);        
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("job", typeof(string));
dt.Columns.Add("email", typeof(string));
dt.Columns.Add("salary", typeof(string));
dt.Rows.Add(new string[] { "Emp_ID", "Name", "Job", "E-mail", "Salary" });
dt.Rows.Add(new string[] { "0241", "Andrews", "Engineer", "andrews@outlook.com", "3.8K" });
dt.Rows.Add(new string[] { "0242", "White", "Manager", "white@outlook.com", "4.2K" });
dt.Rows.Add(new string[] { "0243", "Martin", "Secretary", "martin@gmail.com", "3.5K" });
table.ResetCells(dt.Rows.Count, dt.Columns.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString());
    }
}

BookmarksNavigator navigator = new BookmarksNavigator(doc);
navigator.MoveToBookmark("bookmark_employee");
TextBodyPart part = new TextBodyPart(doc);
part.BodyItems.Add(table);
navigator.ReplaceBookmarkContent(part);
doc.SaveToFile("output.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("output.docx"); 
[VB.NET]
Dim doc As Document = New Document()
doc.LoadFromFile("C:\Users\Administrator\Desktop\employee.docx")
Dim table As Table = New Table(doc, True)
Dim dt As DataTable = New DataTable()
dt.Columns.Add("id", GetType(String))
dt.Columns.Add("name", GetType(String))
dt.Columns.Add("job", GetType(String))
dt.Columns.Add("email", GetType(String))
dt.Columns.Add("salary", GetType(String))
dt.Rows.Add(New String() {"Emp_ID", "Name", "Job", "E-mail", "Salary"})
dt.Rows.Add(New String() {"0241", "Andrews", "Engineer", "andrews@outlook.com", "3.8K"})
dt.Rows.Add(New String() {"0242", "White", "Manager", "white@outlook.com", "4.2K"})
dt.Rows.Add(New String() {"0243", "Martin", "Secretary", "martin@gmail.com", "3.5K"})
table.ResetCells(dt.Rows.Count, dt.Columns.Count)
For i As Integer = 0 To dt.Rows.Count - 1
    For j As Integer = 0 To dt.Columns.Count - 1
        table.Rows(i).Cells(j).AddParagraph().AppendText(dt.Rows(i)(j).ToString())
    Next
Next

Dim navigator As BookmarksNavigator = New BookmarksNavigator(doc)
navigator.MoveToBookmark("bookmark_employee")
Dim part As TextBodyPart = New TextBodyPart(doc)
part.BodyItems.Add(table)
navigator.ReplaceBookmarkContent(part)
doc.SaveToFile("output.docx", FileFormat.Docx2013)