Java:使用 GroupDocs.Editor 將網頁轉換為 DOCX
Converting a webpage to DOCX lets you turn any online HTML page into a fully editable Word document that can be shared, printed, or further customized. Whether you need to archive a marketing article, generate a report from a dashboard, or provide a printable version of a legal notice, the conversion preserves layout, styling, and embedded images. In this guide we’ll walk through using GroupDocs.Editor for Java to read an HTML file, bundle its resources, and save the result as both HTML and DOCX/DOCM files.
快速解答
- 什麼是「將網頁轉換為 docx」的意思? 它會將 HTML 標記及其資源轉換為可編輯的 Word(DOCX/DOCM)檔案。
- 哪個函式庫負責轉換? GroupDocs.Editor for Java。
- 我需要授權嗎? 免費試用可用於測試;正式環境需購買授權。
- 需要哪個 Java 版本? Java 8 或更高版本。
- 可以保留 CSS 和圖片嗎? 可以——編輯器在轉換過程中會保留連結的樣式表和圖片。
什麼是「將網頁轉換為 docx」?
Load the HTML source, bundle any referenced CSS or images, and generate a Word processing document that mirrors the original layout. The conversion preserves headings, tables, lists, and styling, producing a file that can be opened and edited in Microsoft Word or any compatible editor without the need for manual re‑formatting or reconstruction.
為什麼使用 GroupDocs.Editor for Java?
GroupDocs.Editor provides a high‑level API that converts HTML to DOCX in under 2 seconds for files up to 150 MB, supports 30+ HTML elements, and automatically embeds CSS and images. It runs cross‑platform, requires no native dependencies, and guarantees layout fidelity across Word, LibreOffice, and Google Docs.
前置條件
必要的函式庫、版本與相依性
- Apache Commons IO – 簡化檔案 I/O。
- GroupDocs.Editor – 版本 25.3(或最新穩定版)。
環境設定需求
- 安裝 JDK 8 或更新版本。
- 使用 IntelliJ IDEA 或 Eclipse 等 IDE。
知識前提
- 基本的 Java 與 Maven 專案結構。
- 熟悉 HTML 檔案及其資料夾結構。
設定 GroupDocs.Editor for Java
Maven 設定
Add the GroupDocs repository and dependency to your pom.xml:
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/editor/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-editor</artifactId>
<version>25.3</version>
</dependency>
</dependencies>
直接下載
Alternatively, you can download the latest version from GroupDocs.Editor for Java releases.
取得授權步驟
- 免費試用: 先使用試用版探索 API。
- 臨時授權: 使用限時金鑰以延長評估。
- 購買: 取得商業授權以供正式部署。
實作指南
Below is a step‑by‑step walk‑through. Each code block is unchanged from the original tutorial; the surrounding explanations have been expanded for clarity.
功能 1 – 從檔案讀取 HTML 內容
Why this matters: To convert a webpage you first need the raw HTML as a String. Using Apache Commons IO makes this a one‑liner.
1.1 匯入必要的函式庫
import java.io.File;
import org.apache.commons.io.FileUtils;
1.2 指定檔案路徑
Replace YOUR_DOCUMENT_DIRECTORY with the folder that holds your source HTML.
String htmlFilePath = "YOUR_DOCUMENT_DIRECTORY/sample_html_body.html";
1.3 讀取內容至字串
The FileUtils.readFileToString method reads the file using UTF‑8 encoding, preserving all characters.
String content = FileUtils.readFileToString(new File(htmlFilePath), "utf-8");
// Note: This method reads the HTML content as a UTF-8 encoded string, ensuring accurate representation of characters.
功能 2 – 從 HTML 內容初始化 EditableDocument
Why this matters: EditableDocument is the core object that groups the markup with its resources (CSS, images) so the editor can work with a complete document.
The EditableDocument class represents a single HTML document together with its linked resources, enabling seamless conversion to other formats.
2.1 匯入 GroupDocs 函式庫
import com.groupdocs.editor.EditableDocument;
2.2 指定資源資料夾路徑
The folder should contain any CSS files, images, or other assets referenced by the HTML.
String resourceFolderPath = "YOUR_DOCUMENT_DIRECTORY/sample_html_body_resources";
2.3 初始化 EditableDocument
This call merges the HTML markup with the resource folder, creating an in‑memory editable document.
EditableDocument inputDoc = EditableDocument.fromMarkupAndResourceFolder(content, resourceFolderPath);
// This method combines the HTML markup with its linked resources to form a complete editable document.
功能 3 – 檢查文件資源
Why this matters: Knowing how many stylesheets or images are present helps you decide whether extra processing (e.g., image optimization) is needed.
3.1 計算樣式表與圖片數量
int stylesheetCount = inputDoc.getCss().size();
int imageCount = inputDoc.getImages().size();
// These methods provide insights into how many stylesheets or images are linked within your HTML content.
功能 4 – 將 EditableDocument 儲存為 HTML
Why this matters: Sometimes you want to keep an HTML version after making edits, or you need to verify that resources are correctly bundled.
4.1 匯入儲存選項函式庫
import com.groupdocs.editor.Editor;
4.2 指定 HTML 輸出路徑
String outputHtmlFilePath = "YOUR_OUTPUT_DIRECTORY/_output.html";
4.3 儲存文件為 HTML
The save method writes the edited document back to disk, preserving its structure.
inputDoc.save(outputHtmlFilePath);
// This saves all changes made in memory back into a new HTML document, maintaining its editable format and resources.
功能 5 – 將 EditableDocument 儲存為文字處理文件(DOCX/DOCM)
Why this matters: Converting to DOCX/DOCM gives you a fully editable Word file that can be opened in Microsoft Word, LibreOffice, or any compatible editor.
The WordProcessingFormats enum defines the exact Word format (DOCX or DOCM) you want to generate.
5.1 匯入儲存選項函式庫
import com.groupdocs.editor.options.WordProcessingSaveOptions;
import com.groupdocs.editor.formats.WordProcessingFormats;
5.2 指定 DOCX/DOCM 輸出路徑
String outputDocmFilePath = "YOUR_OUTPUT_DIRECTORY/_output.docm";
5.3 設定儲存選項與格式
Here we explicitly request the DOCM format (macro‑enabled Word document). You could switch to "docx" for a standard document.
WordProcessingFormats saveFormat = WordProcessingFormats.fromExtension("docm");
WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(saveFormat);
// Here, we define the desired output format (DOCM) along with any specific saving options needed for conversion.
Editor is the core class that takes an EditableDocument and writes it to the chosen Word format.
5.4 儲存文件為 DOCM
We use the Editor class to perform the final conversion.
Editor editor = new Editor(htmlFilePath);
editor.save(inputDoc, outputDocmFilePath, saveOptions);
// This final step converts and saves your HTML content into a fully functional Word document (DOCM).
實務應用
- 動態報告產生: 從即時儀表板抓取表格,轉換為 Word,並以自動化郵件發送報告。
- 內容管理系統(CMS): 為文章提供「匯出為 Word」按鈕,保留樣式與圖片。
- 法律文件製作: 將網路發布的法規轉為可編輯的合約或政策文件。
- 教育教材彙編: 將 HTML 頁面的講義彙集成一本學習指南。
- 商業提案製作: 將行銷網頁轉為精緻的 DOCM 提案給客戶。
效能考量
- 最佳化記憶體使用: 對於大型 HTML 檔案,可增加 JVM 堆積 (
-Xmx2g) 或分塊處理文件。 - 非同步載入資源: 在基於 Web 的工具中,於背景執行緒載入 CSS 與圖片,以保持 UI 響應。
常見問題與解決方案
| 問題 | 原因 | 解決方法 |
|---|---|---|
| DOCM 中缺少圖片 | 資源資料夾路徑不正確 | 確認 resourceFolderPath 指向包含所有圖片檔案的資料夾。 |
| 轉換後樣式不同 | CSS 未載入 | 確保 inputDoc.getCss() 回傳預期的數量;將缺少的樣式表加入資源資料夾。 |
| 大型頁面發生 OutOfMemoryError | HTML 大且資源眾多 | 增加 JVM 堆積或在轉換前將 HTML 拆分為較小的區段。 |
常見問答
Q: 可以直接將即時 URL 轉換,而不先儲存 HTML 嗎?
A: 可以。使用 Jsoup 或 HttpClient 下載頁面內容,然後將字串傳入 EditableDocument.fromMarkupAndResourceFolder。
Q: GroupDocs.Editor 是否同時支援轉換為 DOCX 與 DOCM?
A: 當然支援。只要在 WordProcessingFormats.fromExtension("docx") 中更改副檔名,並調整輸出檔名即可。
Q: 若我的 HTML 參考了 CDN 上的外部 CSS,該怎麼辦?
A: 在初始化 EditableDocument 前,先將那些 CSS 檔案下載至資源資料夾,或在啟用網路存取時讓編輯器自行抓取。
Q: 免費試用需要授權嗎?
A: 試用版可在未輸入授權金鑰的情況下使用,但受限於 30 天及文件大小上限。正式環境請購買授權。
Q: 可以在 Word 輸出中保留 JavaScript 功能嗎?
A: 無法保留。Word 文字處理格式不支援客戶端 JavaScript,只會保留靜態內容與樣式。
Last Updated: 2026-07-02
Tested With: GroupDocs.Editor 25.3
Author: GroupDocs