Microsoft PowerPoint includes 11 built-in slide layouts as below. In our daily work, we can use the slide layouts to reasonably and concisely lay out the text, images or other content in PowerPoint documents.
Requirement and Solution
Now, I want to insert the slide layouts into the slides using Java
codes without installing Microsoft PowerPoint. Through survey and test, I find
a free Java API called FreeSpire.Presentation for Java can do it. This component can not only set a
single layout but also several different layouts.
Dependency
Before running codes, we need add Spire.presentation.jar in the library
to IDEA. Download the package from the link and manually add the Jar file, or
reference it by using the following Maven configuration in the pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code
Set a single layout
The following codes are used to set a layout called Title Slide for the first slide.
import com.spire.presentation.*;
public class SingleLayout {
public static void main(String[] args) throws Exception {
//Create an instance of Presentation
Presentation ppt = new Presentation();
//Remove the default slide
ppt.getSlides().removeAt(0);
//Append a slide and set the layout for slide
ISlide slide = ppt.getSlides().append(SlideLayoutType.TITLE);
//Add content for Title and Text
IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
shape.getTextFrame().setText("Spire.Presentation for Java");
shape = (IAutoShape)slide.getShapes().get(1);
shape.getTextFrame().setText("Set the Layout of Slide as Title");
//Save the document
ppt.saveToFile("output/SingleSlideLayout.pptx", FileFormat.PPTX_2013);
ppt.dispose();
}
}
Output
Set multiple different layouts
The following codes are designed to set 11 different layouts for slides. The layouts include Title Slide, Title Only, Blank, Title and Content, Section Header, Two Content, Comparison, Content with Caption, Picture with Caption, Title and Vertical Text, Vertical Title and Text.
import com.spire.presentation.*;
public class MultipleLayout {
public static void main(String[] args) throws Exception {
//Create an instance of Presentation
Presentation presentation = new Presentation();
//Remove the default slide
presentation.getSlides().removeAt(0);
//Loop through slide layouts
for (SlideLayoutType type : SlideLayoutType.values())
{
//Append slide by specified slide layout
presentation.getSlides().append(type);
}
//Save the document
presentation.saveToFile("output/MultipleSlideLayout.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}Output
No comments:
Post a Comment