在 Java 中將圖表轉換為影像

在某些情況下,處理包含大量數據(包括圖形數據如圖表)的 PowerPoint 文件可能會變得具有挑戰性。然而,圖形數據比文本數據更有用,因此從 PPT/PPTX 中提取圖表並將其保存為 影像 可以滿足您的需求。然後,您可以快速打開並瀏覽這些提取的圖表,以了解數據。因此,這篇博客文章將幫助您在 Java 中將圖表轉換為影像,使用 Aspose.Slides for Java 以程式方式實現。請按照本指南了解如何從 PPTX/PPT 文件中提取圖表。

這篇博客文章涵蓋以下要點:

  1. 數據提取 API 安裝
  2. 在 Java 中將圖表轉換為影像 - 代碼範例

數據提取 API 安裝

您可以訪問這個 安裝 指南。現在,您可以下載這個 JAR 文件,或者您可以使用以下 Maven 配置來安裝 Aspose.Slides for Java

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository> 
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>24.4</version>
    <classifier>jdk16</classifier>
</dependency>
數據提取 API

在 Java 中將圖表轉換為影像 - 代碼範例

您可以按照以下步驟開發一個圖表提取器

  • 定義源 PowerPoint 文件目錄的路徑。
  • 使用源 PPTX/PPT 文件初始化 Presentation 類的實例。
  • 調用 addChart 方法將圖表添加到集合的末尾。
  • getImage 方法返回形狀縮略圖。
  • 調用 save 方法將圖表保存為 PNG 影像。

以下代碼範例顯示如何在 Java 中將圖表轉換為影像以程式方式實現:

Convert Chart to Image in Javapackage com.example;
import com.aspose.slides.*;
// Convert Chart to Image in Java - Extract Charts From PPTX
public class GetChartImage
{
public static void main(String[] args)
{
// The path to the source PowerPoint file directory.
String dataDir = "/files/";
// Initialize an instance of the Presentation class with a source PPTX/PPT file.
Presentation pres = new Presentation(dataDir + "test.pptx");
try
{
// Invoke the addChart method to add a chart to the end of the collection.
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
// getImage method returns shape thumbnail.
IImage img = chart.getImage();
// Invoke the save method to save the chart as a PNG image.
img.save(dataDir + "image.png", ImageFormat.Png);
}
finally
{
if (pres != null) pres.dispose();
}
}
}

您可以在下面的圖像中查看輸出:

從 PPTX 中提取圖表

從 PPTX 中提取圖表 - 獲取免費許可

現在,您可以獲取一個免費的 臨時許可,以便在沒有評估限制的情況下嘗試這個數據提取 API。

總結

我們在這裡結束這個指南,希望您已經學會如何使用這個數據提取 API在 Java 中將圖表轉換為影像。此外,我們還開發了一個圖表提取服務,用於從 PPT/PPTX 文件中提取圖表作為影像。進一步了解,您可以訪問 文檔GitHub 倉庫和 API 參考,以了解更多關於這個 Java 庫的資訊。最後,aspose.com 持續更新,請保持關注以獲取最新消息。

提問

您可以在我們的 論壇 上告訴我們您的問題或查詢。

參見