Wednesday, 20 April 2022

Set the Indentation, Margin and Spacing for Paragraphs in PowerPoint Slides

To make the text on PowerPoint slides easier to read, you might want to set indentation, margin and spacing for the paragraph where the text is located. This article will demonstrate how to programmatically perform the operation using Java codes.

DEPENDENCY

Before running codes, you need to download the package of Free Spire.Presentation for Java from this link, find Spire.Presentation.jar in the lib folder and add it to your Java program. Or if you use Maven, just type the following code in the pom.xml file to import the JAR file to IntelliJ IDEA.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.Presentation for Java supports setting the indentation, margin and spacing for a specified paragraph in a PowerPoint slide. The following are detailed steps.

l  Create a Presentation instance and load a PowerPoint sample document using Presentation.loadFromFile() method.

l  Get a specified slide using Presentation.getSlides().get() method and get a specified shape in the slide using ShapeList.get() method.

l  Get a specified paragraph in the shape using ParagraphList.get() method and set the indentation, margin and spacing for the paragraph using ParagraphPropertiesEx.setIndent(),ParagraphPropertiesEx.setLeftMargin() and ParagraphPropertiesEx.setSpaceBefore() methods.

l  Save the output document to another file using Presentation.saveToFile() method.

import com.spire.presentation.*;

public class IndentStyle {
public static void main(String[] args) throws Exception {
//Load the sample document
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.pptx");

//get the shapes
IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(1);

//set the indent, margin and space for the first paragraph
shape.getTextFrame().getParagraphs().get(1).setIndent(30);
shape.getTextFrame().getParagraphs().get(1).setLeftMargin(10);
shape.getTextFrame().getParagraphs().get(1).setSpaceAfter(10);

//set the indent, margin and space for the third paragraph
shape.getTextFrame().getParagraphs().get(3).setIndent(-150);
shape.getTextFrame().getParagraphs().get(3).setLeftMargin(40);
shape.getTextFrame().getParagraphs().get(3).setSpaceBefore(0);
shape.getTextFrame().getParagraphs().get(3).setSpaceAfter(0);

//save the document to file
presentation.saveToFile("output/IndentStyle.pptx", FileFormat.PPTX_2010);
}
}


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...