# 通過Java從PPT文檔中提取文本和圖像

> Java 示例代碼，用於從 JSP/JSF 應用程序和桌面應用程序的 Java 運行時環境中的 PPT 文件中提取文本和圖像。

Source: https://products.aspose.com/slides/zh-hant/java/parser/ppt/
Updated: 2026-08-11



## 如何使用 Java 解析 PPT 文件

 為了解析 PPT 文件，我們將使用
 [Aspose.Slides for Java](https://products.aspose.com/slides/zh-hant/java/)
 API 是一個功能豐富、功能強大且易於使用的 Java 平台解析 API。您可以直接從
 [Maven](https://repository.aspose.com/webapp/#/artifacts/browse/tree/General/repo/com/aspose/aspose-slides)
 並通過將以下配置添加到 pom.xml 將其安裝在基於 Maven 的項目中。

### 存儲庫

```xml

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

```

### 依賴

```xml

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>version of aspose-slides API</version>
    <classifier>jdk17</classifier>
</dependency>
```

## 在 Java 中解析 PPT 文件的步驟

使用 [Aspose.Slides for Java](https://products.aspose.com/slides/zh-hant/java/) API 解析基本文檔只需幾行代碼即可完成。

通過實例​​化 Presentation 類加載 PPT 文件。

獲取第一張幻燈片文本框架。

循環遍歷每個段落部分。

獲取所需的輸出，如文本、字體等。

## 系統要求

 Aspose.Slides for Java 支持所有主要平台和操作系統。請確保您具有以下先決條件。

- Microsoft Windows 或與 JSP/JSF 應用程序和桌面應用程序的 Java 運行時環境兼容的操作系統。
- 直接從 Java 獲取最新版本的 Aspose.Slides
 [Maven](https://repository.aspose.com/webapp/#/artifacts/browse/tree/General/repo/com/aspose/aspose-slides)。

### 解析 PPT 文件 - Java

```cs
//Load PPT file
Presentation pptPresentation = new Presentation("demo.ppt");
try{
    //Get an Array of TextFrameEx objects from the first slide
    ITextFrame[] textFramesSlideOne = SlideUtil.getAllTextBoxes(pptPresentation.getSlides().get_Item(0));

    //Loop through the Array of TextFrames
    for (int i = 0; i < textFramesSlideOne.length; i++){
        //Loop through paragraphs in current TextFrame
        for (IParagraph para : textFramesSlideOne[0].getParagraphs()){
            //Loop through portions in the current Paragraph
            for (IPortion port : para.getPortions()){
                //Display text in the current portion
                System.out.print(port.getText());

                //Display font height of the text
                System.out.print(port.getPortionFormat().getFontHeight());

                //Display font name of the text
                System.out.print(port.getPortionFormat().getLatinFont().getFontName());
            }
        }
    }
} finally {
    if (pptPresentation != null) pptPresentation.dispose();
}
//Similarly extarcting text from the Whole Presentation
//Use getAllTextFrames(pptPresentation, true) method and Iterate through Array

```

## 關於 Aspose.Slides for Java API

 Aspose.Slides API 可用於讀取、寫入、操作 Microsoft PowerPoint 文檔並將其轉換為 PDF、XPS、HTML、TIFF、ODP 和各種其他格式。可以從頭開始創建新文件並將其保存為相關支持的格式。 Aspose.Slides 是一個獨立的 API，用於創建、解析或操作演示文稿、幻燈片和元素，它不依賴於 Microsoft 或 OpenOffice 等任何軟件。

A file with PPT extension represents PowerPoint file that consists of a collection of slides for displaying as SlideShow. It specifies the Binary File Format used by Microsoft PowerPoint 97-2003. A PPT file can contain several different types of information such as text, bulleted points, images, multimedia and other embedded OLE objects. Microsoft came up with newer file format for PowerPoint, known as PPTX, from 2007 onwards that is based on Office OpenXML and is different from this binary file format. Several other application programs such as OpenOffice Impress and Apple Keynote can also create PPT files.

## 其他支持的解析文檔

- [ODP](https://products.aspose.com/slides/zh-hant/java/parser/odp/) — OpenDocument 演示格式
- [PPTX](https://products.aspose.com/slides/zh-hant/java/parser/pptx/) — 打開 XML 表示格式
