You can use Java libraries and Java language in SSJS. So if you have already solved this with a Java applet, it should be easy way you to port it to SSJS.
iText is a great library to handle PDF files, I think that would give the page count information quite easy.
"Every morning is the dawn of a new error..."
Totally agree you on this one :)