How to Create PowerPoint Presentation using Java

In this simple topic, we will walk you through how to create PowerPoint Presentation using Java in MS Windows, macOS, or Ubuntu operating systems. This topic covers the detailed steps to set up the environment and by using a few lines of easy code in Java PPT presentation can be generated.

Steps to Generate PowerPoint Presentation in Java

  1. Download and install Aspose.Slides for Java from the Maven repository
  2. Instantiate the Presentation class object to create an empty presentation
  3. Create a blank slide and add that to the presentation slides collection
  4. Using the AddAutoShape method, insert a Rectangle shape in the newly created slide
  5. Insert a text frame using the addTextFrame method and set the text related properties
  6. Save the presentation on the disk in PPTX format

The aforementioned steps in Java create PPTX file on the disk using the simple API interface and with no dependence on PowerPoint. Firstly, an empty presentation is created using the Presentation class instance, which is followed by adding a blank slide inside the presentation. Then, a text frame is added inside the shape and its respective textual properties are set before saving the presentation on the disk using the save method.

Code to Create PowerPoint Presentation using Java

import com.aspose.slides.FillType;
import com.aspose.slides.IAutoShape;
import com.aspose.slides.IPortionFormat;
import com.aspose.slides.ISlide;
import com.aspose.slides.ITextFrame;
import com.aspose.slides.License;
import com.aspose.slides.NullableBool;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ShapeType;
import com.aspose.slides.SlideLayoutType;
import java.awt.Color;
public class CreatePresentation {
public static void main(String[] args){
// Setting the linence for the product
License SlidesLicense = new License();
SlidesLicense.setLicense("Aspose.Total.lic");
// Create an empty presentation using the Presentation class instance
Presentation presentation = new Presentation();
// Insert a Blank slide inside the presentation
ISlide slide = presentation.getSlides().addEmptySlide(presentation.getLayoutSlides()
.getByType(SlideLayoutType.Blank));
// Add an auto-shape of Rectangle type
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 150, 300, 0);
// Fill the shape with Green color
autoShape.getFillFormat().setFillType(FillType.Solid);
autoShape.getFillFormat().getSolidFillColor().setColor(Color.GREEN);
// Adding some text inside the shape
ITextFrame textFrame = autoShape.addTextFrame("This is Aspose.Slides");
// Set text related properties
IPortionFormat portionFormat = textFrame.getParagraphs().get_Item(0)
.getPortions().get_Item(0).getPortionFormat();
portionFormat.getFillFormat().setFillType(FillType.Solid);
portionFormat.getFillFormat().getSolidFillColor().setColor(Color.RED);
portionFormat.setFontBold(NullableBool.True);
portionFormat.setFontItalic(NullableBool.True);
portionFormat.setFontHeight(14);
// Save the generated presentation on the disk
presentation.save("NewJavaPresentation.pptx", SaveFormat.Pptx);
}
}

In Java presentation can be generated using a few lines of code as given in the above example. You may also save the presentation in the other formats like PPT, PPS, PPSX, ODP, POT and POTX using the SaveFormat enumerator. The text inside the presentation can be customized by using the different options exposed by the ParagraphFormat and PortionFormat classes which include setting the options like text wrapping, text autofit, indentations, margins, bullets, text highlighting and strike-through.

In this topic, we have learned how using Java PowerPoint presentation in different formats can be created. If you are interested in converting presentation slides to SVG, please visit the details mentioned in the article on how to convert PPTX to SVG using Java.

 English