Get page count info from documents via command lines
Moderators: Daniel - PDF-XChange, PDF-XChange Support, Vasyl - PDF-XChange, Chris - PDF-XChange, Sean - Tracker, Stefan - PDF-XChange
-
- User
- Posts: 3
- Joined: Mon Jan 13, 2025 10:42 am
Get page count info from documents via command lines
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!
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!
- Stefan - PDF-XChange
- Site Admin
- Posts: 19421
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Get page count info from documents via command lines
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
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
-
- User
- Posts: 3
- Joined: Fri Dec 13, 2024 9:39 am
Re: Get page count info from documents via command lines
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
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
- Stefan - PDF-XChange
- Site Admin
- Posts: 19421
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
-
- User
- Posts: 54
- Joined: Wed Sep 23, 2009 8:39 am
Re: Get page count info from documents via command lines
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:
The result is a txt file that can easily be read with the following command:
Download PDFkt: [url]https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit[/url]
Nimrod
The call batch-command would then be:
Code: Select all
E:\Programmtools\PDFtk Server\bin\pdftk MyPDF.pdf dump_data output PageCount.txt
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"
Nimrod
- Daniel - PDF-XChange
- Site Admin
- Posts: 10300
- Joined: Wed Jan 03, 2018 6:52 pm
Get page count info from documents via command lines

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
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
-
- User
- Posts: 3
- Joined: Mon Jan 13, 2025 10:42 am
Re: Get page count info from documents via command lines
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?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
-
- User
- Posts: 3
- Joined: Mon Jan 13, 2025 10:42 am
Re: Get page count info from documents via command lines
Hi Stefan,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
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!
- Stefan - PDF-XChange
- Site Admin
- Posts: 19421
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Get page count info from documents via command lines
Hello lebaotrung,
Thanks for the update! Once you do get the chance - please let us know how it goes!
Kind regards,
Stefan
Thanks for the update! Once you do get the chance - please let us know how it goes!
Kind regards,
Stefan