Just a brief snippet on how to get the number of pages in a PDF file.
It works most of the time for conventional PDF files (not encrypted and not protected by password).
It works most of the time for conventional PDF files (not encrypted and not protected by password).
file = File.open('myfile.pdf','rb')
# 'rb' Required for windows!!!
text = file.read
file.close
keyword_c = text.scan(/Count\s+(\d+)/).size
keyword_t = text.scan(/\/Type\s*\/Page[^s]/).size
pages = keyword_c > keyword_t ? keyword_c : keyword_t
puts "Total pages: #{pages}"
No comments:
Post a Comment