Когда вы найдете документ PowerPoint с красивыми иллюстрациями или фоновыми изображениями, вы можете сохранить изображения на свой компьютер; или текстовое содержимое документа – это именно то, что вы хотите использовать для ссылок на другие документы, вы можете сначала сохранить текстовое содержимое в текстовый файл. В этой статье мы расскажем, как извлекать текст и изображения из документа PowerPoint с помощью FreeSpire. Презентация для Java .
Вот экран с образцом файла PowerPoint:
Извлечение текста из всего документа
import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.ParagraphEx;
import com.spire.presentation.Presentation;
import java.io.FileWriter;
public class ExtractText {
public static void main(String[] args) throws Exception{
//create a Presentation instance
Presentation ppt = new Presentation();
//load the PowerPoint document
ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\ Triceratops Herds.pptx");
//create a StringBuilder object
StringBuilder buffer = new StringBuilder();
//loop through the slides and extract text
for (Object slide : ppt.getSlides()) {
for (Object shape : ((ISlide) slide).getShapes()) {
if (shape instanceof IAutoShape) {
buffer.append("\r\n");
for (Object tp : ((IAutoShape) shape).getTextFrame().getParagraphs()) {
buffer.append(((ParagraphEx) tp).getText());
buffer.append("\r\n");
}
}
}
}
//write to text file
FileWriter writer = new FileWriter("ExtractedFiles/Text.txt");
writer.write(buffer.toString());
writer.flush();
writer.close();
}
}
Выход:
Извлечение изображений из всего документа
import com.spire.presentation.Presentation;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class ExtractImages {
public static void main(String[] args) throws Exception {
//create a Presentation instance
Presentation ppt = new Presentation();
//load a PowerPoint document
ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\ Triceratops Herds.pptx");
//loop through the images embedded in the document
for (int i = 0; i < ppt.getImages().getCount(); i++) {
//save each image to a local folder
BufferedImage image = ppt.getImages().get(i).getImage();
ImageIO.write(image, "PNG", new File(String.format("ExtractedFiles/" + "Image-%1$s.png", i)));
}
}
}
Выход:
Оригинал: “https://dev.to/eiceblue/extract-text-and-images-from-a-powerpoint-document-in-java-2cig”