Free Support Forum - aspose.com

Insert table within cell (nested table)

Hi, guys!

I have the problem with inserting table inside cell. I want to insert table within cell with several columns.

My sample code is:

private void WriteTable(bool insertInnerTable)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

var isOuterTable = !_builder.CurrentParagraph.IsInCell;

var table = _builder.StartTable();

var columnsCount = 2;

var rowCount = 3;

if (isOuterTable)

{

for (int c = 0; c < columnsCount; c++)

{

Cell cell = _builder.InsertCell();

cell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50);

cell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;

_builder.Font.Bold = false;

_builder.MoveTo(cell.FirstParagraph);

_builder.Write(string.Format("header {0}", c + 1));

}

_builder.EndRow();

_builder.CellFormat.ClearFormatting();

_builder.RowFormat.ClearFormatting();

}

for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)

{

for (int columnIndex = 0; columnIndex < columnsCount; columnIndex++)

{

Cell cell = _builder.InsertCell();

cell.CellFormat.PreferredWidth = PreferredWidth.FromPercent(50);

if (insertInnerTable)

{

WriteTable(false);

}

else

{

_builder.Write("value");

}

}

_builder.EndRow();

rowIndex++;

}

table.AllowAutoFit = false;

_builder.EndTable();

}

I got:

header 1

header 2

value

value

value

value

value

value

value

value

value

value

value

value

value

value

value

value

<o:p></o:p>

As you see, columns in tables are not the same widths from row to row. Why? What am I doing wrong?

Thanks,

Yulia.

Hi Yulia,


Thanks for your inquiry. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v11.10.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v11.10.0 and let us know how it goes on your side. I hope, this will help.

I have used the following code snippet with your shared function. Please find the output document in attachment.

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> doc = new Document();<o:p></o:p>

_builder = new DocumentBuilder(doc);

WriteTable(true);

doc.Save(MyDir + "AsposeOut.docx");


Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.