Aspose Table Insert Using Bookmark

Hi,
Aspose.Words.Tables.Table tblTest = new Aspose.Words.Tables.Table(document);

Can you please suggest how can insert aspose table(tblTest ) using bookmark.

@RiteshAML10

To insert a table using a bookmark in Aspose.Words for .NET, you can follow these steps:

  1. Initialize the Document and DocumentBuilder: Create a new Word document and initialize the DocumentBuilder to work with it.

  2. Start the Bookmark: Use the DocumentBuilder to start a bookmark at the desired location in the document.

  3. Create the Table: Use the DocumentBuilder to start a table and insert cells as needed.

  4. End the Bookmark: After finishing the table, end the bookmark.

Here is a code example demonstrating these steps:

using System;
using Aspose.Words;
using Aspose.Words.Tables;

class Program
{
    static void Main()
    {
        // Create a new document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Start the bookmark
        builder.StartBookmark("MyBookmark");

        // Start the table
        builder.StartTable();

        // Insert the first cell
        builder.InsertCell();
        builder.Write("This is row 1 cell 1");

        // Insert the second cell
        builder.InsertCell();
        builder.Write("This is row 1 cell 2");
        builder.EndRow();

        // Insert the second row
        builder.InsertCell();
        builder.Write("This is row 2 cell 1");

        builder.InsertCell();
        builder.Write("This is row 2 cell 2");
        builder.EndRow();

        // End the table
        builder.EndTable();

        // End the bookmark
        builder.EndBookmark("MyBookmark");

        // Save the document
        doc.Save("BookmarkTable.docx");
    }
}

In this example, a new document is created, a bookmark named “MyBookmark” is started, and a table with two rows and two columns is inserted within that bookmark. Finally, the bookmark is ended, and the document is saved.

This approach allows you to effectively manage the placement of tables within bookmarks in your Word documents using Aspose.Words for .NET.

For more detailed information, you can refer to the tutorial on bookmarking table columns in a Word document here.

Sources:
[1]: Bookmark Table Columns In Word Document | Aspose.Words Document …

thanks @Professionalize.Discourse
any other way to insert table object direct on bookmark. or we can replace placeholder with aspose table object.

@RiteshAML10 You can use DocumentBuilder.MoveToBookmark method to move DocumentBuilder cursor to the bookmark. Then you can insert any content at the current DocumentBuilder location.

Hello @alexey.noskov

As when we are replacing place holder with Aspose table object so giving below error.

So, please suggest how can replace aspose table object with placeholder

@RiteshAML10 Range.Replace method accepts either two strings or regex and string as parameters. So you cannot pass Table object as a replacement to Range.Replace method.

@alexey.noskov So can we insert Table object on bookmark?

@RiteshAML10 Sure, you can use code like this:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Geterate dummy table. 
Table table = new Table(doc);
for (int i = 0; i < 5; i++)
{
    table.AppendChild(new Row(doc));
    for (int j = 0; j < 5; j++)
    {
        Cell c = new Cell(doc);
        c.EnsureMinimum();
        c.FirstParagraph.AppendChild(new Run(doc, $"{i}-{j}"));
        table.LastRow.AppendChild(c);
    }
}

// Insert the table at the bookmark.
builder.MoveToBookmark("test");
builder.Writeln();
builder.CurrentParagraph.ParentNode.InsertBefore(table, builder.CurrentParagraph);

doc.Save(@"C:\Temp\out.docx");

thanks @alexey.noskov

1 Like