Java - How to Create a PowerPoint Presentation
Tags: java, programming, powerpoint, creation
Published: 2022-05-06 02:54:26

Creating an attractive PowerPoint presentation is a meticulous task that needs the cooperation of brain, eyes, and hands. In order to achieve a perfect effect, we need to constantly fine tune the details, such as adjusting the size and position of a shape, or changing the color of text. Due to this reason, creating PowerPoint documents manually is typically more efficient than using code. However, in certain cases, we may have to do it programmatically. 

In this article, you’ll learn how to create a simple PowerPoint document and insert basic elements (including text shape, image shape, list, and table) into it by using [Free Spire.Presentation for Java](https://www.e-iceblue.com/Introduce/free-presentation-for-java.html), which is a free class library for processing PowerPoint documents in Java applications. The main tasks of this tutorial are as follows.

- Create a PowerPoint Document
- Get the First Slide and Set Background Image
- Insert a Text Shape
- Insert an Image Shape
- Insert a List
- Insert a Table 
- Save the PowerPoint Document

##Add Spire.Presentation.jar as Dependency
If you are working on a maven project, you can include the dependency in pom.xml file using this:

```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>
```
If you are not using maven, then you can find the required jar files from the zip file available in [this location](https://www.e-iceblue.com/Download/presentation-for-java-free.html). Include all the jar files into the application lib folder to run the sample code given in this tutorial.

##Import Namespaces 

```java
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
```
##Create a PowerPoint Document

```java
//Create a Presentation object
Presentation presentation = new Presentation();
//Set the slide size type
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
```
#Get the First Slide and Set Background Image
By default, there is one blank slide preset in the newly created PowerPoint document.

```java
//Get the first slide
ISlide slide = presentation.getSlides().get(0);
//Set the background image
String bgImage = "C:\\Users\\Administrator\\Desktop\\background.jpg";
BufferedImage image = ImageIO.read(new FileInputStream(bgImage));
IImageData imageData = slide.getPresentation().getImages().append(image);
slide.getSlideBackground().setType(BackgroundType.CUSTOM);
slide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
slide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
```
##Insert a Text Shape

```java
//Insert a text shape
Rectangle2D textBounds = new Rectangle2D.Float(50, 50, 440, 100);
IAutoShape textShape = slide.getShapes().appendShape(ShapeType.RECTANGLE, textBounds);
textShape.getFill().setFillType(FillFormatType.NONE);
textShape.getLine().setFillType(FillFormatType.NONE);
String text = "This article shows you how to create a PowerPoint document from scratch in Java using Spire.Presentation for Java. " +
        "The following tasks are involved in this tutorial.";
TextFont titleFont = new TextFont("Calibri (Body)");
textShape.getTextFrame().setText(text);
//Set the text formatting
textShape.getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
PortionEx portionEx =  textShape.getTextFrame().getTextRange().getParagraph().getFirstTextRange();
portionEx.getFill().setFillType(FillFormatType.SOLID);
portionEx.getFill().getSolidColor().setColor(Color.blue);
portionEx.setLatinFont(titleFont);
portionEx.setFontHeight(20f);
```
##Insert an Image Shape

```java
//Load an image
String imagePath = "C:\\Users\\Administrator\\Desktop\\java-logo.png";
Rectangle2D imageBounds = new Rectangle2D.Double(500, 80, 400, 200);
ShapeType shapeType = ShapeType.RECTANGLE;
BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));
//Insert an image shape
IImageData iImageData = slide.getPresentation().getImages().append(bufferedImage);
IEmbedImage iEmbedImage = slide.getShapes().appendEmbedImage(shapeType, iImageData, imageBounds);
iEmbedImage.getLine().setFillType(FillFormatType.NONE);
iEmbedImage.getPictureFill().setFillType(PictureFillType.STRETCH);
```
##Insert a List
```java
//Insert a bulleted list
Rectangle2D listBounds = new Rectangle2D.Float(50, 160, 440, 180);
String[] listContent = new String[]{
        " Create a PowerPoint Document",
        " Set the background image",
        " Insert a text shape",
        " Insert an image shape",
        " Insert a bulleted list",
        " Insert a table",
        " Save the PowerPoint document"
};
IAutoShape autoShape = slide.getShapes().appendShape(ShapeType.RECTANGLE, listBounds);
autoShape.getTextFrame().getParagraphs().clear();
autoShape.getFill().setFillType(FillFormatType.NONE);
autoShape.getLine().setFillType(FillFormatType.NONE);
for (int i = 0; i < listContent.length; i++) {
    //Set the formatting of the list text
    ParagraphEx paragraph = new ParagraphEx();
    autoShape.getTextFrame().getParagraphs().append(paragraph);
    paragraph.setText(listContent[i]);
    paragraph.getTextRanges().get(0).getFill().setFillType(FillFormatType.SOLID);
    paragraph.getTextRanges().get(0).getFill().getSolidColor().setColor(Color.black);
    paragraph.getTextRanges().get(0).setLatinFont(new TextFont("Calibri (Body)"));
    paragraph.getTextRanges().get(0).setFontHeight(20);
    paragraph.setBulletType(TextBulletType.SYMBOL);
}
```
##Insert a Table 
```java
//Define column widths and row heights
Double[] widths = new Double[]{100d, 100d, 100d, 150d, 100d, 100d};
Double[] heights = new Double[]{15d, 15d, 15d, 15d};
//Add a table
ITable table = presentation.getSlides().get(0).getShapes().appendTable(50, 350, widths, heights);
//Define table data
String[][] data = new String[][]{
        {"Emp#", "Name", "Job", "HiringD", "Salary", "Dept#"},
        {"7369", "Andrews", "Engineer", "05/12/2021", "1600.00", "20"},
        {"7499", "Nero", "Technician", "03/23/2021", "1800.00", "30"},
        {"7566", "Martin", "Manager", "12/21/2021", "2600.00", "10"},
};
//Loop through the row and column of the table
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 a built-in style to table
table.setStylePreset(TableStylePreset.LIGHT_STYLE_1_ACCENT_4);
```
##Save the PowerPoint Document
```java
//Save to file
presentation.saveToFile("output/CreatePowerPoint.pptx", FileFormat.PPTX_2013);
```
##Output

![PowerPointScreenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rt8g2h86e5nryfogfkm6.png)