
文件解析是在許多行業中一項關鍵任務,該行業需要從各種文件格式中提取數據。在處理 Word 文件時,提取圖像在內容分析、圖像識別或數據可視化等情況下特別有用。從大型 Word 文檔中手動提取圖像可能非常耗時。因此,自動化圖像提取過程可以為您節省時間和精力。在本文中,我們將演示如何在 Java 中以編程方式從 Word 文檔中提取圖像。
本篇文章將涵蓋以下主題:
- Java REST API to Extract Images from Word Documents and SDK Installation
- 如何使用 REST API 從 Word 文檔中提取所有圖片
- 從 Word 文件中根據頁碼提取特定圖像
Java REST API to Extract Images from Word Documents and SDK Installation
GroupDocs.Parser Cloud SDK for Java 是一個強大的 Java 庫,提供了一種簡單而高效的方式來解析和提取各種 document formats 的數據,包括 Word 文檔。它提供了廣泛的文檔解析功能,允許開發人員提取圖像、文本、元數據等。GroupDocs.Parser 還為 Cloud APIs 提供了 C#.NET、Java、PHP、Ruby 和 Python SDK 作為其 document parser family members。
要開始使用,您需要在您的 Java 項目中包含 GroupDocs.Parser Cloud SDK。您可以選擇 下載 API 的 JAR 文件或通過將以下儲存庫和依賴項添加到您項目的 pom.xml 文件中來使用 Maven 進行安裝:
Maven Repository:
<repository>
<id>groupdocs-artifact-repository</id>
<name>GroupDocs Artifact Repository</name>
<url>https://repository.groupdocs.cloud/repo</url>
</repository>
Maven 依賴:
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-parser-cloud</artifactId>
<version>23.3</version>
<scope>compile</scope>
</dependency>
接下來,您需要 sign up 一個免費試用帳戶或 purchase a subscription plan 在 GroupDocs 網站上,並 get your API key。一旦您擁有了 Client Id 和 Client Secret,請將以下代碼片段添加到基於 Java 的應用程序中:
# 從 https://dashboard.groupdocs.cloud 註冊後獲取您的 clientid 和 clientsecret。
String ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
String ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String MyStorage = "test-internal-storage";
Configuration configuration = new Configuration(ClientId, ClientSecret);
如何在 Java 中使用 REST API 提取 Word 文檔中的所有圖像
要使用 GroupDocs.Parser Cloud SDK 從 Word 文件中提取圖像,請遵循以下步驟和示例代碼片段:
- 首先,將所需的類導入到您的 Java 文件中。
- 其次,創建一個 ParseApi 類的實例。
- 第三,創建一個 FileInfo 類的實例。
- 接下來,設置輸入 Word 文檔的路徑。
- 然後,創建 ImagesOptions() 類的實例。
- 接下來,將 fileInfo 指派給 setFileInfo 圖像選項。
- 現在,創建 ImagesRequest() 類的實例並傳遞 ImagesOptions 參數。
- 最後,通過調用 ParseApi.images() 方法並傳遞 ImagesRequest 參數來獲取結果。
以下代碼範例顯示如何使用 REST API 在 Java 中從 Word 文件中提取所有圖像:
package com.groupdocsdev.classes;
import com.groupdocs.cloud.parser.api.ParseApi;
import com.groupdocs.cloud.parser.client.ApiException;
import com.groupdocs.cloud.parser.client.Configuration;
import com.groupdocs.cloud.parser.model.FileInfo;
import com.groupdocs.cloud.parser.model.ImagesOptions;
import com.groupdocs.cloud.parser.model.ImagesResult;
import com.groupdocs.cloud.parser.model.requests.ImagesRequest;
// 如何在 Java 中從 Word 文件中提取圖像。
public class App {
public static void main(String[] args) {
// 建立一個 Parse API 的實例
ParseApi apiInstance = new ParseApi(configuration);
try {
// 準備設置
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("java-testing/input-sample-file.docx");
ImagesOptions options = new ImagesOptions();
options.setFileInfo(fileInfo);
ImagesRequest request = new ImagesRequest(options);
ImagesResult response = apiInstance.images(request);
// Get output file path
System.out.println("Output file path: " + response.getPath());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}
提取特定圖像從 Word 文件中在 Java 中使用頁碼
在本節中,我們將編寫步驟和示例代碼片段,以在 Java 中以編程方式從 Word 文檔中提取特定圖像:
- 首先,將所需的類導入到您的 Java 文件中。
- 其次,創建一個 ParseApi 類的實例。
- 第三,創建一個 FileInfo 類的實例。
- 接下來,將Word文件的路徑設置為輸入。
- 然後,創建 ImagesOptions() 類的實例。
- 接下來,將 fileInfo 指派給 setFileInfo 圖像選項。
- 然後,提供 setStartPageNumber 和 setCountPagesToExtract 的值。
- 現在,創建 ImagesRequest() 類的實例並傳遞 ImagesOptions 參數。
- 最後,通過調用 ParseApi.images() 方法並傳遞 ImagesRequest 參數來獲取結果。
以下代碼範例顯示如何透過 REST API 在 Java 中按頁面範圍從 Word 檔案中提取特定圖像:
package com.groupdocsdev.classes;
import com.groupdocs.cloud.parser.api.ParseApi;
import com.groupdocs.cloud.parser.client.ApiException;
import com.groupdocs.cloud.parser.client.Configuration;
import com.groupdocs.cloud.parser.model.FileInfo;
import com.groupdocs.cloud.parser.model.ImagesOptions;
import com.groupdocs.cloud.parser.model.ImagesResult;
import com.groupdocs.cloud.parser.model.requests.ImagesRequest;
// 提取 Word 文件中指定頁碼範圍的圖像,使用 Java 在線。
public class App {
public static void main(String[] args) {
// 建立 Parse API 的一個實例
ParseApi apiInstance = new ParseApi(configuration);
try {
// 準備設置
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("java-testing/input-sample-file.docx");
ImagesOptions options = new ImagesOptions();
options.setFileInfo(fileInfo);
options.setStartPageNumber(1);
options.setCountPagesToExtract(2);
ImagesRequest request = new ImagesRequest(options);
ImagesResult response = apiInstance.images(request);
// 獲取輸出文件路徑
System.out.println("Output file path: " + response.getPath());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}
免費在線圖像提取器
什麼是從 Word 在線免費提取圖像的最佳方法?請嘗試使用 online Word document parser 提取 Word 中的圖像。這個 Word Parser 工具是使用上述提到的 Java 解析庫開發的。
結論
總結來說,GroupDocs.Parser Cloud SDK 是一個優秀的解決方案,用於從 Word 文件中提取圖像,節省時間和精力,同時確保準確的結果。以下是您從本文中學到的內容:
- how to extract all images from Word DOCX programmatically in Java using REST API;
- how to extract specific images from Word documents in Java using REST API;
- 和在線圖像提取工具從 Word 文件中提取圖片。
此外,您可以通過 documentation 瞭解更多關於 GroupDocs.Parser Cloud API 的資訊。我們還提供了一個 API Reference 區域,可讓您直接通過瀏覽器視覺化並與我們的 API 互動。Java SDK 的完整源代碼可以在 Github 上免費獲得。
Finally, we keep writing new blog articles on different file formats and parsing them using REST API. So, please get in touch for the latest updates.
Ask a question
如果您對如何從文件中提取圖像有任何疑問,請隨時通過我們的 forum 與我們聯繫。
常見問題解答
如何在 Java 中解析 Word 文件?
要提取圖像或文本,您首先需要使用 GroupDocs.Parser Cloud SDK 載入並解析 Word 文件。此過程涉及指定文件路徑並調用 Parse 方法來解析文檔。
GroupDocs.Parser Cloud SDK 是否能從其他文件格式中提取圖像?
是的, GroupDocs.Parser Cloud SDK for Java 支援從各種文件格式中提取圖像,包括 Word、PDF、Excel、PowerPoint 等等。
Can the GroupDocs.Parser Cloud SDK extract multiple images from a single Word document?
是的, SDK 可以從單一 Word 文件中提取多個圖像,為您提供文件中包含的所有圖像。
Does the GroupDocs.Parser Cloud SDK preserve the original image quality during the extraction process?
是的,GroupDocs.Parser Cloud SDK for Java 在從 Word 文件中提取圖像時保留了原始圖像質量。
See Also
這裡有一些相關的文章,您可能會覺得有幫助: