PDF 문서 조작 Java에서
Aspose.PDF FOSS for Java은 기존 PDF 문서를 조작하기 위한 풍부한 API 세트를 제공합니다 — 분할, 병합, 페이지 추출, 페이지 범위 삽입 및 페이지 내 콘텐츠 크기 조정. 이러한 기능은 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)는 내부 저장소를 힙에서 디스크로 전환합니다.
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의 6인자 생성자는 다음 순서대로 값을 받습니다: 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()는 저장된 값을 검사하고 그것이 백분율인지 절대 포인트 수인지 판단할 수 있게 해줍니다.
소책자 및 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()는 문서가 뷰어에서 열릴 때 초기 확대 배율을 제어하는 부동 소수점 곱셈값을 받아들입니다:
PdfPageEditor pageEditor = new PdfPageEditor();
pageEditor.bindPdf("input.pdf");
pageEditor.setZoom(1.5f);
pageEditor.save("zoomed.pdf");
0.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 for Java은 MIT 라이선스이며 https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java에서 사용할 수 있습니다.