Column to display the number of pages in PDF files

The following script generates a column named "PDF pages" to display the page count of PDF files. To obtain the page numbers, use Poppler's "PDF Info."

The script works fine, but the "PDF Info" window appears every time it analyzes a PDF file. Could someone help me get "PDF Info" to run in the background? Thank you very much.

PDF pages.js.txt (1.3 KB)
PDF Info

function OnInit(initData) {
    initData.name = "PDF pages";
    initData.desc = "Column to display the number of pages in PDF files";
    initData.version = "1.0";
    initData.copyright = "(c) 2025 DASOTA";
    initData.default_enable = true;
}

function OnAddColumns(addColData) {
    var col = addColData.AddColumn();
    col.name = "PDFpages";
    col.label = "PDF pages";
    col.justify = "right";
    col.autorefresh = true;
    col.type = "number";
    col.method = "OnGet";
}

function OnGet(getData) {
    var item = getData.item;

    var ext = item.ext ? item.ext.toLowerCase() : "";
    if (ext !== ".pdf") {
        getData.value = "";
        return;
    }

    var shell = new ActiveXObject("WScript.Shell");
    var pdfinfoPath = "C:\\Poppler\\Library\\bin\\pdfinfo.exe"; // Path to the pdfinfo.exe file

    try {
        var exec = shell.Exec('"' + pdfinfoPath + '" "' + item.realpath + '"');
        var pages = "";
        while (!exec.StdOut.AtEndOfStream) {
            var line = exec.StdOut.ReadLine();
            if (line.match(/^Pages:\s*(\d+)/i)) {
                pages = RegExp.$1;
                break;
            }
        }
        getData.value = pages;
    } catch (e) {
        getData.value = "";
    }
}

Hi Dasota, good to read you again!

If you go to Preferencias / Columnas del Listado / Propiedades de Shell and search for "page", do you get that option? Because that property shows me the number of pages in PDFs.

There’s a chance, though, that it might depend on which PDF viewer you have installed.

1 Like

Why don't you use the import dialog for shell properties?

1 Like

Hi @errante and @Hardkorn, thanks a lot for the suggestions, but System.Document.PageCount doesn't return the page count of PDF files, at least in my case, since I use WS PDFelement as a PDF reader/editor. Could you tell me which PDF program you use? I can experiment and see if it works here.

I experimented in Windows Explorer, and the "Pages" column also doesn't show the page count of PDF files.

PDF-XChange Editor Free.

Excellent, I'll install it and then I'll tell you.

This is the one I use too.

1 Like

Ok @errante, I already installed it, I'll try it.

@errante and @Hardkorn, I got the result I wanted. I can now get the page count of PDF documents without even having to see the PDF Info windows. And I went a little further. I created another script that displays both the page count of PDF files and Office documents. This way, there's no need to have two separate columns, one for PDFs and another for Office (native to Opus).

I'm not going to publish the scripts here because I'm going to post them in the Buttons - Scripts section. Maybe they'll be useful to other people, and they'll have faster and cleaner access there. Thanks a lot!

3 Likes

Here you can access the script, for those who might be interested.
Final script

These are my most favorite threads in any forum anywhere on the internets!
:thread: /t/column-to-display-the-number-of-pages-in-pdf-files/55872/10
:thread: /t/pdf-page-counts-appear-in-explorer-but-missing-from-dopus/52984/2

These are my most favorite people in the world!
:nerd_face: @Leo
:nerd_face: @Hardkorn
:nerd_face: @errante
:nerd_face: @dasota
:nerd_face: @bssmith
:nerd_face: @lxp

I had successfully written to ADS (Alternate Data Stream) with python and then in PowerShell I could use PS> Get-Content to successfully return it, but then lost soooo many hours trying to get evaluator and script columns to work with it. :sleepy_face:

# Read the entire stream as a single string 
$pages = Get-Content -Path "C:\path\to\your.pdf:Pages" -Raw
Write-Output "Page count: $pages"

I'm glad I returned to this forum to fully digest every character of every post about PDF page counts that I could find.

BTW, in a rush to just make PDF-XChange Editor Free work I was at first discouraged after installing it and then removing it from the initial Blocked state at Preferences / Miscellaneous / Shell Extensions / Blocked, that it didn't show me the pages I had hoped for. :worried:

Lesson learned, slow down! I apparently allowed it and then just closed the window. Evidently you have to click 'Apply' after Allowing something that is blocked or it doesn't take.

I am THRILLED to announce that you fine folk have solved this for me. This even works when browsing PDF files on my NAS via it's hostname which is a Dropbox folder.

I'm going to make this page my browser's start page. :laughing:

THANK YOU ALL ! :star::star::star::star::star:

3 Likes