Replace table in bookmark C#

I’m trying to replace bookmark with table it works perfectly but when replacing same bookmark with table
it append the table to the old table not replacing here is my code:

DocumentBuilder builder = new DocumentBuilder(asposeDocument);
builder.MoveToBookmark(input.Key, true, true);
Table table = builder.StartTable();
// Make the header row.
builder.InsertCell();

table.AllowAutoFit = true;
table.LeftPadding = 5.0;
table.RightPadding = 5.0;
table.BottomPadding = 5.0;
table.TopPadding = 5.0;

// Set height and define the height rule for the header row.
builder.RowFormat.Height = 30.0;
builder.RowFormat.HeightRule = HeightRule.AtLeast;

// Some special features for the header row.
builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(198, 217, 241);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Size = 12;
builder.Font.Bold = false;
builder.Font.Name = "Calibri";


builder.EndRow();

// Set features for the other rows and cells.
builder.CellFormat.Shading.BackgroundPatternColor = Color.White;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

// Reset height and define a different height rule for table body
builder.RowFormat.Height = 30.0;
builder.RowFormat.HeightRule = HeightRule.Auto;
// Reset font formatting.
builder.Font.Size = 12;
builder.Font.Bold = false;
// Build the other cells.
builder.Write("This is row 1 cell 1");

// Insert a cell

builder.InsertCell();

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

builder.EndRow();

// Insert a cell

builder.InsertCell();

builder.Writeln("This is row 2 cell 1");

// Insert a cell

builder.InsertCell();

builder.Writeln("This is row 2 cell 2");

builder.EndRow();
builder.EndTable();

@fadelsahmarani

I have moved this thread to Aspose.Words support forum where our concerned team will assist you further in this regard.

@fadelsahmarani,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document you are getting this problem with
  • Aspose.Words version 21.3 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.
  • A standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario/issue and provide you more information.

Dear ,
Kindly find requested pieces of information.
Regards,
Issue aspose.zip (135.0 KB)

@fadelsahmarani,

Please note that Microsoft Word merges two or more consecutive Tables into one big Table. To prevent Microsoft Word from doing this, you need to insert an empty Paragraph (builder.Writeln();) in between those Tables. Inside static void runThisFunctionForSecondOutput(), you can add following line to generate two separate/independent Tables:

...
...
    DocumentBuilder builder = new DocumentBuilder(asposeDocument);
    builder.MoveToBookmark("datagrid", true, true);

    builder.Writeln();

    Table table = builder.StartTable();

    // Make the header row.
    builder.InsertCell();

...
...