How to insert Table into a Shape

I was wondering how to insert a table into a rectangle shape.

This is my Shape where I want to insert a table into:

 var firstSectionWhite = new Shape(document, ShapeType.RoundedRectangle, Layout.Floating(
    new HorizontalPosition(15, LengthUnit.Pixel, HorizontalPositionAnchor.TopLeftCorner),
    new VerticalPosition(70, LengthUnit.Pixel, VerticalPositionAnchor.TopLeftCorner),
    new Size(pageSetup.PageWidth - 175, 100, LengthUnit.Point)));
            
firstSectionWhite.AdjustValues["adj"] = 5000;
firstSectionWhite.Outline.Fill.SetEmpty();
firstSectionWhite.Fill.SetSolid(Color.White);
ItemGroup.Add(firstSectionWhite);

and here is my Table:

List<string> Items = new List<string> { "Description: End User Computing", "Recovery Order: 25", "Mtpd: 02:00 hours" };

int rowCount = 3;
int columnCount = 2;

var table = new Table(document);
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Point);
            
section.Blocks.Add(table);

for (int i = 0; i < rowCount; i++)
{
    var row = new TableRow(document);
    table.Rows.Add(row);

    for (int c = 0; c < columnCount; c++)
    {
        var cell = new TableCell(document);
        row.Cells.Add(cell);

        cell.Blocks.Add(new Paragraph(document, "TestCells"));
    }
}

Thanks for any help

You need to use the TextBox class for that. Your code for creating shape stays almost the same. You only need to change the order of parameters, since ShapeType comes last.

var firstSectionWhite = new TextBox(document, 
    Layout.Floating(
        new HorizontalPosition(15, LengthUnit.Pixel, HorizontalPositionAnchor.TopLeftCorner),
        new VerticalPosition(70, LengthUnit.Pixel, VerticalPositionAnchor.TopLeftCorner),
        new Size(doc.Sections[0].PageSetup.PageWidth - 175, 100, LengthUnit.Point)), 
    ShapeType.RoundedRectangle);

Afterward, you just add the table to the shape’s Blocks collection.

firstSectionWhite.Blocks.Add(table);

I hope that is what you were looking for.

1 Like

It is working, thank you!