วิธีการทํางานกับ PDF Facades ใน Java

วิธีการทํางานกับ PDF Facades ใน Java

การแสดงความหมายแบบแบน

การทดหมายทั้งหมดในกระแสเนื้อหาหน้า เพื่อให้มันไม่สามารถแก้ไขหรือลบได้เป็นวัตถุการทอดหมายที่แยกกันอีกต่อไป นี่คือขั้นตอนทั่วไปก่อนพิมพ์หรือเก็บเอกสาร:

try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
    editor.bindPdf("annotated.pdf");
    editor.flattenAnnotations();
    editor.save("flat.pdf");
}

ลบตราหมาย

ลบตราลักษณ์อธิบายทั้งหมดจากเอกสาร ตราเลิศ (ที่เรียกว่า กล่อง) เป็นรายการต้นทางเดินทัศนคติ ที่สามารถมองเห็นได้ในบาร์ข้างของเครื่องอ่าน PDF:

try (PdfBookmarkEditor editor = new PdfBookmarkEditor()) {
    editor.bindPdf("input.pdf");
    editor.deleteBookmarks();
    editor.save("no-bookmarks.pdf");
}

แทนข้อความ

แทนรหัสในทุกหน้าของเอกสาร. นี้เป็นประโยชน์สําหรับการสรรหาตัวเก็บตําแหน่งแบบเทมป์ลัด ก่อนกระจายเภท:

try (PdfContentEditor editor = new PdfContentEditor()) {
    editor.bindPdf("template.pdf");
    editor.replaceText("{{Date}}", "2026-05-24");
    editor.save("output.pdf");
}

การเข้ารหัสเอกสาร

ใช้การเข้ารหัส AES-256 ด้วยอนุญาตทางใช้ PdfFileSecurity.- ครับ DocumentPrivilege.getForbidAll() preset disables all operations; การอนุญาตส่วนตัว จากนั้นสามารถเปิดใหม่ได้ด้วยวิธีการเซตเตอร์:

try (PdfFileSecurity security = new PdfFileSecurity("input.pdf", "secured.pdf")) {
    DocumentPrivilege priv = DocumentPrivilege.getForbidAll();
    priv.setAllowPrint(true);
    security.encryptFile("user", "owner", priv, KeySize.x256);
}

ข้อความถอดออก

การคัดแปลงบทความหน้าโดยใช้ PdfExtractor.โทรหาฉันนะ extractText() การประมวลผลครั้งแรก ทุกหน้า แล้วหาข้อความของแต่ละหน้าด้วย getNextPageText():

try (PdfExtractor extractor = new PdfExtractor()) {
    extractor.bindPdf("document.pdf");
    extractor.extractText();
    while (extractor.hasNextPageText()) {
        String text = extractor.getNextPageText();
        System.out.println(text);
    }
}

See Also

 ภาษาไทย