Table inside other table padding problem

Hi
I’m trying to create a table with an other table inside. I add the sub-table in a cell to be able to add it to a row of the main table.I don’t want any spaces beetween the two table, as if it was one table with splitted cells.It is working, except for the inner top and bottom padding. Left and right are OK.

public MemoryStream ExportWord()
{
    // Set the license.
    Aspose.Words.License license = new Aspose.Words.License();
    license.SetLicense(ASPOSE_WORDS_LICENSE_PATH);
 
    Document document = new Document();
    Section section = document.FirstSection;
    section.PageSetup.Orientation = Orientation.Portrait;
    section.PageSetup.PaperSize = PaperSize.A4;
    Body body = section.Body;
 
    Table mainTable = new Table(document);
    Row mainRow = new Row(document);
    mainRow.AppendChild(BuildCell("cell_left", document));
 
    Table subTable = new Table(document);
    Row subRow1 = new Row(document);
    subRow1.AppendChild(BuildCell("row_1_cell_1", document));
    subRow1.AppendChild(BuildCell("row_1_cell_2", document));
    subTable.AppendChild(subRow1);
    Row subRow2 = new Row(document);
    subRow2.AppendChild(BuildCell("row_2_cell_1", document));
    subRow2.AppendChild(BuildCell("row_2_cell_2", document));
    subTable.AppendChild(subRow2);
 
    Cell container = new Cell(document);
    container.AppendChild(subTable);
 
    container.CellFormat.BottomPadding = 0.0;
    container.CellFormat.LeftPadding = 0.0;
    container.CellFormat.TopPadding = 0.0;
    container.CellFormat.RightPadding = 0.0;
 
    mainRow.AppendChild(container);
    mainTable.AppendChild(mainRow);
    body.AppendChild(mainTable);
 
    mainTable.BottomPadding = 0.0;
    mainTable.LeftPadding = 0.0;
    mainTable.TopPadding = 0.0;
    mainTable.RightPadding = 0.0;
 
    // Save word document as stream ...
    document.UpdateTableLayout();
    MemoryStream stream = new MemoryStream();
    document.Save(stream, SaveFormat.Docx);
    // ... and return it
    return stream;
}
 
private Cell BuildCell(string content, Document document)
{
    Run run = new Run(document);
    run.Text = content;
    Paragraph paragraph = new Paragraph(document);
    paragraph.AppendChild(run);
    Cell cell = new Cell(document);
    cell.AppendChild(paragraph);
 
    cell.CellFormat.BottomPadding = 4.0;
    cell.CellFormat.LeftPadding = 4.0;
    cell.CellFormat.TopPadding = 4.0;
    cell.CellFormat.RightPadding = 4.0;
 
    return cell;
}

How can I remove those bottom and top spaces ?(see docx attachment)
Thanks
Adrian

Hi Steven,

Thanks for your inquiry. I have modified your code for cell padding. In your case, you need to set same padding for each cell in a row. Please let us know if you have any more queries.

public MemoryStream ExportWord()
{
    Document document = new Document();
    Section section = document.FirstSection;
    section.PageSetup.Orientation = Aspose.Words.Orientation.Portrait;
    section.PageSetup.PaperSize = Aspose.Words.PaperSize.A4;
    Body body = section.Body;
    Table mainTable = new Table(document);
    Row mainRow = new Row(document);
    mainRow.AppendChild(BuildCell("cell_left", document));
    Table subTable = new Table(document);
    Row subRow1 = new Row(document);
    subRow1.AppendChild(BuildCell("row_1_cell_1", document));
    subRow1.AppendChild(BuildCell("row_1_cell_2", document));
    subTable.AppendChild(subRow1);
    Row subRow2 = new Row(document);
    subRow2.AppendChild(BuildCell("row_2_cell_1", document));
    subRow2.AppendChild(BuildCell("row_2_cell_2", document));
    subTable.AppendChild(subRow2);
    Cell container = new Cell(document);
    container.AppendChild(subTable);
    // container.CellFormat.BottomPadding = 0.0;
    // container.CellFormat.LeftPadding = 0.0;
    // container.CellFormat.TopPadding = 0.0;
    // container.CellFormat.RightPadding = 0.0;
    mainRow.AppendChild(container);
    mainTable.AppendChild(mainRow);
    body.AppendChild(mainTable);
    mainTable.BottomPadding = 0.0;
    mainTable.LeftPadding = 0.0;
    mainTable.TopPadding = 0.0;
    mainTable.RightPadding = 0.0;
    foreach(Cell cell in mainRow)
    {
        cell.CellFormat.BottomPadding = 0.0;
        cell.CellFormat.LeftPadding = 0.0;
        cell.CellFormat.TopPadding = 0.0;
        cell.CellFormat.RightPadding = 0.0;
    }
    // Save word document as stream ...
    document.UpdateTableLayout();
    MemoryStream stream = new MemoryStream();
    // ... and return it
    return stream;
}

Hi

Thanks for the solution, it’s already much better than mine, but now I have “cell_left” with a padding of 0. Can’t I have different paddings for each cell in a same row ?

Regards

Hi Steven,

Thanks for your query. Aspose.Words mimics the same behavior as MS Word do. If you set different padding for each cell in the same row you will get the same behavior. I have created a sample document by using MS Word and have set different cell padding for each cell in the same row. Please see the attached images and document.

Please let us know if you have any more queries.