Java에서 PDF를 편집 가능한 Excel 워크북으로 변환하는 방법
이 가이드는 PDF를 단순한 평면 내보내기가 아니라 실제로 편집할 수 있는 워크북으로 변환하는 방법을 보여줍니다. ExcelSaveOptions를 사용해 PDF를 XLSX로 변환하고, 테이블이 적절한 수의 시트에 깔끔하게 배치되도록 내보내기를 조정한 뒤, 편집된 워크북을 ExcelLoadOptions으로 다시 PDF로 변환합니다.
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로 다시 변환하기
검토자가 워크북을 편집한 후, ExcelLoadOptions을(를) 사용해 로드하고 결과 문서를 저장하여 PDF 형태로 다시 변환합니다:
import org.aspose.pdf.Document;
import org.aspose.pdf.ExcelLoadOptions;
try (Document doc = new Document("book.xlsx", new ExcelLoadOptions())) {
doc.save("book.pdf");
}