DEV Community


Posted on

Java - How to Create a PowerPoint Presentation

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, 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:

Enter fullscreen mode Exit fullscreen mode

If you are not using maven, then you can find the required jar files from the zip file available in this location. Include all the jar files into the application lib folder to run the sample code given in this tutorial.

Import Namespaces

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;
Enter fullscreen mode Exit fullscreen mode

Create a PowerPoint Document

//Create a Presentation object
Presentation presentation = new Presentation();
//Set the slide size type
Enter fullscreen mode Exit fullscreen mode

Get the First Slide and Set Background Image

By default, there is one blank slide preset in the newly created PowerPoint document.

//Get the first slide
ISlide slide = presentation.getSlides().get(0);
//Set the background image
String bgImage = "C:\\Users\\Administrator\\Desktop\\background.jpg";
BufferedImage image = FileInputStream(bgImage));
IImageData imageData = slide.getPresentation().getImages().append(image);
Enter fullscreen mode Exit fullscreen mode

Insert a Text Shape

//Insert a text shape
Rectangle2D textBounds = new Rectangle2D.Float(50, 50, 440, 100);
IAutoShape textShape = slide.getShapes().appendShape(ShapeType.RECTANGLE, textBounds);
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)");
//Set the text formatting
PortionEx portionEx =  textShape.getTextFrame().getTextRange().getParagraph().getFirstTextRange();
Enter fullscreen mode Exit fullscreen mode

Insert an Image Shape

//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 = FileInputStream(imagePath));
//Insert an image shape
IImageData iImageData = slide.getPresentation().getImages().append(bufferedImage);
IEmbedImage iEmbedImage = slide.getShapes().appendEmbedImage(shapeType, iImageData, imageBounds);
Enter fullscreen mode Exit fullscreen mode

Insert a List

//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);
for (int i = 0; i < listContent.length; i++) {
    //Set the formatting of the list text
    ParagraphEx paragraph = new ParagraphEx();
    paragraph.getTextRanges().get(0).setLatinFont(new TextFont("Calibri (Body)"));
Enter fullscreen mode Exit fullscreen mode

Insert a Table

//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
Enter fullscreen mode Exit fullscreen mode

Save the PowerPoint Document

//Save to file
presentation.saveToFile("output/CreatePowerPoint.pptx", FileFormat.PPTX_2013);
Enter fullscreen mode Exit fullscreen mode



Top comments (0)