Dec 4, 2010

Getting the Number of Pages in PDF Files with Ruby

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).

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