프로그래밍/사용 라이브러리

ApachePDFBox - PDDocument

Mt.Hwang 2025. 4. 10. 15:52

ApachePDFBox

주로 사용하는 클래스는
 1. PDDocument
 2. PDFTextStripper
 3. PDFRenderer

..~~~~~..

 * PDDocument

PDF 문서를 나타내는 클래스
PDF 문서를 로드, 수정, 저장하는 클래스

..~~~~..

 * load(File file) -> PDDocument

file을 PDDocument 객체로 반환
(생성자 느낌?)

다만 2.xx 버전까지는 load()를 통해서 PDDocument 객체를 만들고
3.xx 버전부터는 Loader.loadPDF(File file) -> PDDocument를 이용해서 PDDocument 객체를 만든다

2.xx 버전까지는
PDDocument document = PDDocument.load(file);

3.xx 버전부터는
PDDocument document = Loader.loadPDF(file);

사용할 때 예외 처리 필요
ex)
try { ... }
catch (IOException e) {
e.printStackTrace();
}

..~~~~..

 * PDPage

PDDocument에서 사용하는 클래스
한 페이지라고 생각 (PDF 파일의 n번째 페이지)

..~~~~..

 * save(File file) -> void

PDDocument의 내용을 file에 저장

..~~~~..

 * getPage(int n) -> PDPage

n번째 페이지를 반환
PDPage 객체로 반환

index는 0부터 시작
getPage(3)은 2번째 페이지 리턴을 의미함

..~~~~..

 * getNumberOfPages() -> int

총 페이지 수 리턴

..~~~~..

 * getPages() -> PDPageTree

문서의 모든 페이지를 반환

List로 받는 것이 좋아 보인다
PDPageTree는 Iterable하므로 for-each문을 이용해 List로 변환 가능

ex)
PDDocument document = PDDocument.load(file);

List<PDPage> pagelist = new ArrayList<>();
for (PDPage page : document.getPages()) {
pageList.add(page);
}


참고로 PDPageTree는 페이지의 계층적 관리 구조

..~~~~..

 * close() -> void

PDDocument 객체 닫음
Scanner.close()와 비슷한 이유

..~~~~..

 

'프로그래밍 > 사용 라이브러리' 카테고리의 다른 글

OpenCV  (2) 2025.06.08
ApachePDFBox - PDFTextStripper  (0) 2025.04.10
ApachePDFBox - PDFRenderer  (0) 2025.04.10
MultipartFile  (0) 2025.04.10
Tesseract  (0) 2025.04.10