Monday 7 March 2016

PDFBox: Get number of pages in PDF file

Following statements are used to get number of pages in a PDF file.

PDDocument pdDoc = PDDocument.load(new File(fileName))
int numberOfPages = pdDoc.getNumberOfPages()



public static Optional<Integer> getNumberOfPages(String fileName) {
 if (Objects.isNull(fileName)) {
  throw new NullPointerException("fileName shouldn't be null");
 }
 try (final PDDocument pdDoc = PDDocument.load(new File(fileName))) {
  return Optional.of(pdDoc.getNumberOfPages());
 } catch (IOException e) {
  System.out.println(e.getMessage());
  return Optional.empty();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment