Java เปรียบเทียบไฟล์ PDF – มาสเตอร์ GroupDocs.Comparison API
ในคู่มือนี้คุณจะได้เรียนรู้วิธี java compare pdf files อย่างมีประสิทธิภาพด้วย GroupDocs.Comparison for Java ไม่ว่าคุณจะสร้างพอร์ทัลตรวจสอบสัญญา ระบบควบคุมเวอร์ชัน หรือเครื่องมือตรวจสอบเอกสารวิชาการ ความสามารถในการตรวจจับการเปลี่ยนแปลงแม้เพียงเล็กน้อยในเอกสาร PDF ถือเป็นสิ่งสำคัญ เราจะพาคุณผ่านการตั้งค่า โลจิกการเปรียบเทียบหลัก การสร้างตัวอย่าง การจัดการรหัสผ่าน การรวมคลาวด์ และแนวปฏิบัติที่ปรับประสิทธิภาพ—เพื่อให้คุณสามารถส่งมอบโซลูชันที่เชื่อถือได้ในเวลาไม่กี่นาที
คำตอบอย่างรวดเร็ว
- GroupDocs Comparison Java ทำอะไร? มันเปรียบเทียบเอกสารสองฉบับหรือมากกว่า, ไฮไลท์การเปลี่ยนแปลงทุกอย่าง, และสามารถสร้างภาพตัวอย่างเพื่อการตรวจสอบอย่างรวดเร็ว.
- รูปแบบไฟล์ที่รองรับคืออะไร? Word, PDF, Excel, PowerPoint, รูปภาพ, HTML, และอื่น ๆ อีกมาก.
- ต้องการไลเซนส์สำหรับการผลิตหรือไม่? ใช่ – ไลเซนส์ GroupDocs ที่ถูกต้องจะลบลายน้ำและเปิดใช้งานฟีเจอร์ทั้งหมด.
- สามารถจัดการเอกสารขนาดใหญ่ได้หรือไม่? ใช่ – ด้วยการจัดการหน่วยความจำที่เหมาะสมและการแบ่งหน้าตัวอย่าง คุณสามารถประมวลผล PDF หลายร้อยหน้าได้.
- จะหา Maven dependency ล่าสุดได้จากที่ไหน? บนรีโพสิตอรีของ GroupDocs – ตรวจสอบเวอร์ชันล่าสุดก่อนเพิ่มมัน.
java compare pdf files คืออะไร?
java compare pdf files หมายถึงกระบวนการโปรแกรมเมติกของการโหลดเวอร์ชัน PDF สองไฟล์, ตรวจจับความแตกต่างด้านข้อความ, การจัดรูปแบบ, และกราฟิก, และโดยอาจสร้างเอกสารผลลัพธ์ที่ทำเครื่องหมายการเปลี่ยนแปลงเหล่านั้นอย่างชัดเจน GroupDocs.Comparison ทำสิ่งนี้ด้วยการเรียก API เพียงครั้งเดียว, ลดความจำเป็นในการเขียนตรรกะ diff ด้วยตนเอง
ทำไมต้องใช้ GroupDocs.Comparison ในโครงการ Java?
GroupDocs.Comparison มอบ การตรวจจับการเปลี่ยนแปลงที่แม่นยำ ครอบคลุมกว่า 50 รูปแบบที่รองรับ, ประมวลผล PDF ขนาดสูงสุด 200 หน้าในเวลาต่ำกว่า 5 วินาที บน JVM 8 GB มาตรฐาน, และรวมฟีเจอร์การสร้างตัวอย่างในตัว แพคเกจ Maven‑ready ทำให้การรวมเป็นเรื่องง่าย, และไลบรารีสามารถสเกลแนวนอนได้เมื่อคุณปฏิบัติตามรูปแบบการจัดการหน่วยความจำที่แนะนำ
ข้อกำหนดเบื้องต้น: สิ่งที่คุณต้องการเพื่อเริ่มต้น
- Java Development Kit: JDK 8 หรือใหม่กว่า; แนะนำ JDK 11 หรือใหม่กว่าเพื่อประสิทธิภาพการเก็บขยะที่ดีกว่า.
- Build tool: Maven หรือ Gradle สำหรับการจัดการ dependencies.
- IDE: IntelliJ IDEA, Eclipse, หรือ VS Code – IDE Java สมัยใหม่ใดก็ได้ทำงาน.
- Basic Java knowledge: คุณควรคุ้นเคยกับคลาส, เมธอด, และการทำ I/O ของไฟล์.
- Network access: เพื่อดาวน์โหลด Maven artifact ของ GroupDocs และหากคุณใช้คลาวด์สตอเรจ, ต้องเข้าถึง endpoint ของสตอเรจของคุณ.
เพิ่ม GroupDocs.Comparison ไปยังโปรเจคของคุณ
Add the following dependency to your pom.xml (replace X.Y.Z with the newest version you find on the GroupDocs site):
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-comparison</artifactId>
<version>X.Y.Z</version>
</dependency>
Pro tip: ตรวจสอบบันทึกการปล่อยของ GroupDocs สำหรับการแก้บั๊กและการสนับสนุนฟอร์แมตใหม่ตามเวอร์ชันก่อนอัปเกรด
การให้ลิขสิทธิ์ (อย่าข้ามส่วนนี้!)
A trial license lets you evaluate the API, but production deployments require a permanent license to remove watermarks and enable full functionality.
- ทดลองใช้ฟรี: Download from GroupDocs
- Temporary license: Get one temporary license for extended testing
- Full license: Purchase from the GroupDocs Store
การตั้งค่าเบื้องต้น: เตรียม GroupDocs.Comparison ให้พร้อม
การเริ่มต้นพื้นฐาน
The Comparer class is the entry point for all comparison operations. It orchestrates document loading, diff calculation, and result generation.
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/comparison/java/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-comparison</artifactId>
<version>25.2</version>
</dependency>
</dependencies>
What the snippet does: It creates a Comparer instance, applies a license (if you have one), and prepares the engine for subsequent compare calls.
คู่มือการทำตามขั้นตอน
ส่วนที่ 1: ตั้งค่าการเปรียบเทียบเอกสาร
ขั้นตอนที่ 1: เริ่มต้น comparer ของคุณ
The Comparer object is the core component that executes the diff algorithm. It holds configuration such as comparison options and licensing information.
import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.examples.SampleFiles;
// Initialize the comparer with a source document
Comparer comparer = new Comparer(SampleFiles.SOURCE_WORD);
Why this matters: The source document you provide becomes the baseline; every change is measured against it.
ขั้นตอนที่ 2: เพิ่มเอกสารเป้าหมาย
You can compare the source with one or more target files. In a contract‑management scenario, the source is the original agreement and the target is the revised version from the legal team.
// Initialize comparer with the source document
Comparer comparer = new Comparer(SampleFiles.SOURCE_WORD);
ส่วนที่ 2: สร้างตัวอย่างหน้า
ขั้นตอนที่ 1: ตั้งค่าการสร้าง output stream
The preview delegate pattern gives you full control over where preview images are stored—local disk, cloud bucket, or a database.
// Add a target document for comparison
comparer.add(SampleFiles.TARGET1_WORD);
ขั้นตอนที่ 2: กำหนดค่าตัวเลือกการแสดงตัวอย่าง
Only generate previews for pages you actually need; this reduces CPU usage by up to 60 % for large PDFs.
import com.groupdocs.comparison.common.delegates.Delegates;
import java.io.FileOutputStream;
import java.io.OutputStream;
Delegates.CreatePageStream createPageStream = new Delegates.CreatePageStream() {
@Override
public OutputStream invoke(int pageNumber) {
String pagePath = "YOUR_OUTPUT_DIRECTORY" + "/result-GetPagePreviewsForTargetDocument_" + pageNumber + ".png";
try {
return new FileOutputStream(pagePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
};
ขั้นตอนที่ 3: สร้างตัวอย่าง
The API renders each requested page as a PNG (lossless) or JPEG (smaller size). PNG is recommended for legal documents where pixel‑perfect fidelity matters.
import com.groupdocs.comparison.options.PreviewOptions;
import com.groupdocs.comparison.options.enums.PreviewFormats;
// Set preview options
PreviewOptions previewOptions = new PreviewOptions.Builder(createPageStream)
.setPreviewFormat(PreviewFormats.PNG) // High-quality images
.setPageNumbers(new int[]{1, 2}) // Only generate what you need
.build();
รูปแบบไฟล์ที่รองรับ
GroupDocs.Comparison works with more than 50 input and output formats, including:
- Microsoft Office: DOCX, DOC, XLSX, XLS, PPTX, PPT
- PDF: ทุกเวอร์ชัน PDF‑1.x, PDF ที่เข้ารหัส, และ PDF ที่มีฟอนต์ฝังอยู่
- Text & markup: TXT, RTF, HTML, MHTML
- Images: JPEG, PNG, BMP, GIF
- OpenDocument: ODT, ODS, ODP
These broad capabilities let you compare a contract stored as a Word file with a scanned PDF version, all within the same API call.
ปัญหาทั่วไปและวิธีแก้
ปัญหา 1: FileNotFoundException ระหว่างการสร้างตัวอย่าง
If the output directory does not exist or the application lacks write permissions, the preview delegate throws this exception.
// Generate page previews
comparer.getTargets().get(0).generatePreview(previewOptions);
Solution: Verify the path, create missing folders programmatically, and grant the Java process appropriate file‑system rights.
ปัญหา 2: ปัญหาหน่วยความจำกับเอกสารขนาดใหญ่
Processing a 300‑page PDF can exceed the default 512 MB heap, resulting in OutOfMemoryError.
Delegates.CreatePageStream createPageStream = new Delegates.CreatePageStream() {
@Override
public OutputStream invoke(int pageNumber) {
String outputDir = "previews";
File directory = new File(outputDir);
if (!directory.exists()) {
directory.mkdirs(); // Create directory if it doesn't exist
}
String pagePath = outputDir + "/preview_page_" + pageNumber + ".png";
try {
return new FileOutputStream(pagePath);
} catch (FileNotFoundException e) {
System.err.println("Failed to create output file: " + pagePath);
throw new RuntimeException("Cannot create preview file", e);
}
}
};
Solution: Increase the JVM heap (-Xmx2g or higher) and process the document in smaller page ranges using the PreviewOptions pagination feature.
ปัญหา 3: ปัญหาการให้ลิขสิทธิ์
When a license file is missing or malformed, the API adds a watermark to every output and disables some advanced options.
// Process fewer pages at a time
PreviewOptions previewOptions = new PreviewOptions.Builder(createPageStream)
.setPreviewFormat(PreviewFormats.PNG)
.setPageNumbers(new int[]{1, 2, 3}) // Limit page range
.build();
// Always dispose of the comparer when done
try (Comparer comparer = new Comparer(sourceDocument)) {
comparer.add(targetDocument);
comparer.getTargets().get(0).generatePreview(previewOptions);
} // Automatic cleanup
Solution: Place the license file in a known location and load it with License.setLicense("path/to/license.lic") before any comparison call.
เคล็ดลับประสิทธิภาพและแนวทางปฏิบัติที่ดีที่สุด (java comparison best practices)
- จำกัดการสร้างตัวอย่าง – ขอเฉพาะหน้าที่ต้องการ; ใช้
pageNumbersเพื่อข้ามส่วนที่ไม่เกี่ยวข้อง. - เลือกรูปแบบภาพที่เหมาะสม – PNG สำหรับเอกสารกฎหมายที่ต้องการความคมชัดเต็ม, JPEG สำหรับภาพย่อที่ต้องการขนาดเล็ก.
- ใช้แคช – เก็บผลการเปรียบเทียบ (เช่น แฮชของ source + target) เพื่อหลีกเลี่ยงการประมวลผลไฟล์ที่ไม่เปลี่ยนแปลง.
- จัดการหน่วยความจำ – ห่อการใช้
Comparerด้วย try‑with‑resources block และเรียกclose()เมื่อเสร็จ. - ทำลายอ็อบเจ็กต์ comparer – ปล่อยทรัพยากรเนทีฟเสมอเพื่อให้ footprint ของ JVM ต่ำ.
รูปแบบโค้ดพร้อมใช้งานในโปรดักชัน
Below is the recommended lifecycle for a comparison task in a microservice.
// Apply license at the start of your application
License license = new License();
license.setLicense("path/to/your/license.lic");
ตัวอย่างการใช้งานจริง
ตัวอย่าง 1: ระบบจัดการสัญญา
A legal department uploads the original contract (source) and the revised version (target). The service returns a highlighted PDF and a set of page‑preview PNGs for quick visual inspection.
public class DocumentComparisonService {
private static final String OUTPUT_DIR = "document-previews/";
public ComparisonResult compareDocuments(String sourcePath, String targetPath) {
try (Comparer comparer = new Comparer(sourcePath)) {
comparer.add(targetPath);
// Perform comparison
String resultPath = OUTPUT_DIR + "comparison-result.docx";
comparer.compare(resultPath);
// Generate previews if needed
generatePreviews(comparer, 3); // First 3 pages only
return new ComparisonResult(resultPath, true);
} catch (Exception e) {
log.error("Document comparison failed", e);
return new ComparisonResult(null, false);
}
}
private void generatePreviews(Comparer comparer, int maxPages) {
// Implementation details...
}
}
ตัวอย่าง 2: การตรวจสอบเอกสารวิชาการ
Researchers submit revised manuscripts. The system compares the new PDF with the previous version, flags altered citations, and generates preview images for the reviewer dashboard.
public class ContractVersionManager {
public void reviewContractChanges(String originalContract, String revisedContract) {
try (Comparer comparer = new Comparer(originalContract)) {
comparer.add(revisedContract);
// Generate comparison document
String comparisonResult = "contracts/comparison-" + System.currentTimeMillis() + ".docx";
comparer.compare(comparisonResult);
// Create preview for stakeholder review
generatePreviewsForReview(comparer);
}
}
}
วิธี java compare pdf files กับการป้องกันด้วยรหัสผ่าน
LoadOptions is a class that lets you specify loading parameters such as passwords for encrypted documents. To compare a protected PDF, create a LoadOptions instance with the password, pass it to the Comparer when loading the document, and then run the comparison as usual. The library decrypts the file in memory, ensuring no plaintext is written to disk, and all subsequent operations—including preview generation and diff calculation—work transparently.
public class AcademicDocumentReview {
public void compareResearchDrafts(String draft1, String draft2) {
try (Comparer comparer = new Comparer(draft1)) {
comparer.add(draft2);
// Focus on specific sections (first 10 pages typically contain main content)
PreviewOptions options = new PreviewOptions.Builder(createPageStream)
.setPageNumbers(IntStream.rangeClosed(1, 10).toArray())
.setPreviewFormat(PreviewFormats.PNG)
.build();
comparer.getTargets().get(0).generatePreview(options);
}
}
}
การเปรียบเทียบเอกสารที่จัดเก็บในคลาวด์
If your source and target reside in Amazon S3, Azure Blob, or Google Cloud Storage, simply provide an InputStream obtained from the cloud SDK instead of a local file path.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("your-password");
Comparer comparer = new Comparer("protected-document.docx", loadOptions);
คำถามที่พบบ่อย
Q: How do I handle password‑protected documents?
A: Use LoadOptions to supply the password when creating the Comparer instance, as shown above.
Q: Can I compare documents stored in cloud storage?
A: Yes—simply provide input streams from your cloud provider to the Comparer.
Q: What’s the maximum file size GroupDocs.Comparison can handle?
A: There’s no hard limit, but for files larger than 100 MB you should increase the JVM heap size or process the document in smaller chunks to avoid OutOfMemoryError.
Q: How accurate is the comparison algorithm?
A: The library uses advanced diff algorithms that detect changes in text, formatting, images, tables, and embedded objects, delivering near‑pixel‑perfect visual diff results.
Q: Can I customize which types of changes are detected?
A: Absolutely. Set CompareOptions to enable or disable detection of text, formatting, images, tables, and more.
Q: Does the API support generating previews for only selected pages?
A: Yes—configure PreviewOptions with a specific pageNumbers array to limit output to the pages you need.
สรุป
You now have a complete, production‑ready guide to java compare pdf files with GroupDocs.Comparison. By following the setup steps, applying the performance recommendations, and leveraging the password‑handling and cloud‑integration snippets, you can embed robust document‑comparison capabilities into any Java application—whether it’s a contract‑review portal, an academic‑paper workflow, or a large‑scale document‑archiving system.
อัปเดตล่าสุด: 2026-11-01
ทดสอบด้วย: GroupDocs.Comparison 25.2
ผู้เขียน: GroupDocs
InputStream sourceStream = getDocumentFromCloud("source-doc-id");
InputStream targetStream = getDocumentFromCloud("target-doc-id");
Comparer comparer = new Comparer(sourceStream);
comparer.add(targetStream);