Get page count info from documents via command lines  SOLVED

This Forum is for the use of End Users requiring help and assistance for Tracker Software's PDF-Tools.

Moderators: Daniel - PDF-XChange, PDF-XChange Support, Vasyl - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Stefan - PDF-XChange

Post Reply
lebaotrung
User
Posts: 4
Joined: Mon Jan 13, 2025 10:42 am

Get page count info from documents via command lines

Post by lebaotrung »

Hi,
I am using PDF-Tools command lines to execute some pdf operation within VBA in excel. One of the task is to write the pdf document's page count to excel.
The solution I am using is to parse the pdf as binary and look using RegExp to look for "/Type /Page" to count the total number of pages inside. This work like most of the time.
However for some documents, especially those that are produced by merging multiple pdf(s) with PDF-Xchange merge, this counting method does not work (return count 0).

Is there any way via PDF-Tools command line that can return back the page count info of a pdf document so that VBA can read? Like write them inside a text file or something.

Thanks!
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19819
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Get page count info from documents via command lines

Post by Stefan - PDF-XChange »

Hello lebaotrung,

Welcome to our forums!

You can run JS via PDF Tools, and something simple as "this.numPages;" should get you the desired value. I am not 100% sure though how you can take that value back to your VBA for further processing.

Working with JS via PDF-Tools is explained here:
https://help.pdf-xchange.com/pdfxt10/in ... pt_t2.html

Kind regards,
Stefan
KunjanSoni
User
Posts: 3
Joined: Fri Dec 13, 2024 9:39 am

Re: Get page count info from documents via command lines

Post by KunjanSoni »

Hi lebaotrung,

Here is VBA code which provides page count of a PDF file.

Sub PDFpageCount()

Dim Inst As PDFXEdit.PXV_Inst
Dim PXC As PDFXEdit.PXC_Inst
Dim cDoc As PDFXEdit.IPXC_Document
Dim Path As String

Path = "C:\xxxx\test.pdf"


Set Inst = New PDFXEdit.PXV_Inst
Inst.Init

Set PXC = Inst.GetExtension("PXC")
PXC.Init ""

Set cDoc = PXC.OpenDocumentFromFile(Path, Nothing)
MsgBox cDoc.Pages.Count


End Sub
Nimrod_189
User
Posts: 60
Joined: Wed Sep 23, 2009 8:39 am

Re: Get page count info from documents via command lines

Post by Nimrod_189 »

In VBA I always use PDFtk. This allows you to make various edits or obtain information about PDF.

The call batch-command would then be:

Code: Select all

E:\Programmtools\PDFtk Server\bin\pdftk MyPDF.pdf dump_data output PageCount.txt
The result is a txt file that can easily be read with the following command:

Code: Select all

iFile = FreeFile
Open TmpFolder & "PageCount.txt" For Input As #iFile
Do While Not EOF(iFile)
Line Input #iFile, str
If InStrRev(str, "NumberOfPages", , vbTextCompare) <> 0 Then
Pagecount = Val(Right(str, Len(str) - Len("NumberOfPages: ")))
Exit Thu
End If
                          
loops

Close #iFile
Kill TmpFolder & "PageCount.txt"
Download PDFkt: [url]https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit[/url]

Nimrod
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 10921
Joined: Wed Jan 03, 2018 6:52 pm

Get page count info from documents via command lines

Post by Daniel - PDF-XChange »

:)
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
lebaotrung
User
Posts: 4
Joined: Mon Jan 13, 2025 10:42 am

Re: Get page count info from documents via command lines

Post by lebaotrung »

KunjanSoni wrote: Wed Jan 22, 2025 3:40 am Hi lebaotrung,

Here is VBA code which provides page count of a PDF file.

Sub PDFpageCount()

Dim Inst As PDFXEdit.PXV_Inst
Dim PXC As PDFXEdit.PXC_Inst
Dim cDoc As PDFXEdit.IPXC_Document
Dim Path As String

Path = "C:\xxxx\test.pdf"


Set Inst = New PDFXEdit.PXV_Inst
Inst.Init

Set PXC = Inst.GetExtension("PXC")
PXC.Init ""

Set cDoc = PXC.OpenDocumentFromFile(Path, Nothing)
MsgBox cDoc.Pages.Count


End Sub
Thanks KunjanSoni for the answer. This seems to be like the type of solution i am looking for. But seems like this required the SDK. I was told before that the SDK is not available product anymore?
lebaotrung
User
Posts: 4
Joined: Mon Jan 13, 2025 10:42 am

Re: Get page count info from documents via command lines

Post by lebaotrung »

Stefan - PDF-XChange wrote: Mon Jan 13, 2025 12:49 pm Hello lebaotrung,

Welcome to our forums!

You can run JS via PDF Tools, and something simple as "this.numPages;" should get you the desired value. I am not 100% sure though how you can take that value back to your VBA for further processing.

Working with JS via PDF-Tools is explained here:
https://help.pdf-xchange.com/pdfxt10/in ... pt_t2.html

Kind regards,
Stefan
Hi Stefan,
Did not get chance to try this out yet. Because my org's license is still with v9. The run JS features is v10. I'll need to get IT to update the version first.
Thanks!
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19819
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Get page count info from documents via command lines

Post by Stefan - PDF-XChange »

Hello lebaotrung,

Thanks for the update! Once you do get the chance - please let us know how it goes!

Kind regards,
Stefan
biogic
User
Posts: 1
Joined: Tue Feb 25, 2025 1:23 pm

Re: Get page count info from documents via command lines  SOLVED

Post by biogic »

You can use PDF-Tools on the command line to extract the number of pages and write it in a text file. A command like PDFXEdit.exe /GetNumPages "input.pdf" > output.txt might work. Then, in VBA, you can read this file and retrieve the value. This is more reliable than parsing the binary with RegExp, especially for merged PDFs.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19819
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Get page count info from documents via command lines

Post by Stefan - PDF-XChange »

Hello biogic,

Thanks for the suggestion!
However /GetNumPages never existed as a command in the Editor, so did you use some AI to help with such a command? Maybe it exists in other PDF Tools and the AI suggested it when it is not a valid one for the Editor!

Kind regards,
Stefan
lebaotrung
User
Posts: 4
Joined: Mon Jan 13, 2025 10:42 am

Re: Get page count info from documents via command lines

Post by lebaotrung »

biogic wrote: Tue Feb 25, 2025 7:45 pm You can use PDF-Tools on the command line to extract the number of pages and write it in a text file. A command like PDFXEdit.exe /GetNumPages "input.pdf" > output.txt might work. Then, in VBA, you can read this file and retrieve the value. This is more reliable than parsing the binary with RegExp, especially for merged PDFs.
Haha this definitely works. Initially i was using another dumb method (but works). I have a tool to save as the PDF file as another pdf with the file name a weird recognition name, and at the end %[Pages], which is then read by VBA to get the number of pages. This is so stupidly simple and inefficient but it works for now.
Post Reply