PDF number of pages (1 Viewer)

pepepito78

New member
Local time
Today, 08:01
Joined
Mar 7, 2020
Messages
15
Good afternoon, do you know any VBA code that returns the number of pages of a pdf file?

Thank you
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:01
Joined
Oct 29, 2018
Messages
21,577
Hi. Not off hand, but it might require the use of Acrobat API, which means you'll need the full version of Adobe Acrobat. Do you have it?
 

pepepito78

New member
Local time
Today, 08:01
Joined
Mar 7, 2020
Messages
15
Hi. Not off hand, but it might require the use of Acrobat API, which means you'll need the full version of Adobe Acrobat. Do you have it?
No, I dont have, but I used:

Function NumeroPag(El_Fichero As String) As Integer

Dim Cadena As String
Dim NumeroF As Long
Dim ObjetoPDF As Object
Cadena = ""
Set ObjetoPDF = CreateObject("VBscript.RegExp")
ObjetoPDF.Global = True
ObjetoPDF.Pattern = "/Type\s*/Page[^s]"
NumeroF = FreeFile
Open (El_Fichero) For Binary As #NumeroF
Cadena = Space(LOF(NumeroF))
Get #NumeroF, , Cadena
Close #NumeroF

NumeroPag = ObjetoPDF.Execute(Cadena).Count

End Function

but is there a file that works
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:01
Joined
Oct 29, 2018
Messages
21,577
No, I dont have, but I used:

Function NumeroPag(El_Fichero As String) As Integer

Dim Cadena As String
Dim NumeroF As Long
Dim ObjetoPDF As Object
Cadena = ""
Set ObjetoPDF = CreateObject("VBscript.RegExp")
ObjetoPDF.Global = True
ObjetoPDF.Pattern = "/Type\s*/Page[^s]"
NumeroF = FreeFile
Open (El_Fichero) For Binary As #NumeroF
Cadena = Space(LOF(NumeroF))
Get #NumeroF, , Cadena
Close #NumeroF

NumeroPag = ObjetoPDF.Execute(Cadena).Count

End Function

but is there a file that works
Hi. What do you mean by "but is there a file that works?" What happened when you tried that code?
 

pepepito78

New member
Local time
Today, 08:01
Joined
Mar 7, 2020
Messages
15
It has always worked, but today I received pdf files created differently and it does not read the number of pages
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:01
Joined
Oct 29, 2018
Messages
21,577
It has always worked, but today I received pdf files created differently and it does not read the number of pages
Re: bolded part in your statement, is that true? Did the code use to work on a different set of PDF files created differently than the set of files you're using now?
 

pepepito78

New member
Local time
Today, 08:01
Joined
Mar 7, 2020
Messages
15
It is a pdf created by another application to the usual one from which the number of pages is not obtained
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:01
Joined
Oct 29, 2018
Messages
21,577
It is a pdf created by another application to the usual one from which the number of pages is not obtained
I guess that means your code is not compatible with the new set of PDF files. Do you know if the new set of PDF files are OCR'd or are they just images (meaning, the text on the pages cannot be "read" by the computer)? For example, can you select a word or a character or a sentence using the mouse?
 

pepepito78

New member
Local time
Today, 08:01
Joined
Mar 7, 2020
Messages
15
if it can be selected, the application reads binary and looks for the text 'page count' and in this new pdf it does not exist, so I wanted to use another way to do it
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:01
Joined
Oct 29, 2018
Messages
21,577
if it can be selected, the application reads binary and looks for the text 'page count' and in this new pdf it does not exist, so I wanted to use another way to do it
Okay, that's what I understood in your original post. You want to "actually count the pages" in a PDF document rather than rely on some text for pagination in the file, correct? If so, the only way I can think of is by using Acrobat API. I'm sure there are other ways, but I haven't tried any of them. Since you don't have a full version of Acrobat, the API approach won't work for you. You might look up PDFtk and see if they may have something you can use. Good luck!
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:01
Joined
Oct 29, 2018
Messages
21,577
Thank you!
Hi. You're welcome. Sorry I wasn't able to help much. Hope you find an answer using PDFtk. If you do, please consider sharing the solution with us. Cheers!
 

Users who are viewing this thread

Top Bottom