groupdocs word to pdf:在 Java 中將受保護的 Word 轉換為 PDF

在本指南中,您將學習如何在 Java 中執行 groupdocs word to pdf 轉換,輕鬆將受密碼保護的 Word 文件轉換為高品質的 PDF。我們將逐步說明設定頁面範圍、調整 DPI、旋轉頁面以及微調尺寸,讓您能依需求精確調整輸出。

快速解答

  • 什麼函式庫負責轉換? GroupDocs.Conversion for Java.
  • 我可以轉換受密碼保護的 Word 檔案嗎? Yes – just supply the password via WordProcessingLoadOptions.
  • 如何將轉換限制在特定頁面? Use setPageNumber() and setPagesCount() on PdfConvertOptions.
  • DPI 可否設定? Absolutely; call options.setDpi(yourValue).
  • 我需要 Maven 來加入 GroupDocs 嗎? Yes – include the Maven repository and dependency (see the Maven groupdocs dependency section).

什麼是 groupdocs word to pdf 轉換?

GroupDocs.Conversion 是一個 Java 函式庫,可將 Word 文件(包括受保護的文件)轉換為 PDF 檔案。它抽象化了低層的解析與渲染工作,讓您專注於商業邏輯,如安全處理、頁面選取與輸出品質。

為什麼在 Java 中使用 GroupDocs 進行 Word 轉 PDF 任務?

  • Zero‑install – 純 Java,無需本機二進位檔。
  • Password support – 安全開啟加密文件。
  • Fine‑grained control – 頁面範圍、DPI、旋轉與自訂尺寸。
  • Scalable performance – 為大型檔案與伺服器端工作負載優化。

前置條件

  • JDK 8 或更新版本已安裝並設定。
  • 基本的 Java 開發經驗。
  • 取得 GroupDocs.Conversion 授權(提供免費試用)。

必要的函式庫與相依性

要使用 GroupDocs.Conversion,請在 pom.xml 中加入 Maven 儲存庫與相依性:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/conversion/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-conversion</artifactId>
      <version>25.2</version>
   </dependency>
</dependencies>

取得授權

GroupDocs.Conversion 提供免費試用版以測試功能。若需長期使用,請從 GroupDocs Purchase 取得臨時或正式授權。

在 Java 中設定 GroupDocs.Conversion

Maven 設定

上述 Maven 片段可確保自動下載所有必要的 JAR。

基本初始化

建立 Converter 實例並載入受保護的文件:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.load.WordProcessingLoadOptions;

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
// Set password for protected documents if necessary:
loadOptions.setPassword("your_password_here");

Converter converter = new Converter("path_to_your_document.docx", () -> loadOptions);

loadOptions 物件即是處理 convert password protected word 情境的地方。

實作指南

以下我們將深入探討在穩健的 java convert word pdf 工作流程中可能需要的各項功能。

轉換受密碼保護的文件為 PDF

概述: 只需一次呼叫,即可將受保護的 Word 檔案轉換為 PDF。

步驟實作

  1. Initialize Load Options with Password – 提供正確的密碼。
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
loadOptions.setPassword("12345"); // Replace with your actual password.
  1. Set Up Converter and Convert – 定義 PDF 選項並執行。
import com.groupdocs.conversion.options.convert.PdfConvertOptions;

String convertedFile = "YOUR_OUTPUT_DIRECTORY/ConvertedDocument.pdf";
PdfConvertOptions options = new PdfConvertOptions();

Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleProtectedDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);

說明: loadOptions 物件負責解鎖文件,而 PdfConvertOptions 讓您之後可調整輸出設定(如有需要)。

疑難排解技巧

  • 驗證密碼是否正確;打錯字會拋出 IncorrectPasswordException
  • 使用絕對路徑或確保工作目錄與相對路徑相符,以避免 FileNotFoundException

指定要在 PDF 中轉換的頁面

概述: 僅轉換所需頁面,可節省時間與儲存空間。

步驟實作

  1. Set Page Range – 告訴轉換器要渲染哪些頁面。
PdfConvertOptions options = new PdfConvertOptions();
options.setPageNumber(2); // Start from page 2.
options.setPagesCount(1); // Convert only one page.
  1. Conversion Process – 重複使用相同的 Converter 實例。
String convertedFile = "YOUR_OUTPUT_DIRECTORY/SelectedPagesPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);

說明: setPageNumber() 定義起始頁,setPagesCount() 限制處理的頁數。

在 PDF 轉換中旋轉頁面

概述: 在轉換過程中直接調整頁面方向。

步驟實作

  1. Set Rotation Options – 選擇旋轉列舉值。
import com.groupdocs.conversion.options.convert.Rotation;

PdfConvertOptions options = new PdfConvertOptions();
options.setRotate(Rotation.On180); // Rotate pages 180 degrees.
  1. Execute Conversion – 與前述相同的流程。
String convertedFile = "YOUR_OUTPUT_DIRECTORY/RotatedPagesPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);

說明: 旋轉可修正橫向掃描或符合特定版面需求。

設定 PDF 轉換的 DPI

概述: 控制 PDF 內圖像與向量圖形的解析度。

步驟實作

  1. Configure DPI Settings
PdfConvertOptions options = new PdfConvertOptions();
options.setDpi(300); // Set DPI to 300 for high resolution.
  1. Perform Conversion with Custom DPI
String convertedFile = "YOUR_OUTPUT_DIRECTORY/HighResolutionPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);

說明: 較高的 DPI 可提升視覺真實度,但會增大檔案大小——請依目標媒介選擇適當值。

設定 PDF 轉換的寬度與高度

概述: 為輸出 PDF 定義明確的像素尺寸。

步驟實作

  1. Define Dimensions
PdfConvertOptions options = new PdfConvertOptions();
options.setWidth(1024); // Set width to 1024 pixels.
options.setHeight(768); // Set height to 768 pixels.
  1. Convert with Custom Sizes
String convertedFile = "YOUR_OUTPUT_DIRECTORY/SizedPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);

說明: 自訂尺寸對於產生符合特定螢幕大小或列印格式的 PDF 十分便利。

常見問題與解決方案

IssueLikely CauseFix
IncorrectPasswordException提供的密碼錯誤再次確認密碼字串,並去除前後空白。
FileNotFoundException檔案路徑無效使用絕對路徑或確認工作目錄是否正確。
Output PDF is blurryDPI 設定過低透過 options.setDpi() 提高 DPI。
Pages appear upside‑down旋轉未設定或設定錯誤使用 options.setRotate(Rotation.On180)(或其他列舉值)。
Converted file is larger than expectedDPI 較高且尺寸過大降低 DPI 或調整寬度/高度,以在檔案大小與品質之間取得平衡。

常見問答

Q: 我可以轉換同時具備密碼與唯讀保護的 Word 文件嗎?
A: 可以。透過 WordProcessingLoadOptions.setPassword() 提供開啟密碼。轉換過程會忽略唯讀旗標。

Q: GroupDocs.Conversion 是否同時支援 .doc(舊版)檔案與 .docx?
A: 當然支援。函式庫會透明地處理兩種格式。

Q: java convert word pdf 的效能在大型檔案上如何擴展?
A: GroupDocs 會以串流方式處理資料,並在每次轉換後釋放資源。對於極大檔案,建議增加 JVM 堆積大小,並在完成後使用 Converter.dispose() 方法。

Q: 是否可以批次轉換多個文件?
A: 可以。遍歷檔案路徑,為每個檔案建立新的 Converter,並在適當情況下重複使用相同的 PdfConvertOptions

Q: 開發版是否需要商業授權?
A: 免費試用版可用於評估,但正式上線的部署需要有效的 GroupDocs.Conversion 授權。

結論

您現在已擁有完整且可投入生產的 groupdocs word to pdf Java 轉換藍圖,涵蓋密碼保護、頁面選取、旋轉、DPI 以及自訂尺寸等功能。將這些程式碼片段結合至您的工作流程,即可產出符合精確業務需求的 PDF。


最後更新: 2026-03-06
測試環境: GroupDocs.Conversion 25.2 for Java
作者: GroupDocs