PDF 파일 작업을 Java에서 수행하는 방법

PDF 파일 작업을 Java에서 수행하는 방법

PDF 파일 연결

PdfFileEditor.concatenate()를 사용하여 여러 PDF 파일을 하나의 출력 파일로 병합합니다. 입력 경로 배열과 대상 출력 경로를 전달하십시오:

PdfFileEditor editor = new PdfFileEditor();
String[] inputs = {"part1.pdf", "part2.pdf", "part3.pdf"};
editor.concatenate(inputs, "merged.pdf");

페이지 범위 추출

시작 및 종료 페이지 번호(1부터 시작)를 사용하여 연속적인 페이지 범위를 추출합니다. 추출된 페이지는 새 파일에 기록됩니다:

PdfFileEditor editor = new PdfFileEditor();
// Extract pages 2 through 5 (1-indexed)
editor.extract("source.pdf", 2, 5, "extracted.pdf");

각 페이지를 개별 파일로 분할

문서를 분할하여 각 페이지가 별도의 파일이 되도록 합니다. 이 메서드를 호출하기 전에 출력 디렉터리가 이미 존재해야 합니다:

PdfFileEditor editor = new PdfFileEditor();
editor.splitToPages("document.pdf", "output_dir/");

페이지 콘텐츠 크기 조정

기존 페이지 경계 내에서 ContentsResizeParameters를 사용하여 콘텐츠를 스케일링합니다. 왼쪽 여백, 콘텐츠 너비, 오른쪽 여백, 위쪽 여백, 콘텐츠 높이 및 아래쪽 여백을 백분율로 지정하십시오 — 각 차원에서 합계가 100이어야 합니다:

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");
}

소책자 만들기

책자 인쇄 순서대로 페이지를 배치하여 문서를 접었을 때 올바르게 인쇄되도록 합니다. 이 과정은 양면 인쇄에 맞게 페이지를 적절히 재배열하고 짝을 맞춥니다:

PdfFileEditor editor = new PdfFileEditor();
editor.makeBooklet("input.pdf", "booklet.pdf");

참조

 한국어