Get the number of pages in a PDF document

后端 未结 12 1827
你的背包
你的背包 2020-12-07 09:00

This question is for referencing and comparing. The solution is the accepted answer below.

Many hours have I searched for a fast and easy, but mostly a

12条回答
  •  再見小時候
    2020-12-07 09:20

    If you have access to shell, a simplest (but not usable on 100% of PDFs) approach would be to use grep.

    This should return just the number of pages:

    grep -m 1 -aoP '(?<=\/N )\d+(?=\/)' file.pdf
    

    Example: https://regex101.com/r/BrUTKn/1

    Switches description:

    • -m 1 is neccessary as some files can have more than one match of regex pattern (volonteer needed to replace this with match-only-first regex solution extension)
    • -a is neccessary to treat the binary file as text
    • -o to show only the match
    • -P to use Perl regular expression

    Regex explanation:

    • starting "delimiter": (?<=\/N ) lookbehind of /N (nb. space character not seen here)
    • actual result: \d+ any number of digits
    • ending "delimiter": (?=\/) lookahead of /

    Nota bene: if in some case match is not found, it's safe to assume only 1 page exists.

提交回复
热议问题