Get number of pages in a pdf using a cmd batch file

I can see there are a lot of questions for getting the number of pages in a a pdf with C, PHP and others but am wondering with a batch file or cmd is there a simple way of getting the number of pages?


Solution 1:

Using pdftk:

pdftk my.pdf dump_data | grep NumberOfPages

does the trick.

Solution 2:

Alternatively you can use the command, which returns only the number:

pdfinfo "${PDFFILE}" | grep Pages | sed 's/[^0-9]*//'

You will need the xpdf package (usually preinstalled in many distributions):

http://www.foolabs.com/xpdf/download.html

Solution 3:

QPDF is a lightweight alternative to PDFtk (requiring Java runtime) and pdfinfo (quite a dumb tool).

qpdf --show-npages file.pdf

It prints just the number of pages, needing no post-processing.

Packages for most Linux distributions exist, usually named just qpdf. Pages like Softpedia host binaries for Windows. The source code can be downloaded from SourceForge, or from the official GitHub repository.


The --show-npages option was added in a version after 4.1.0, in commit 91367239fd55f7c4996ed6158405ea10573ae3cb. To be compatible with version 4.1.0 and earlier, you can dump basic information about each page and count the pages. In Linux and OS X:

qpdf --show-pages file.pdf | grep -c ^page

On Windows, you should use findstr and find instead:

qpdf --show-pages file.pdf | findstr ^page | find /c /v ""

Solution 4:

Without any external tools (save the script bellow as .bat) :

@if (@X)==(@Y) @end /* JScript comment
@echo off

cscript //E:JScript //nologo "%~f0"  %*

exit /b 0
@if (@X)==(@Y) @end JScript comment */

   var args=WScript.Arguments;
   var filename=args.Item(0);
   var fSize=0;
   var inTag=false;
   var tempString="";
   var pages="";

   function getChars(fPath) {

        var ado = WScript.CreateObject("ADODB.Stream");
        ado.Type = 2;  // adTypeText = 2
        ado.CharSet = "iso-8859-1";
        ado.Open();
        ado.LoadFromFile(fPath);                     
        var fs = new ActiveXObject("Scripting.FileSystemObject");
        fSize = (fs.getFile(fPath)).size;

        var fBytes = ado.ReadText(fSize);
        var fChars=fBytes.split('');
        ado.Close();
        return fChars;
   }


   function checkTag(tempString) {

    if (tempString.length == 0 ) {
        return;
    }

    if (tempString.toLowerCase().indexOf("/count") == -1) {
        return;
    }

    if (tempString.toLowerCase().indexOf("/type") == -1) {
        return;
    }

    if (tempString.toLowerCase().indexOf("/pages") == -1) {
        return;
    }

    if (tempString.toLowerCase().indexOf("/parent") > -1) {
        return;
    }


    var elements=tempString.split("/");
    for (i = 0;i < elements.length;i++) {

        if (elements[i].toLowerCase().indexOf("count") > -1) {
            pages=elements[i].split(" ")[1];

        }
    }
   }

   function getPages(fPath) {
        var fChars = getChars(fPath);

        for (i=0;i<fSize-1;i++) {

            if ( fChars[i] == "<" && fChars[i+1] == "<" ) {
                inTag = true;
                continue;
            }

            if (inTag && fChars[i] == "<") {
                continue;
            }

            if ( inTag && 
                 fChars[i] == ">" &&
                 fChars[i+1] == ">" ) {

                inTag = false;
                checkTag(tempString);
                if (pages != "" ) {
                    return;
                }

                tempString="";

            }

            if (inTag) {
                if (fChars[i] != '\n' && fChars[i] != '\r') {
                   tempString += fChars[i];
                }
            }

        }

   }

   getPages(filename);
   if (pages == "") {
     WScript.Echo("1");
   } else {
    WScript.Echo(pages);
   }

It takes the path to the .pdf file and simply prints the number of the pages.Not pretty fast as it reads the pdf symbol by symbol , but could be optimized.