groupdocs word to pdf: แปลง Word ที่ป้องกันเป็น PDF ใน Java

ในคู่มือนี้คุณจะได้เรียนรู้วิธีทำการแปลง groupdocs word to pdf ใน Java โดยเปลี่ยนเอกสาร Word ที่มีการป้องกันด้วยรหัสผ่านให้เป็น PDF คุณภาพสูงอย่างง่ายดาย เราจะอธิบายการตั้งค่าช่วงหน้า, การปรับ DPI, การหมุนหน้า, และการปรับขนาดอย่างละเอียด เพื่อให้คุณสามารถปรับผลลัพธ์ให้ตรงกับความต้องการของคุณได้

Quick Answers

  • ไลบรารีที่จัดการการแปลงคืออะไร? GroupDocs.Conversion for Java.
  • ฉันสามารถแปลงไฟล์ Word ที่ป้องกันด้วยรหัสผ่านได้หรือไม่? ใช่ – เพียงแค่ส่งรหัสผ่านผ่าน WordProcessingLoadOptions.
  • ฉันจะจำกัดการแปลงให้เฉพาะหน้าที่ต้องการได้อย่างไร? ใช้ setPageNumber() และ setPagesCount() บน PdfConvertOptions.
  • DPI สามารถกำหนดค่าได้หรือไม่? แน่นอน; เรียก options.setDpi(yourValue).
  • ฉันต้องใช้ Maven เพื่อเพิ่ม GroupDocs หรือไม่? ใช่ – รวมรีโพซิทอรี Maven และ dependency (ดูส่วน Maven groupdocs dependency).

What is groupdocs word to pdf conversion?

GroupDocs.Conversion เป็นไลบรารี Java ที่แปลงเอกสาร Word (รวมถึงเอกสารที่ป้องกัน) เป็นไฟล์ PDF มันทำหน้าที่ซ่อนขั้นตอนการแยกวิเคราะห์และการเรนเดอร์ระดับต่ำ ให้คุณมุ่งเน้นที่ตรรกะธุรกิจ เช่น การจัดการความปลอดภัย การเลือกหน้า และคุณภาพของผลลัพธ์

Why use GroupDocs for Java convert word pdf tasks?

  • Zero‑install – Java แท้, ไม่มีไบนารีเนทีฟ.
  • Password support – เปิดเอกสารที่เข้ารหัสอย่างปลอดภัย.
  • Fine‑grained control – ช่วงหน้า, DPI, การหมุน, และขนาดที่กำหนดเอง.
  • Scalable performance – ปรับให้เหมาะกับไฟล์ขนาดใหญ่และงานฝั่งเซิร์ฟเวอร์

Prerequisites

  • JDK 8 หรือใหม่กว่า ติดตั้งและกำหนดค่าแล้ว.
  • มีประสบการณ์พื้นฐานในการพัฒนา Java.
  • มีสิทธิ์ใช้งานไลเซนส์ GroupDocs.Conversion (มีรุ่นทดลองฟรี).

Required Libraries and Dependencies

To use GroupDocs.Conversion, include the Maven repository and dependency in your pom.xml:

<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>

License Acquisition

GroupDocs.Conversion มีรุ่นทดลองฟรีสำหรับทดสอบฟีเจอร์ หากต้องการใช้งานต่อเนื่อง ควรพิจารณาได้รับไลเซนส์ชั่วคราวหรือเต็มจาก GroupDocs Purchase.

Setting Up GroupDocs.Conversion for Java

Maven Setup

The Maven snippet above ensures all required JARs are downloaded automatically.

Basic Initialization

Create a Converter instance and load a protected document:

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);

The loadOptions object is where you handle the convert password protected word scenario.

Implementation Guide

Below we dive into each feature you might need for a robust java convert word pdf workflow.

Convert Password‑Protected Document to PDF

Overview: Turn a secured Word file into a PDF with a single call.

Step‑by‑Step Implementation

  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);

Explanation: The loadOptions object unlocks the document, while PdfConvertOptions lets you tweak the output later if needed.

Troubleshooting Tips

  • ตรวจสอบรหัสผ่าน; การพิมพ์ผิดจะทำให้เกิด IncorrectPasswordException.
  • ใช้เส้นทางแบบ absolute หรือให้แน่ใจว่าไดเรกทอรีทำงานตรงกับเส้นทาง relative เพื่อหลีกเลี่ยง FileNotFoundException.

Specify Pages to Convert in PDF

Overview: Convert only the pages you need, saving time and storage.

Step‑by‑Step Implementation

  1. Set Page Range – tell the converter which pages to render.
PdfConvertOptions options = new PdfConvertOptions();
options.setPageNumber(2); // Start from page 2.
options.setPagesCount(1); // Convert only one page.
  1. Conversion Process – reuse the same Converter instance.
String convertedFile = "YOUR_OUTPUT_DIRECTORY/SelectedPagesPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);

Explanation: setPageNumber() defines the first page, while setPagesCount() limits how many pages are processed.

Rotate Pages in PDF Conversion

Overview: Adjust page orientation directly during conversion.

Step‑by‑Step Implementation

  1. Set Rotation Options – choose a rotation enum.
import com.groupdocs.conversion.options.convert.Rotation;

PdfConvertOptions options = new PdfConvertOptions();
options.setRotate(Rotation.On180); // Rotate pages 180 degrees.
  1. Execute Conversion – same pattern as before.
String convertedFile = "YOUR_OUTPUT_DIRECTORY/RotatedPagesPdf.pdf";
Converter converter = new Converter("YOUR_DOCUMENT_DIRECTORY/SampleDocx.docx", () -> loadOptions);
converter.convert(convertedFile, options);

Explanation: Rotating can fix landscape scans or meet specific layout requirements.

Set DPI for PDF Conversion

Overview: Control the resolution of images and vector graphics inside the PDF.

Step‑by‑Step Implementation

  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);

Explanation: Higher DPI improves visual fidelity but increases file size—choose based on your target medium.

Set Width and Height for PDF Conversion

Overview: Define explicit pixel dimensions for the output PDF.

Step‑by‑Step Implementation

  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);

Explanation: Custom dimensions are handy for generating PDFs that fit specific screen sizes or print formats.

Common Issues and Solutions

ปัญหาสาเหตุที่เป็นไปได้วิธีแก้
IncorrectPasswordExceptionรหัสผ่านที่ให้ไม่ถูกต้องตรวจสอบสตริงรหัสผ่านอีกครั้ง; ตัด whitespace ที่เกินออก.
FileNotFoundExceptionเส้นทางไฟล์ไม่ถูกต้องใช้เส้นทางแบบ absolute หรือยืนยันไดเรกทอรีทำงาน.
Output PDF is blurryDPI ต่ำเกินไปเพิ่ม DPI ผ่าน options.setDpi().
Pages appear upside‑downการหมุนไม่ได้ตั้งค่าหรือตั้งค่าไม่ถูกต้องใช้ options.setRotate(Rotation.On180) (หรือ enum อื่น).
Converted file is larger than expectedDPI สูง + ขนาดใหญ่ลด DPI หรือปรับความกว้าง/ความสูงเพื่อสมดุลขนาดกับคุณภาพ.

Frequently Asked Questions

ถาม: ฉันสามารถแปลงเอกสาร Word ที่มีทั้งรหัสผ่านและการป้องกันแบบอ่าน‑อย่างเดียวได้หรือไม่?
ตอบ: ใช่. ส่งรหัสผ่านเปิดผ่าน WordProcessingLoadOptions.setPassword(). ธงอ่าน‑อย่างเดียวจะถูกละเว้นในการแปลง.

ถาม: GroupDocs.Conversion รองรับไฟล์ .doc (รุ่นเก่า) รวมถึง .docx หรือไม่?
ตอบ: รองรับอย่างแน่นอน. ไลบรารีจัดการทั้งสองรูปแบบโดยอัตโนมัติ.

ถาม: ประสิทธิภาพของ java convert word pdf ขยายตัวอย่างไรกับไฟล์ขนาดใหญ่?
ตอบ: GroupDocs สตรีมข้อมูลและปล่อยทรัพยากรหลังการแปลงแต่ละครั้ง สำหรับไฟล์ขนาดใหญ่มาก ควรเพิ่มขนาด heap ของ JVM และใช้เมธอด Converter.dispose() เมื่อเสร็จ.

ถาม: สามารถแปลงหลายเอกสารเป็นชุดได้หรือไม่?
ตอบ: ได้. วนลูปผ่านเส้นทางไฟล์, สร้าง Converter ใหม่สำหรับแต่ละไฟล์, และใช้ PdfConvertOptions เดียวกันเมื่อเหมาะสม.

ถาม: ฉันต้องการไลเซนส์เชิงพาณิชย์สำหรับการสร้างเวอร์ชันพัฒนาไหม?
ตอบ: รุ่นทดลองฟรีใช้ได้สำหรับการประเมิน, แต่การใช้งานจริงต้องมีไลเซนส์ GroupDocs.Conversion ที่ถูกต้อง.

Conclusion

คุณมีแผนที่ครบถ้วนและพร้อมใช้งานสำหรับการทำ groupdocs word to pdf ใน Java แล้ว รวมถึงการจัดการการป้องกันด้วยรหัสผ่าน, การเลือกหน้า, การหมุน, DPI, และขนาดที่กำหนดเอง ผสานส่วนโค้ดเหล่านี้ให้เข้ากับ workflow ของคุณ และคุณจะสามารถส่งมอบ PDF ที่ตรงตามความต้องการทางธุรกิจได้อย่างแม่นยำ


Last Updated: 2026-03-06
Tested With: GroupDocs.Conversion 25.2 for Java
Author: GroupDocs