การจัดการเอกสาร PDF ใน Java
Aspose.PDF FOSS สำหรับ Java มีชุด API ที่ครอบคลุมสำหรับการจัดการเอกสาร PDF ที่มีอยู่ — การแยก, การต่อ, การดึงหน้าต่าง ๆ, การแทรกช่วงหน้า, และการปรับขนาดเนื้อหาในแต่ละหน้า ความสามารถเหล่านี้รวมอยู่ใน PdfFileEditor และคลาสคู่หู ContentsResizeParameters และ ContentsResizeValue.
PdfFileEditor: การดำเนินการหน้าหลัก
คลาส PdfFileEditor เป็นศูนย์กลางสำหรับการดำเนินการไฟล์ PDF ระดับสูง มันให้เมธอดสำหรับต่อหลายไฟล์ PDF, ดึงส่วนย่อยของหน้า, แยกเอกสารเป็นหน้าต่าง ๆ, และแทรกหน้าจากเอกสารหนึ่งไปยังอีกเอกสารหนึ่งในตำแหน่งที่กำหนด.
PdfFileEditor editor = new PdfFileEditor();
// Concatenate two documents into one
editor.concatenate("first.pdf", "second.pdf", "combined.pdf");
// Extract pages 2 through 5 to a new file
editor.extract("source.pdf", 2, 5, "extracted.pdf");
เมธอดหลักได้แก่:
concatenate(inputFiles, outputFile)— รวมหลายเอกสารตามลำดับextract(inputFile, startPage, endPage, outputFile)— ส่งออกช่วงต่อเนื่องextract(inputFile, pageNumbers, outputFile)— ส่งออกชุดที่ไม่ต่อเนื่องsplitToPages(inputFile)— แบ่งเป็นByteArrayOutputStreamหนึ่งรายการต่อหน้าinsert(inputFile, insertPosition, portFile, startPage, endPage, outputFile)— แทรกช่วงdelete(inputFile, pageNumbers, outputFile)— ลบหน้าที่ระบุappend(inputFile, portFile, startPage, endPage, outputFile)— เพิ่มช่วงหน้า
สำหรับไฟล์ขนาดใหญ่, setUseDiskBuffer(true) จะเปลี่ยนการจัดเก็บภายในจาก heap ไปเป็น disk.
การปรับขนาดเนื้อหาด้วย ContentsResizeParameters
PdfFileEditor.resizeContents() ปรับเนื้อหาให้อยู่ภายในขอบเขตหน้าที่มีอยู่แล้ว. แต่ละระยะขอบ — ซ้าย, ขวา, บน, ล่าง — และความกว้างและความสูงของเนื้อหาถูกระบุเป็นอินสแตนซ์ ContentsResizeValue, สร้างโดยเมธอดแฟคทอรี ContentsResizeValue.percents(value) หรือ ContentsResizeValue.units(value).
try (Document doc = new Document("input.pdf")) {
PdfFileEditor editor = new PdfFileEditor();
ContentsResizeParameters params = new ContentsResizeParameters(
ContentsResizeValue.percents(10), // left margin
ContentsResizeValue.percents(80), // content width
ContentsResizeValue.percents(10), // right margin
ContentsResizeValue.percents(10), // top margin
ContentsResizeValue.percents(80), // content height
ContentsResizeValue.percents(10) // bottom margin
);
editor.resizeContents(doc, params);
doc.save("resized.pdf");
}
คอนสตรัคเตอร์ที่รับอากิวเมนต์หกตัวสำหรับ ContentsResizeParameters รับค่าตามลำดับ: leftMargin, contentsWidth, rightMargin, topMargin, contentsHeight, bottomMargin. ทั้งหกค่าต้องบวกกันเป็น 100% เมื่อใช้ค่าตามเปอร์เซ็นต์ หรือเท่ากับขนาดหน้ากระดาษเมื่อใช้ค่าจุดแบบคงที่.
ปรับขนาดหน้าที่เฉพาะ
เมื่อมีเพียงส่วนหนึ่งของหน้าเท่านั้นที่ต้องการปรับขนาด, การโอเวอร์โหลดที่รับอาร์เรย์ของหมายเลขหน้าประเภทจำนวนเต็มจะมุ่งเป้าไปยังหน้านั้น ๆ ขณะเดียวกันหน้าที่เหลือจะคงเดิม:
PdfFileEditor editor = new PdfFileEditor();
ContentsResizeParameters params = new ContentsResizeParameters(
ContentsResizeValue.units(28), // left margin in points
ContentsResizeValue.units(540), // content width
ContentsResizeValue.units(28), // right margin
ContentsResizeValue.units(28), // top margin
ContentsResizeValue.units(786), // content height
ContentsResizeValue.units(28) // bottom margin
);
// Only resize pages 1 and 3
editor.resizeContents(doc, new int[]{1, 3}, params);
ContentsResizeValue.getValue() และ isPercent() ให้คุณตรวจสอบค่าที่จัดเก็บและกำหนดว่ามันเป็นเปอร์เซ็นต์หรือจำนวนจุดแบบคงที่.
การจัดหน้าแบบ Booklet และ N-Up
PdfFileEditor มี makeBooklet() สำหรับจัดหน้าแบบหนังสือสองด้านและ makeNUp() สำหรับการจัดหลายหน้าในหนึ่งแผ่น ทั้งสองวิธีรับพาธไฟล์หรือสตรีม:
PdfFileEditor editor = new PdfFileEditor();
// Print as a folded booklet
editor.makeBooklet("input.pdf", "booklet.pdf");
// Tile 2 columns × 3 rows per sheet
editor.makeNUp("input.pdf", "nup.pdf", 2, 3);
PdfPageEditor: การควบคุมการซูมต่อหน้า
PdfPageEditor มุ่งเป้าไปยังคุณสมบัติของหน้าแต่ละหน้า เช่น ระดับการซูม. setZoom() รับตัวคูณแบบ float ที่ควบคุมการขยายเริ่มต้นเมื่อเปิดเอกสารในโปรแกรมดู:
PdfPageEditor pageEditor = new PdfPageEditor();
pageEditor.bindPdf("input.pdf");
pageEditor.setZoom(1.5f);
pageEditor.save("zoomed.pdf");
ค่าของ 0.0 หรือค่าติดลบจะถูกปฏิเสธ — getZoom() จะคืนค่า 1.0 ในกรณีนั้น ทำให้ PdfPageEditor เหมาะสำหรับการเตรียมเอกสารที่ต้องเปิดที่ระดับการขยายที่เฉพาะเจาะจงสำหรับการเข้าถึงหรือการใช้งานในคีออส.
เริ่มต้น
เพิ่มการพึ่งพา Maven และนำเข้าแพ็คเกจ facades:
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-pdf-foss</artifactId>
<version>26.8.0</version>
</dependency>import org.aspose.pdf.*;
import org.aspose.pdf.facades.*;
Aspose.PDF FOSS สำหรับ Java มีใบอนุญาต MIT และสามารถเข้าถึงได้ที่ https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java.