DocumentLength.java
package edu.odu.cs.cs350;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
/**
* Checks if a PDF file is less than 50 pages.
* This makes sure the program only accepts small documents.
*/
public class DocumentLength {
/**
* Return true if the PDF has less than 50 pages.
* @param file the PDF file
* @return true if under 50 pages, false otherwise
*/
public static boolean isValidLength(File file) {
try (PDDocument document = Loader.loadPDF(file)) {
int pageCount = document.getNumberOfPages();
// just printing to check page count while testing
// System.out.println("Page count: " + pageCount);
// only accept files with less than 50 pages
return pageCount < 50;
} catch (IOException e) {
// if something goes wrong, print the error and return false
System.err.println("Error reading PDF: " + e.getMessage());
return false;
}
}
}