Java에서 PDF 페이지 추가

Aspose.PDF for Java를 사용하면 빈 페이지를 삽입하거나 다른 문서에서 페이지를 가져올 수 있습니다.

특정 위치에 빈 페이지 삽입

기존 PDF 중간에 빈 페이지를 추가해야 할 때 이 예를 사용하세요.

  1. 원본 PDF Document를 여세요.
  2. 페이지 컬렉션의 대상 위치에 새 페이지를 삽입하세요.
  3. 업데이트된 문서를 저장하세요.
public static void insertEmptyPage(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getPages().insert(2);
        document.save(outputFile.toString());
    }
}

끝에 빈 페이지 추가

새로운 빈 마지막 페이지로 문서를 확장해야 할 때 이 예를 사용하세요.

  1. 원본 PDF Document를 여세요.
  2. 페이지 컬렉션 끝에 새 페이지를 추가하세요.
  3. 수정된 PDF를 저장하세요.
public static void addEmptyPageToEnd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getPages().add();
        document.save(outputFile.toString());
    }
}

다른 문서의 페이지 추가

한 PDF의 페이지를 다른 PDF로 가져오려는 경우 이 예를 사용하세요.

  1. 대상 Document를 생성하고 원본 문서를 여세요.
  2. 필요한 대상 콘텐츠를 추가하고 소스 PDF에서 대상 페이지를 가져오세요.
  3. 결과 문서를 저장하세요.
public static void addPageFromAnotherDocument(Path inputFile, Path outputFile) {
    try (Document document = new Document();
         Document anotherDocument = new Document(inputFile.toString())) {
        document.getPages().add().getParagraphs().add(new TextFragment("This is first page!"));
        document.getPages().add(anotherDocument.getPages().get_Item(1));
        document.save(outputFile.toString());
    }
}