Free Support Forum - aspose.com

Table inside table with cell merge fails

Trying to put table inside table. Internal table does not spans over parent table and always remains in one cell despite CellMerge.


[Test]
public void TableInsideTable()
{
var doc = new Document();
var db = new DocumentBuilder(doc);

// table

var table = db.StartTable();
var cell1 = db.InsertCell();
// cell1.CellFormat.HorizontalMerge = CellMerge.First; /* uncommenting this makes output wierd. */
var cell2 = db.InsertCell();
cell2.CellFormat.HorizontalMerge = CellMerge.Previous;

// subtable

var subTable = db.StartTable();
var subCell1 = db.InsertCell();
db.Write(“subTable.col1”);
var subCell2 = db.InsertCell();
db.Write(“subTable.col2”);
var subCell3 = db.InsertCell();
db.Write(“subTable.col3”);
db.EndRow();
db.EndTable();

// table

db.EndRow();

db.InsertCell();
db.Write(“table.col1”);
db.InsertCell();
db.Write(“table.col2”);

db.EndRow();
db.EndTable();

doc.Save(@“C:\out.docx”);
}

Result is always disappointing whatever I do as sub table remains in one parent cell. Results attached.

Thank you for help!
Hi Philo,

Thanks for your inquiry. Please use the following code snippet to achieve your requirements. Hope this helps. Please let us know if you have any more queries.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

Cell cell = builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.First;

builder.Write("Text in merged cells.");

builder.InsertCell();

// This cell is merged to the previous and should be empty.

builder.CellFormat.HorizontalMerge = CellMerge.Previous;

builder.EndRow();

builder.InsertCell();

builder.CellFormat.HorizontalMerge = CellMerge.None;

builder.Write("Text in one cell.");

builder.InsertCell();

builder.Write("Text in another cell.");

builder.EndRow();

builder.EndTable();

// nested table

// Move to the first cell of the outer table.

builder.MoveTo(cell.FirstParagraph);

// Build the inner table.

builder.InsertCell();

builder.Writeln("Inner Table Cell 1");

builder.InsertCell();

builder.Writeln("Inner Table Cell 2");

builder.EndTable();

doc.Save(MyDir + @"out.docx");