Aspose.Slides for Java

Aspose.Slides for Java

Aspose.Slides for Java 是一個類別庫,可在 Java 應用程式中建立、讀取、編輯及轉換 PowerPoint 與 OpenDocument 簡報,無需 Microsoft PowerPoint。

它能載入與儲存 PPT、PPTX、PPS、POT 與 ODP,包含支援巨集的版本與範本變體,並可匯出為 PDF、XPS、HTML、SVG、TIFF、Markdown 與影像。



您的第一個簡報

Aspose.Slides for Java 發佈於 Aspose 自己的 Maven 儲存庫,而非 Maven Central。為 Maven 專案建立一個資料夾,並將此 pom.xml 存於其中。它會宣告儲存庫、加入函式庫,並指定要執行的類別:

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>hello-slides</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.release>11</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <exec.mainClass>HelloSlides</exec.mainClass>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-slides</artifactId>
            <version>26.9</version>
            <classifier>jdk16</classifier>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.15.0</version>
            </plugin>
        </plugins>
    </build>
</project>

將此程式碼儲存為 src/main/java/HelloSlides.java:

import com.aspose.slides.*;

public class HelloSlides {
    public static void main(String[] args) {
        // 建立簡報。它已包含一張空白投影片。
        Presentation presentation = new Presentation();
        try {
            // 取得第一張投影片。
            ISlide slide = presentation.getSlides().get_Item(0);

            // 新增雲狀圖形並在其中放入文字。
            IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Cloud, 20, 20, 200, 80);
            autoShape.getTextFrame().setText("Hello, Aspose!");

            // 將簡報儲存為 PPTX 檔案。
            presentation.save("new_presentation.pptx", SaveFormat.Pptx);
        } finally {
            presentation.dispose();
        }
    }
}

接著,在已安裝 JDK 11 或更新版本以及 Apache Maven 的情況下,於專案資料夾執行以下指令:

mvn compile exec:java

此程式會在專案資料夾中產生 new_presentation.pptx,內容為一張包含雲狀圖形與文字的投影片。於 Linux 環境下必須安裝 fontconfig 與至少一種字型;詳見Installation。未取得授權時,儲存的檔案會帶有評估水印——請參閱Licensing。欲了解更多建立與填充簡報的方式,請參考Create Presentations。