Introduction
Aspose.Slides also facilitates developers to add custom tables in their slides from scratch. This is one of the newest features added in Aspose.Slides since last few versions. In this topic, we will explain that how can developers create and add tables to their slides using Aspose.Slides.
Creating a Table from Scratch
Aspose.Slides 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 Position
- Set Table Parameters that are required to create a table. This step is Optional
- Add a Table to the slide according to specified Table Parameters
- Set the Alternative Text of the Table that can be used to identify this table instance in the slide. This step is also Optional
- Merge first two cells of the first row of the table. This step is Optional too
- Access the Text Frame of a Cell. It's Optional
- Add some text to the Text Frame. It's also Optional
- Write the modified presentation as a PPT file
Example:
[C#]
//Instantiate a Presentation object that represents a PPT file
Presentation pres=new Presentation("demo.ppt");
//Accessing a slide using its slide position
Slide slide = pres.GetSlideByPosition(2);
//Setting table parameters
int xPosition=880;
int yPosition=1400;
int tableWidth=4000;
int tableHeight=500;
int columns=4;
int rows=4;
double borderWidth=2;
//Adding a new table to the slide using specified table parameters
Table table=slide.Shapes.AddTable(xPosition,yPosition,tableWidth,tableHeight,
columns,rows,borderWidth,Color.Blue);
//Setting the alternative text for the table that can help in future to find
//and identify this specific table
table.AlternativeText="myTable";
//Merging two cells
table.MergeCells(table.GetCell(0,0),table.GetCell(1,0));
//Accessing the text frame of the first cell of the first row in the table
TextFrame tf=table.GetCell(0,0).TextFrame;
//If text frame is not null then add some text to the cell
if(tf!=null)
{
tf.Paragraphs[0].Portions[0].Text="Welcome to Aspose.Slides";
}
//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("demo.ppt")
'Accessing a slide using its slide position
Dim slide As Slide = pres.GetSlideByPosition(2)
'Setting table parameters
Dim xPosition As Integer = 880
Dim yPosition As Integer = 1400
Dim tableWidth As Integer = 4000
Dim tableHeight As Integer = 500
Dim columns As Integer = 4
Dim rows As Integer = 4
Dim borderWidth As Double = 2
'Adding a new table to the slide using specified table parameters
Table table=slide.Shapes.AddTable(xPosition,yPosition,tableWidth,tableHeight,
columns,rows,borderWidth,Color.Blue)
'Setting the alternative text for the table that can help in future to find
'and identify this specific table
table.AlternativeText="myTable"
'Merging two cells
table.MergeCells(table.GetCell(0,0),table.GetCell(1,0))
'Accessing the text frame of the first cell of the first row in the table
Dim tf As TextFrame = table.GetCell(0,0).TextFrame
'If text frame is not null then add some text to the cell
If tf <> Nothing Then
tf.Paragraphs(0).Portions(0).Text="Welcome to Aspose.Slides"
'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("demo.ppt")));
//Accessing a slide using its slide position
Slide slide = pres.getSlideByPosition(2);
//Setting table parameters
int xPosition=880;
int yPosition=1400;
int tableWidth=4000;
int tableHeight=500;
int columns=4;
int rows=4;
double borderWidth=2;
//Adding a new table to the slide using specified table parameters
Table table=slide.getShapes().addTable(xPosition,yPosition,tableWidth,tableHeight,
columns,rows,borderWidth,java.awt.Color.Blue);
//Setting the alternative text for the table that can help in future to find
//and identify this specific table
table.setAlternativeText("myTable");
//Merging two cells
table.mergeCells(table.getCell(0,0),table.getCell(1,0));
//Accessing the text frame of the first cell of the first row in the table
TextFrame tf=table.getCell(0,0).getTextFrame();
//If text frame is not null then add some text to the cell
if(tf!=null)
{
tf.getParagraphs().get(0).getPortions().get(0).setText("Welcome to Aspose.Slides");
}
//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:\\demo.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);
//Setting table parameters
$xPosition=880;
$yPosition=1400;
$tableWidth=4000;
$tableHeight=500;
$columns=4;
$rows=4;
$borderWidth=2.0;
//Creating Color enumeration so that it can be used later
$Color=new Java("java.awt.Color");
//Adding a new table to the slide using specified table parameters
$table=$slide->getShapes()->addTable($xPosition,$yPosition,$tableWidth,$tableHeight,
$columns,$rows,$borderWidth,$Color->Blue);
//Setting the alternative text for the table that can help in future to
//find and identify this specific table
$table->setAlternativeText("myTable");
//Merging two cells
$table->mergeCells($table->getCell(0,0),$table->getCell(1,0));
//Accessing the text frame of the first cell of the first row in the table
$tf=$table->getCell(0,0)->getTextFrame();
//If text frame is not null then add some text to the cell
if($tf!=null)
{
$tf->getParagraphs()->get(0)->getPortions()->get(0)->setText("Welcome to Aspose.Slides");
}
//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 above code snippet adds a table to the slide as shown below:
|
Figure: Table added to the slide
|