A PowerPoint slideshow is a type of document that is mean to be shown to audiences to convey a piece of information among which data and graphics are very important. Creating tables is the simplest way to show those data and graphics without wasting any time or work in vain.
The tutorial in this article will show you how to create a table in your presentation and then manipulate it according to your requirements. It’s worth mentioning that I will do these actions in Java program using a free API called FreeSpire.Presentation for Java without installing Microsoft PowerPoint or any other component.
IMPORT SPIRE.PRESENTATION.JAR IN YOUR IDEA
Before typing codes, you need to
create a test environment. In addition to JDK and Intellij IDEA, Spire.Presentation.jar
located at Free Spire.Presentation for Java should be imported in your IDEA.
There are two methods as follows to do it.
Method 1. Download the jar file from here to your local disk, and add it as a dependency in your project.
Method 2. If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
CREATE A TABLE
import com.spire.presentation.*;
public class CreateTable {
public static void main(String[] args) throws Exception {
//create a Presentation instance
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//define column widths and row heights
Double[] widths = new Double[]{100d, 100d, 150d, 140d, 120d};
Double[] heights = new Double[]{15d, 15d, 15d, 15d,15d};
//add a table
ITable table = presentation.getSlides().get(0).getShapes().appendTable((float)(presentation.getSlideSize().getSize().getWidth() - 650)/2, 40, widths, heights);
//define data
String[][] data = new String[][]
{
{"Name", "Capital", "Continent", "Area", "Population"},
{"Indonesia", "Jakarta", "Southeast Asia", "1913578.68", "262000000"},
{"Bangladesh", "Dhaka", "South Aisa", "147570", "164700000"},
{"Mexican", "Mexico City", "North America", "1964375", "123000000"},
{"China", "Beijing", "East Asia", "9600000", "1400500000"},
};
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
//fill the table with data
table.get(j, i).getTextFrame().setText(data[i][j]);
//align text in each cell to center
table.get(j, i).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
}
}
//apply built-in table style
table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_2);
//save the document
presentation.saveToFile("output/InsertTable.pptx", FileFormat.PPTX_2013);
}
}Output
MANIPULATE A TABLE
1. Access an existing table
//load a PowerPoint file containing table
Presentation presentation = new Presentation();
presentation.loadFromFile("Table.pptx");
//get the table from the specified slide
ITable table = null;
for (Object shape: presentation.getSlides().get(0).getShapes()
) {
if (shape instanceof ITable){
table = (ITable)shape;
}
}2. Add a row or a column
//add a row by duplicating the last row
table.getTableRows().append(table.getTableRows().get(table.getTableRows().getCount()-1));
//obtain the new row added
TableRow lastRow = table.getTableRows().get(table.getTableRows().getCount()-1);
//add a column by duplicating the last columntable.getColumnsList().add(table.getColumnsList().get(table.getColumnsList().getCount()-1));
//obtain the new column added
TableColumn lastColumn = table.getColumnsList().get(table.getColumnsList().getCount()-1);3. Insert a specific row or column to the specified position
table.getTableRows().insert(0, table.getTableRows().get(0));
table.getColumnsList().insert(0, table.getColumnsList().get(0));4. Delete a row or a column
table.getTableRows().removeAt(0, false);
table.getColumnsList().removeAt(0, false);5. Merge cells
table.mergeCells(table.get(0,0), table.get(0,1), false);6. Split a cell
table.get(0,0).Split(3,2);7. Set the row height or column width
table.getTableRows().get(0).setHeight(50);
table.getColumnsList().get(0).setWidth(100);8. Fill a cell with picture
table.get(0,0).getFillFormat().setFillType(FillFormatType.PICTURE);
table.get(0,0).getFillFormat().getPictureFill().getPicture().setUrl((new java.io.File("bkg.jpg")).getAbsolutePath());9. Fill a cell with solid colortable.get(0,1).getFillFormat().setFillType(FillFormatType.SOLID);
table.get(0,1).getFillFormat().getSolidColor().setColor(Color.blue);10. Set border color
table.setTableBorder(TableBorderType.All, 1, Color.black);
11. Set the text color
table.get(0,0).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
table.get(0,0).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).getFill().getSolidColor().setColor(color.red);12. Set the text alignment within a celltable.get(0,0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);13. Set the font name and font size of text in a cell
table.get(0,0).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Calibri"));
table.get(0,0).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setFontHeight(20f);
No comments:
Post a Comment