DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Count Number of Pages in PDF Files  (Read 56,669 times)

nnebeel

  • Participant
  • Joined in 2013
  • *
  • Posts: 5
Count Number of Pages in PDF Files
« on: April 30, 2013, 05:27 PM »
I'd like a small program that allows me to select multiple PDF files or a directory with PDF files (recursive) and see the total number of pages across the files.

For example, if I have folder A with ten three-page PDF files and folder B with five five-page PDF files, and I select both folders, the program would display a dialog box with the total number of pages in the PDF files (55 pages).

Optionally, it would be nice to have the program output a delimited text file with the file name, file size, and number of pages, kind of like a DIR command, but with page numbers as an added property. Then I could import it into the database/spreadsheet program of my choice.

CMD or GUI would be fine.
« Last Edit: April 30, 2013, 09:20 PM by nnebeel »

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 3,217
Re: Count Number of Pages in PDF Files
« Reply #1 on: April 30, 2013, 09:06 PM »
This only gets you part way:

Command-line pdfinfo from the XPDF suite will print the page count of an individual file, amongst much other data.  So can
PDFTK, but again one file at a time.

You could put either in a batch file for multiple PDFs, but you'd have to massage the output data to make it tidy.

nnebeel

  • Participant
  • Joined in 2013
  • *
  • Posts: 5
Re: Count Number of Pages in PDF Files
« Reply #2 on: April 30, 2013, 09:52 PM »
Quote
You could put either in a batch file for multiple PDFs, but you'd have to massage the output data to make it tidy.

I had looked into using pdfinfo before, but the output would indeed need some massaging. The source code for pdfinfo is at https://github.com/flooved/pdf2image/blob/master/xpdf/pdfinfo.cc. When I use the command pdfinfo -meta [filename], I get the following output:

Code: (text)
Tagged:         no
Form:           none
Pages:          7
Encrypted:      no
Page size:      613 x 790 pts
File size:      339400 bytes
Optimized:      no
PDF version:    1.4

Maybe someone could create a spin-off from this so that, instead of creating a eight-line block of text, it created a single line in a delimited (maybe comma-delimited) text file with column headers in the first line—something like this:

Code: (text)
Tagged,Form,Pages,Encrypted,Page width,Page height,File size,Optimized,PDF Version
none,7,no,613,790,339400,no,1.4

Then it could easily be used in a batch file operation and/or in a GUI.

x16wda

  • what am I doing in this handbasket?
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
    • Participated in NANY Challenge Once
Re: Count Number of Pages in PDF Files
« Reply #3 on: May 01, 2013, 01:07 AM »
Couldn't you just grep the output to find the Pages: line then use set to pull out the page count?  Set has some niceties you don't often think about.
vi vi vi - editor of the beast

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Count Number of Pages in PDF Files
« Reply #4 on: May 01, 2013, 07:23 AM »
If you're willing to add a couple of extra files, (besides pdfinfo.exe), to get it working here's a DOS batch/command file that will output:

"Filename", pages

Call as follows:

PDF-Pages.cmd <directory>

Include quotes around the directory path if required, eg. PDF-Pages.cmd "D:\My Documents"

Output goes to a file named output.txt.

Code: (generic)
REM PDF-Pages.cmd
@echo off
del output.txt
for /r %1 %%f in (*.pdf) do pdfinfo.exe -meta "%%f" >out.txt & echo "%%f", | tr.exe -d "\r\n" >>output.txt & find "Pages:" out.txt | tr.exe -d "\055\056\072[:alpha:][:space:]" >>output.txt & echo. >>output.txt
del out.txt

You'll need tr.exe, libintl3.dll and libiconv2.dll from coreutils binaries and coreutils dependencies archives available here - put them in the same directory as the batch/command file.

Worked OK here over 380 PDF files in 21 sub-directories.

Anyway, it was an exercise in DOS :D

EDIT: Ooppss!  Just realised not what you're after  :-[
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: May 01, 2013, 07:36 AM by 4wd »

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: Count Number of Pages in PDF Files
« Reply #5 on: May 01, 2013, 10:15 AM »
Another way to crack this could be to use SysExporter - see SysExporter - (Screen-scraping) Export data from Windows controls - Mini-Review
You could then drop the PDF files' details (including page counts) into Excel and perform arithmetic or other operations - whatever you wanted to do with the data.
For example, you could copy this sort of data into Excel, from Windows Explorer (I'm using xplorer² in this screenshot clip):

[attach]
« Last Edit: May 01, 2013, 10:26 AM by IainB »

nnebeel

  • Participant
  • Joined in 2013
  • *
  • Posts: 5
Re: Count Number of Pages in PDF Files
« Reply #6 on: May 01, 2013, 06:10 PM »
Thank you all for your input! 4wd, your solution worked great! I only had to tweak it slightly to make it do exactly what I wanted. Here's the code I ended up using:

Code: (generic)
REM PDF-Pages.cmd
@echo off
del output.txt
for /r %1 %%f in (*.pdf) do pdfinfo.exe -meta "%%f" >out.txt & echo %%~nxf,| tr.exe -d "\r\n" >>output.txt & find "Pages:" out.txt | tr.exe -d "\055\056\072[:alpha:][:space:]" >>output.txt & echo. >>output.txt
del out.txt

I just changed the output file name so that it would only include the file name instead of the full path, and I deleted the quotes around the file name and the space after the comma so that Excel would handle it more easily. These are obviously minor tweaks. I also realized that a comma might not be the best delimiter here because file names can include commas, too. But that would be easily fixed if I needed to.

Of course, we could also use TR to extract the other fields (if we wanted to), but this is a great solution for me! TR seems very powerful. I haven't had much use for GNU commands before. Now I have some new stuff to play with!

Thank you!

IanB, that sounds like a nifty solution, but I was hoping for something I could use without having to install anything.

x16wda, sure, GREP would work, too.

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: Count Number of Pages in PDF Files
« Reply #7 on: May 01, 2013, 06:48 PM »

nnebeel

  • Participant
  • Joined in 2013
  • *
  • Posts: 5
Re: Count Number of Pages in PDF Files
« Reply #8 on: May 01, 2013, 07:10 PM »
skwire, thanks for playing the trump card!  :Thmbsup:

PDFInfoGUI
Export CSV button? Check.
Folder recursion? Check.
Custom PDF file list? Check.
Separate fields for path and file name? Check.
Full document properties? Check.

Best tool I've found this month! Had to come to the experts to find it, though.

Thanks!

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: Count Number of Pages in PDF Files
« Reply #9 on: May 01, 2013, 08:05 PM »
Right on.  =]  BTW, welcome to the DonationCoder site and enjoy your stay.  =]

velvasi

  • Participant
  • Joined in 2010
  • *
  • Posts: 1
Re: Count Number of Pages in PDF Files
« Reply #10 on: August 08, 2013, 02:23 AM »
Hi, I came across this post when I was searching for pdf page counter.  So, I thought of sharing it when I found a simple freeware, so that it would be useful for somebody in the future.  http://download.cnet.com/TTFA-PDF-Page-Counter/3000-10743_4-75967133.html

Thank you!

senturion

  • Participant
  • Joined in 2014
  • *
  • Posts: 2
Re: Count Number of Pages in PDF Files
« Reply #11 on: July 21, 2014, 05:38 PM »
Thank you 4wd!!!


This greatly helped me. I'm now saving the script and binaries and dependencies into a zip for future use. I had also added a bit onto the batch to give me the file size in addition. Then when I open in Excel I'd convert it to kilobytes (as it is in bytes to begin with). Here is the script including file size:

Code
REM PDF-Pages.cmd
@echo off
del output.txt
for /r %1 %%f in (*.pdf) do pdfinfo.exe -meta "%%f" >out.txt & echo "%%f", | tr.exe -d "\r\n" >>output.txt & find "Pages:" out.txt | tr.exe -d "\r\n\055\056\072[:alpha:][:space:]" >>output.txt & echo , | tr.exe -d "\r\n" >>output.txt & find "File size:" out.txt | tr.exe -d "\055\056\072[:space:][:alpha:]" >>output.txt & echo. >>output.txt
del out.txt

« Last Edit: July 21, 2014, 06:29 PM by senturion »

senturion

  • Participant
  • Joined in 2014
  • *
  • Posts: 2
Re: Count Number of Pages in PDF Files
« Reply #12 on: July 21, 2014, 06:50 PM »
Perhaps a couple things to note when you are running the above script in a top-level directory containing 58,000+ PDF documents:

A. This will take a while :)

B. Prepare for cmd.exe to start consuming memory. We're up to 2.97 GB used by CMD.exe at the moment. I may have to let this run overnight instead.  :P

scaver

  • Participant
  • Joined in 2015
  • *
  • Posts: 1
Re: Count Number of Pages in PDF Files
« Reply #13 on: March 12, 2015, 06:49 AM »
Sorry for bumping up this threat, but I'm struggeling with the following.
I want to display the total number of pages of the pdfs of the (sub)folders as wel. So not only the final total.

Something like:
c:\pdfs\path1 83
c:\pdfs\path2 12
c:\pdfs\path2\subfolder 3
etc.

Is that possible?


Original address: https://www.donationcoder.com/forum/index.php?topic=34814.0