Introduction
Aspose.Slides allows developers to not only add custom tables in their slides but also access or manage the existing ones. In this topic, we will discuss about accessing a table that already exists in a slide.
Accessing an 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 a Table 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 Table object to control the table. For example, in our case, we have added a new row in the desired table
- Write the modified presentation as a PPT file
Example:
[C#]
//Instantiate a Presentation object that represents a PPT file
Presentation pres=new Presentation("table.ppt");
//Accessing a slide using its slide position
Slide slide = pres.GetSlideByPosition(2);
//Setting table object to null
Table table = null;
//Iterating through all shapes unless the desired table is found
for (int i = 0; i < slide.Shapes.Count; i++)
{
if (slide.Shapes[i] is Table)
{
table = (Table)slide.Shapes[i];
if(table.AlternativeText.Equals("myTable"))
{
System.Console.WriteLine("Table Found");
break;
}
}
}
//Adding a new row in the table
table.AddRow();
//Writing the presentation as a PPT file
pres.Write("C:\\modified.ppt");
[VB.NET]
'Instantiate a Presentation object that represents a PPT file
Dim pres As Presentation = New Presentation("table.ppt")
'Accessing a slide using its slide position
Dim slide As Slide = pres.GetSlideByPosition(2)
'Setting table object to Nothing
Dim table As table = Nothing
'Iterating through all shapes unless the desired table is found
For i = 0 To slide.Shapes.Count - 1
If TypeOf slide.Shapes(i) Is table Then
table = CType(slide.Shapes(i), table)
If table.AlternativeText.Equals("myTable") Then
System.Console.WriteLine("Table Found");
Exit For
Next
'Adding a new row in the table
table.AddRow()
'Writing the presentation as a PPT file
pres.Write("C:\\modified.ppt")
[Java]
try
{
//Instantiate a Presentation object that represents a PPT file
Presentation pres = new Presentation(new FileInputStream(new File("table.ppt")));
//Accessing a slide using its slide position
Slide slide = pres.getSlideByPosition(2);
//Setting table object to null
Table table = null;
//Iterating through all shapes unless the desired table is found
for (int i = 0; i < slide.getShapes().size(); i++)
{
if (slide.getShapes().get(i) instanceof Table)
{
table = (Table)slide.getShapes().get(i);
if(table.getAlternativeText().equals("myTable"))
{
System.out.println("Table Found");
break;
}
}
}
//Adding a new row in the table
table.addRow();
//Writing the presentation as a PPT file
pres.write(new FileOutputStream(new File("modified.ppt")));
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
[PHP]
<?php
//Using aspose.slides.jar file so that the classes inside the jar file
//can be used
java_require("aspose.slides.jar");
try
{
//Creating a file input stream to read the PPT file
$fistream=new Java("java.io.FileInputStream","C:\\table.ppt");
//Instantiate a Presentation object that represents a PPT file
$pres=new Java("com.aspose.slides.Presentation",$fistream);
//Accessing a slide using its slide position
$slide=$pres->getSlideByPosition(2);
//Creating Table class
$Table=new JavaClass("com.aspose.slides.Table");
//Setting table object to null
$table=null;
//Iterating through all shapes unless the desired table is found
for($i = 0; $i < $slide->getShapes()->size(); $i++)
{
if(java_instanceof($slide->getShapes()->get($i),$Table))
{
$table = $slide->getShapes()->get($i);
if($table->getAlternativeText()->equals("myTable"))
{
echo "Table Found";
break;
}
}
}
//Adding a new row in the table
$table->addRow();
//Creating a file output stream to write the output file
$fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");
//Writing the presentation as a PPT file
$pres->write($fostream);
//Closing the streams
$fistream->close();
$fostream->close();
}
catch(JavaException $ex)
{
echo $ex->toString();
}
?>
The original table Before executing the above code snippet is shown below:
|
Figure: Original table before modification
|
The above code snippet locates an existing table in the slide and then adds an extra row in the table as shown below:
|
Figure: Table with new row added to it
|
Note: You will notice that the new row added in the table is similar to the last row. It is because of the fact that Add New Row feature of Aspose.Slides simply clones the last row from the table and append it to the end of the table.