Friday, 15 April 2022

Change Font Styles of Text in PowerPoint Documents using Java

When manipulating PowerPoint documents, you sometimes set font styles of text for aesthetic or practical reasons. For example, change the font name/font size, change the font color, make font bold, italic or underlined. This article will demonstrate how to programmatically accomplish the operation using Java codes.

DEPENDENCY

First of all, you need to download the package of a third-party library called Free Spire.Presentation for Java from this link, and add Spire.Presentation.jar to the Java program. Or if you use Maven, just type the following code in the pom.xml file to easily import the JAR file.

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

USING THE CODE

The following are detailed steps to change font styles of text in a PowerPoint document.

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

l  Get a specific slide of the document using Presentation.getSlides().get() method and get a specific text shape using the method provided by the ISlide interface.

l  Get the object of the TextFrame using IAutoShape.getTextFrame() method, and get a specified paragraph of the object using the method provided by the ITextFrameProperties interface.

l  Get the text content of the paragraph using ParagraphEx.getTextRanges().get() method.

l  Set the filling type of the text content using FillFormat.setFillType(FillFormatType value) method, and set the filling color using ColorFormat.setColor(ColorType value) method.

l  Use similar methods to specify another text shape and set the font styles, such as Italicize, bold, and underline the text.

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

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;

public class ChangeFontStyles {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();

//Load a PowerPoint sample file
presentation.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.pptx");

//Get the first text shape
IAutoShape shape1 = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);

//Get the first paragraph and change its font color
ParagraphEx paragraph = shape1.getTextFrame().getParagraphs().get(0);
PortionEx textRange = paragraph.getFirstTextRange();
textRange.getFormat().getFill().setFillType(FillFormatType.SOLID);
textRange.getFormat().getFill().getSolidColor().setColor(Color.red);

//Get the second text shape
IAutoShape shape2 = (IAutoShape) presentation.getSlides().get(0).getShapes().get(1);

//Get the third paragraph and make the text bold, italic and underlined
paragraph = shape2.getTextFrame().getParagraphs().get(2);
textRange = paragraph.getFirstTextRange();
textRange.getFormat().isBold(TriState.TRUE);
textRange.getFormat().isItalic(TriState.TRUE);
textRange.getFormat().setTextUnderlineType(TextUnderlineType.DASHED);

//Get the eighth paragraph and change its font name and size
paragraph = shape2.getTextFrame().getParagraphs().get(7);
textRange = paragraph.getFirstTextRange();
textRange.getFormat().setLatinFont(new TextFont("Calibri"));
textRange.getFormat().setFontHeight(22f);

//Save the output document
presentation.saveToFile("output/ChangeFontStyles.pptx", FileFormat.PPTX_2013);
}
}



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...