Manage Table

Create Table from Scratch

Aspose.Slides for C++ has provided the simplest API to create tables in an easiest way. To create a table in a slide and perform some basic operations on the table, please follow the steps below:

  • Create an instance of Presentation class.
  • Obtain the reference of a slide by using its Index.
  • Define Array of Columns with Width.
  • Define Array of Rows with Height.
  • Add a Table to the slide using AddTable method exposed by IShapeCollection object.
  • Iterate through each Cell to apply formatting to the Top, Bottom, Right, Left Borders.
  • Merge first two cells of the first row of the table.
  • Access the Text Frame of a Cell.
  • Add some text to the Text Frame.
  • Save the modified presentation as a PPTX file.
// Instantiate Presentation class that represents PPTX file
auto pres = System::MakeObject<Presentation>();

// Access first slide
auto sld = pres->get_Slides()->idx_get(0);

// Define columns with widths and rows with heights
auto dblCols = System::MakeArray<double>({ 50, 50, 50 });
auto dblRows = System::MakeArray<double>({ 50, 30, 30, 30, 30 });

// Add table shape to slide
auto tbl = sld->get_Shapes()->AddTable(100.0f, 50.0f, dblCols, dblRows);

// Set border format for each cell
for (int32_t row = 0; row < tbl->get_Rows()->get_Count(); row++)
{
    for (int32_t cell = 0; cell < tbl->get_Rows()->idx_get(row)->get_Count(); cell++)
    {
        auto cellFormat = tbl->get_Rows()->idx_get(row)->idx_get(cell)->get_CellFormat();

        cellFormat->get_BorderTop()->get_FillFormat()->set_FillType(FillType::Solid);
        cellFormat->get_BorderTop()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderTop()->set_Width(5);

        cellFormat->get_BorderBottom()->get_FillFormat()->set_FillType((FillType::Solid));
        cellFormat->get_BorderBottom()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderBottom()->set_Width(5);

        cellFormat->get_BorderLeft()->get_FillFormat()->set_FillType(FillType::Solid);
        cellFormat->get_BorderLeft()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderLeft()->set_Width(5);

        cellFormat->get_BorderRight()->get_FillFormat()->set_FillType(FillType::Solid);
        cellFormat->get_BorderRight()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderRight()->set_Width(5);
    }
}
// Merge cells 1 & 2 of row 1
tbl->MergeCells(tbl->get_Rows()->idx_get(0)->idx_get(0), tbl->get_Rows()->idx_get(1)->idx_get(1), false);

// Add text to the merged cell
tbl->get_Rows()->idx_get(0)->idx_get(0)->get_TextFrame()->set_Text(u"Merged Cells");

// Save PPTX to Disk
pres->Save(u"table.pptx", SaveFormat::Pptx);

Access Existing Table

To access a table that already exists in a slide, please follow the steps below:

  • Create an instance of Presentation class.
  • Obtain the reference of a slide (that contains the table) by using its Position.
  • Create an ITable object and set it to null.
  • Iterate through all Shapes until you find the Table. If a slide contains only one table then you can simply check a shape and if it is found to be a Table then just typecast it as a Table object. But, if the slide contains more than one tables then it’s better to find your desired table using its Alternative Text.
  • After the Table is found, you can use ITable object to control the table. For example, in our case, we have added a new row in the desired table.
  • Save the modified presentation as a PPT file.
// Instantiate Presentation class that represents PPTX
auto pres = System::MakeObject<Presentation>(u"UpdateExistingTable.pptx");

// Access the first slide
auto sld = pres->get_Slides()->idx_get(0);

// Initialize null Table
System::SharedPtr<ITable> tbl;

// Iterate through the shapes and set a reference to the table found
for (const auto& shp : System::IterateOver(sld->get_Shapes()))
{
    if (System::ObjectExt::Is<ITable>(shp))
    {
        tbl = System::ExplicitCast<ITable>(shp);
    }
}

// Set the text of the first column of second row
tbl->idx_get(0, 1)->get_TextFrame()->set_Text(u"New");

//Write the PPTX to Disk
pres->Save(u"table1_out.pptx", SaveFormat::Pptx);

Align Text in Table

Aspose.Slides for C++ has provided the simplest API to work with tables in an easiest way. To clone a table row or column in a slide, please follow the steps below:

  • Create an instance of Presentation class.
  • Obtain the reference of a slide by using its Index.
  • Insert table in the slide.
  • Access text frame.
  • Access paragraph.
  • Align text vertically.
  • Save the presentation as a PPTX file.
// Create an instance of Presentation class
auto presentation = System::MakeObject<Presentation>();

// Get the first slide 
auto slide = presentation->get_Slides()->idx_get(0);

// Define columns with widths and rows with heights
auto dblCols = System::MakeArray<double>({ 120, 120, 120, 120 });
auto dblRows = System::MakeArray<double>({ 100, 100, 100, 100 });

// Add table shape to slide
auto tbl = slide->get_Shapes()->AddTable(100.0f, 50.0f, dblCols, dblRows);
tbl->idx_get(1, 0)->get_TextFrame()->set_Text(u"10");
tbl->idx_get(2, 0)->get_TextFrame()->set_Text(u"20");
tbl->idx_get(3, 0)->get_TextFrame()->set_Text(u"30");

// Accessing the text frame
auto txtFrame = tbl->idx_get(0, 0)->get_TextFrame();

// Create the Paragraph object for text frame
auto paragraph = txtFrame->get_Paragraphs()->idx_get(0);

// Create Portion object for paragraph
auto portion = paragraph->get_Portions()->idx_get(0);
portion->set_Text(u"Text here");
portion->get_PortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
portion->get_PortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());

// Aligning the text vertically
auto cell = tbl->idx_get(0, 0);
cell->set_TextAnchorType(TextAnchorType::Center);
cell->set_TextVerticalType(TextVerticalType::Vertical270);

// Save Presentation
presentation->Save(u"Vertical_Align_Text_out.pptx", SaveFormat::Pptx);

Set Text Formatting on Table Level

Aspose.Slides for C++ has provided the simplest API to create tables in an easiest way. In order to remove Text Formatting from table cells, please follow the steps below:

  • Create an instance of Presentation class.
  • Obtain the reference of a slide by using its Index.
  • Access Table from Slide.
  • Set Table Cells Font Height.
  • Set Table Cells Text Alignment and right Margin in one Call.
  • Set Table Cells Vertical Type.
  • Save the modified presentation as a PPTX file.
// Create an instance of Presentation class
auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slides()->idx_get(0);

// let's say that the first shape on the first slide is a table
auto someTable = System::AsCast<ITable>(presentation->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0));

// setting table cells' font height
auto portionFormat = System::MakeObject<PortionFormat>();
portionFormat->set_FontHeight(25.0f);
someTable->SetTextFormat(portionFormat);

// setting table cells' text alignment and right margin in one call
auto paragraphFormat = System::MakeObject<ParagraphFormat>();
paragraphFormat->set_Alignment(TextAlignment::Right);
paragraphFormat->set_MarginRight(20.0f);
someTable->SetTextFormat(paragraphFormat);

// setting table cells' text vertical type
auto textFrameFormat = System::MakeObject<TextFrameFormat>();
textFrameFormat->set_TextVerticalType(TextVerticalType::Vertical);
someTable->SetTextFormat(textFrameFormat);

presentation->Save(u"result.pptx", SaveFormat::Pptx);

Numbering in Standard Table

In a standard table numeration of cells is straightforward and zero-based. The first cell in a table is indexed as 0,0 (column 0, row 0). For example, the cells in a table with 4 columns and 4 rows will be numbered accordingly:

(0, 0) (1, 0) (2, 0) (3, 0)
(0, 1) (1, 1) (2, 1) (3, 1)
(0, 2) (1, 2) (2, 2) (3, 2)
(0, 3) (1, 3) (2, 3) (3, 3)
// Instantiate Presentation class that represents PPTX file
auto pres = System::MakeObject<Presentation>();

// Access first slide
auto sld = pres->get_Slides()->idx_get(0);

// Define columns with widths and rows with heights
auto dblCols = System::MakeArray<double>({ 70, 70, 70, 70 });
auto dblRows = System::MakeArray<double>({ 70, 70, 70, 70 });

// Add table shape to slide
auto tbl = sld->get_Shapes()->AddTable(100.0f, 50.0f, dblCols, dblRows);

// Set border format for each cell
for (const auto& row : tbl->get_Rows())
{
    for (const auto& cell : row)
    {
        auto cellFormat = cell->get_CellFormat();
        cellFormat->get_BorderTop()->get_FillFormat()->set_FillType(FillType::Solid);
        cellFormat->get_BorderTop()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderTop()->set_Width(5);

        cellFormat->get_BorderBottom()->get_FillFormat()->set_FillType(FillType::Solid);
        cellFormat->get_BorderBottom()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderBottom()->set_Width(5);

        cellFormat->get_BorderLeft()->get_FillFormat()->set_FillType(FillType::Solid);
        cellFormat->get_BorderLeft()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderLeft()->set_Width(5);

        cellFormat->get_BorderRight()->get_FillFormat()->set_FillType(FillType::Solid);
        cellFormat->get_BorderRight()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
        cellFormat->get_BorderRight()->set_Width(5);
    }
}

// Write PPTX to Disk
pres->Save(u"StandardTables_out.pptx", SaveFormat::Pptx);

Lock Aspect Ratio of Table

The aspect ratio of a geometric shape is the ratio of its sizes in different dimensions. You can lock aspect ratio of table using get_GraphicalObjectLock()::get(set)_AspectRatioLocked() methods. Below code example shows how to use this methods.

auto pres = System::MakeObject<Presentation>(u"pres.pptx");
auto table = System::ExplicitCast<ITable>(pres->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0));

Console::WriteLine(u"Lock aspect ratio set: {0}", table->get_GraphicalObjectLock()->get_AspectRatioLocked());

// invert
table->get_GraphicalObjectLock()->set_AspectRatioLocked(!table->get_GraphicalObjectLock()->get_AspectRatioLocked());

Console::WriteLine(u"Lock aspect ratio set: {0}", table->get_GraphicalObjectLock()->get_AspectRatioLocked());

pres->Save(u"pres-out.pptx", SaveFormat::Pptx);