วิธีแปลง PDF เป็น Excel Workbook ที่แก้ไขได้ใน Java
คู่มือนี้แสดงวิธีแปลง PDF ให้เป็นเวิร์กบุ๊กที่คุณสามารถแก้ไขได้จริง ไม่ใช่แค่การส่งออกแบบแบน คุณจะทำการแปลง PDF เป็น XLSX ด้วย ExcelSaveOptions, ปรับการส่งออกเพื่อให้ตารางลงอย่างเรียบร้อยบนจำนวนแผ่นที่สมเหตุสมผล, และแปลงเวิร์กบุ๊กที่แก้ไขแล้วกลับเป็น PDF ด้วย ExcelLoadOptions.
แปลง PDF เป็นเวิร์กบุ๊ก Excel พื้นฐาน
Document.save กับ SaveFormat.Xlsx ทำการแปลงอย่างตรงไปตรงมา: ข้อความและเนื้อหาแบบตารางของแต่ละหน้าจะถูกวางลงบนแผ่นงานของตนเอง.
import org.aspose.pdf.Document;
import org.aspose.pdf.SaveFormat;
try (Document doc = new Document("report.pdf")) {
doc.save("report.xlsx", SaveFormat.Xlsx); // SaveFormat.Excel is an alias
}ปรับการส่งออกเพื่อให้สเปรดชีตสะอาดขึ้น
การส่งออกแบบหนึ่งแผ่นต่อหนึ่งหน้า มักไม่เป็นสิ่งที่ผู้ใช้สเปรดชีตต้องการ. ExcelSaveOptions ควบคุมการรวมแผ่นงาน, การจัดเรียงคอลัมน์, และอัลกอริทึมการกู้คืนตารางสำหรับ PDF ที่ไม่มีโครงสร้างตารางที่แท็กไว้:
import org.aspose.pdf.Document;
import org.aspose.pdf.ExcelSaveOptions;
try (Document doc = new Document("report.pdf")) {
ExcelSaveOptions options = new ExcelSaveOptions();
options.setFormat(ExcelSaveOptions.ExcelFormat.XLSX); // default
options.setMinimizeTheNumberOfWorksheets(true); // merge pages onto fewer sheets
options.setInsertBlankColumnAtFirst(false); // no leading spacer column
options.setUniformWorksheets(false); // allow per-sheet column widths
options.setStructuralHeuristics(true); // recover tables on untagged PDFs
options.setSuppressRunningHeadersFooters(true); // drop repeated page furniture
doc.save("report.xlsx", options);
}setMinimizeTheNumberOfWorksheets(true) เป็นตัวเลือกเดียวที่เปลี่ยนรูปแบบผลลัพธ์มากที่สุด: หากไม่มีมัน รายงาน 20 หน้า จะผลิต 20 แผ่นงาน.
แปลงเวิร์กบุ๊ก Excel กลับเป็น PDF
หลังจากผู้ตรวจสอบแก้ไขเวิร์กบุ๊กแล้ว ให้นำกลับเข้าสู่รูปแบบ PDF โดยโหลดด้วย ExcelLoadOptions แล้วบันทึกเอกสารที่ได้:
import org.aspose.pdf.Document;
import org.aspose.pdf.ExcelLoadOptions;
try (Document doc = new Document("book.xlsx", new ExcelLoadOptions())) {
doc.save("book.pdf");
}