Java (481)
A PowerPoint presentation, developed by Microsoft Corporation, is a versatile file format used for creating visually captivating and interactive content. It includes rich features and multiple elements such as text and images, making it a powerful tool for various scenarios, such as business introductions and academic speeches. If you need to edit or manipulate the text of PowerPoint, programmatically extracting it and saving it to a new file is an effective approach. In this article, we will show you how to extract text from PowerPoint using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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</artifactId>
<version>11.1.1</version>
</dependency>
</dependencies>
Extract Text from the Whole PowerPoint File
Spire.Presentation for Java supports looping through all slides and extracting text from the paragraphs on each slide using ParagraphEx.getText() method. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample presentation using Presentation.loadFromFile() method.
- Create an object of StringBuilder class.
- Loop through shapes in each slide and paragraphs in each shape.
- Extract all text from these slides by calling ParagraphEx.getText() method and append the extracted text to StringBuilder object.
- Create an object of FileWriter class, and write the extracted text to a new .txt file.
- Java
import com.spire.presentation.*;
import java.io.*;
public class ExtractText {
public static void main(String[] args) throws Exception {
//Create an object of Presentation class
Presentation presentation = new Presentation();
//Load a sample presentation
presentation.loadFromFile("sample.pptx");
//Create a StringBuilder object
StringBuilder buffer = new StringBuilder();
//Loop through each slide and extract text
for (Object slide : presentation.getSlides()) {
for (Object shape : ((ISlide) slide).getShapes()) {
if (shape instanceof IAutoShape) {
for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
buffer.append(((ParagraphEx) tp).getText()+"\n");
}
}
}
}
//Write the extracted text to a new .txt file
FileWriter writer = new FileWriter("output/ExtractAllText.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
presentation.dispose();
}
}

Extract Text from the Specific Slide
Spire.Presentation for Java also supports users to extract text from the specific slide. Simply get the desired slide by calling Presentation.getSlides().get() method before extracting text from the paragraphs on it. The following are detailed steps.
- Create an object of Presentation class.
- Load a sample presentation using Presentation.loadFromFile() method.
- Create an object of StringBuilder class.
- Get the first slide of this file by calling Presentation.getSlides().get() method.
- Loop through each shape and the paragraphs in each shape.
- Extract the text from the first slide by calling ParagraphEx.getText() method and append the extracted text to StringBuilder object.
- Create an object of FileWriter class, and write the extracted text to a new .txt file.
- Java
import com.spire.presentation.*;
import java.io.*;
public class ExtractText {
public static void main(String[] args) throws Exception {
//Create an object of Presentation class
Presentation presentation = new Presentation();
//Load a sample presentation
presentation.loadFromFile("sample.pptx");
//Create a StringBuilder object
StringBuilder buffer = new StringBuilder();
//Get the first slide of the presentation
ISlide Slide = presentation.getSlides().get(0);
//Loop through each paragraphs in each shape and extract text
for (Object shape : Slide.getShapes()) {
if (shape instanceof IAutoShape) {
for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
buffer.append(((ParagraphEx) tp).getText()+"\n");
}
}
}
//Write the extracted text to a new .txt file
FileWriter writer = new FileWriter("output/ExtractSlideText.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
presentation.dispose();
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Occasionally, you may need to protect PowerPoint documents. For instance, when you want to prevent unauthorized users from viewing and editing a PowerPoint document. Conversely, sometimes you may also need to unprotect PowerPoint documents. For example, when you wish to make a password-protected PowerPoint document accessible to everyone. In this article, we will introduce how to protect or unprotect PowerPoint documents in Java using Spire.Presentation for Java.
- Protect a PowerPoint Document with a Password
- Mark a PowerPoint Document as Final
- Remove Password Protection from a PowerPoint Document
- Remove Mark as Final Option from a PowerPoint Document
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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</artifactId>
<version>11.1.1</version>
</dependency>
</dependencies>
Protect a PowerPoint Document with a Password in Java
You can protect a PowerPoint document with a password to ensure that only the people who have the right password can view it.
The following steps demonstrate how to protect a PowerPoint document with a password:
- Initialize an instance of Presentation class.
- Load a PowerPoint document using Presentation.loadFromFile() method.
- Encrypt the document with a password using Presentation.encrypt() method.
- Save the result document using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class ProtectPPTWithPassword {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a PowerPoint document
presentation.loadFromFile("Sample.pptx");
//Encrypt the document with a password
presentation.encrypt("your password");
//Save the result document
presentation.saveToFile("Encrypted.pptx", FileFormat.PPTX_2013);
}
}

Mark a PowerPoint Document as Final in Java
You can mark a PowerPoint document as final to inform readers that the document is final and no further editing is expected.
The following steps demonstrate how to mark a PowerPoint document as final:
- Initialize an instance of Presentation class.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Mark the document as final using Presentation.getDocumentProperty().set() method.
- Save the result document using Presentation.SaveToFile() method.
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class MarkPPTAsFinal {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load a PowerPoint document
ppt.loadFromFile("Sample.pptx");
//Mark the document as final
ppt.getDocumentProperty().set("_MarkAsFinal", true);
//Save the result document
ppt.saveToFile("MarkAsFinal.pptx", FileFormat.PPTX_2013);
}
}

Remove Password Protection from a PowerPoint Document in Java
You can remove password protection from a PowerPoint document by loading the document with the correct password, then removing the password protection from it.
The following steps demonstrate how to remove password protection from a PowerPoint document:
- Initialize an instance of Presentation class.
- Load a PowerPoint document using Presentation.loadFromFile() method.
- Mark the document as final through Presentation.removeEncryption() method.
- Save the result document using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class RemovePasswordProtectionFromPPT {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a password-protected PowerPoint document with the right password
presentation.loadFromFile("Encrypted.pptx", "your password");
//Remove password protection from the document
presentation.removeEncryption();
//Save the result document
presentation.saveToFile("RemoveProtection.pptx", FileFormat.PPTX_2013);
}
}

Remove Mark as Final Option from a PowerPoint Document in Java
The mark as final feature makes a PowerPoint document read-only to prevent further changes, if you decide to make changes to the document later, you can remove the mark as final option from it.
The following steps demonstrate how to remove mark as final option from a PowerPoint document:
- Initialize an instance of Presentation class.
- Load a PowerPoint document using Presentation.loadFromFile() method.
- Remove the mark as final option from the document using Presentation.getDocumentProperty().set() method.
- Save the result document using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class RemoveMarkAsFinalFromPPT {
public static void main(String []args) throws Exception {
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load a PowerPoint document
ppt.loadFromFile( "MarkAsFinal.pptx");
//Remove mark as final option from the document
ppt.getDocumentProperty().set("_MarkAsFinal", false);
//Save the result document
ppt.saveToFile("RemoveMarkAsFinal.pptx", FileFormat.PPTX_2013);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Tables in PowerPoint are a valuable tool for organizing and presenting data in a clear and concise manner. When creating business presentations or financial reports, you can insert tables to present data effectively and make your presentations more impactful and attractive to your audience. This article will demonstrate how to programmatically add a table to a presentation slide using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml 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</artifactId>
<version>11.1.1</version>
</dependency>
</dependencies>
Insert a Table in PowerPoint in Java
To create a table in a specified PowerPoint slide, you can use the ISlide.getShapes().appendTable() method. With Spire.Presentation for Java, you are also allowed to format the style of the table. The following are the detailed steps.
- Create a Presentation instance.
- Get a specified slide using Presentation.getSlides().get() method.
- Define two double arrays to specify the number and size of rows and columns in the table.
- Add a table with the specified number and size of rows and columns to the slide using ISlide.getShapes().appendTable() method.
- Define some data and then then fill the table with the data using ITable.get().getTextFrame().setText() method.
- Set the text font and alignment of the table.
- Set a built-in table style using ITable.setStylePreset() method.
- Save the result document using Presentation.saveToFile() method.
- Java
import com.spire.presentation.*;
public class AddTable {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Get the first slide
ISlide slide = presentation.getSlides().get(0);
//Define two double arrays to specify the number and size of rows and columns in the table
Double[] widths = new Double[]{100d, 100d, 150d, 100d, 100d};
Double[] heights = new Double[]{15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d, 15d};
//Add a table with the specified number and size of rows and columns to the slide
ITable table = slide.getShapes().appendTable((float) presentation.getSlideSize().getSize().getWidth() / 2 - 275, 90, widths, heights);
//Specify table data
String[][] dataStr = new String[][]
{
{"Name", "Capital", "Continent", "Area", "Population"},
{"Venezuela", "Caracas", "South America", "912047", "19700000"},
{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
{"Canada", "Ottawa", "North America", "9976147", "26500000"},
{"Chile", "Santiago", "South America", "756943", "13200000"},
{"Colombia", "Bagota", "South America", "1138907", "33000000"},
{"Cuba", "Havana", "North America", "114524", "10600000"},
{"Ecuador", "Quito", "South America", "455502", "10600000"},
{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
{"Peru", "Lima", "South America", "1285215", "21600000"},
{"Jamaica", "Kingston", "North America", "11424", "2500000"},
{"Mexico", "Mexico City", "North America", "1967180", "88600000"}
};
//Add data to table
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 5; j++) {
//Fill the table with data
table.get(j, i).getTextFrame().setText(dataStr[i][j]);
//Set the text font
table.get(j, i).getTextFrame().getParagraphs().get(0).getTextRanges().get(0).setLatinFont(new TextFont("Calibri"));
//Set the text alignment of the table
table.get(j, i).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
}
}
//Set the table style
table.setStylePreset(TableStylePreset.LIGHT_STYLE_3_ACCENT_1);
//Save the result document
presentation.saveToFile("AddTable.pptx", FileFormat.PPTX_2013);
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.