PDA

View Full Version : Get page count of pdf files



entwined
01-31-2012, 05:01 PM
Hi VB'ers :),

Is it possible to get the page count of pdf files through vb code? Like the pdfs are saved in one folder and it will list their filenames and page count of each in a simple excel sheet? Please help I really need this one. Any help would be very much appreciated.

Thanks a lot!... :)

Kenneth Hobs
01-31-2012, 06:06 PM
IF you had Adobe Acrobat Distiller, we could probably do it with that object.

You could use a vb.net solution. I use a c#.net DLL in my vb.net project. For details, see: http://www.wpuniverse.com/vb/showthread.php?32338-VB.NET-111-Pass-Parameters-5-iTextSharp

There may be a 3rd party application that we could shell to.

entwined
01-31-2012, 06:25 PM
Sadly, I don't have Acrobat Distiller. So are you saying this would be impossible without it? :(

Kenneth Hobs
01-31-2012, 07:16 PM
I did not use the word impossible. Obviously, solution one would not work for you since you do not have distiller, ergo my reason for giving two other solution methods.

For solution three, an example 3rd party program is pdfsam. There are many out there that use command line switches. The general method is to output to a txt or xml file and parse it. http://vbaexpress.com/forum/showthread.php?p=180767

At work, I use my vb.net solution method.

I might have another 3rd party program solution method other than pdfsam somewhere at work.

entwined
01-31-2012, 07:39 PM
Ok sir I see. I'm at work too but sadly, we are not allowed to install or download whatever programs or applications and that's why I ask if a vb code alone could do this (no shells, no .exes, etc.).

Kenneth Hobs
01-31-2012, 08:17 PM
There is a 4th method but I have not seen it done. The code would read the binary parts of the file's header to find the page count by translating characters.

For my vb.net EXE, may need the iTextSharp.DLL which you can install it to your c:\windows\system32. It also needs the vb.net framework files which some IT's install routinely. If that interests you, I can look for the VBA code that shell's to it and parses the output.

entwined
01-31-2012, 08:24 PM
Sounds worth a try sir. Thanks. I'll be waiting... :)

mohanvijay
01-31-2012, 08:29 PM
Do you have Only Adobe Reader Or Adobe Acrobat?

If you have Adobe Acrobat You can count PDF page count by VBA

entwined
01-31-2012, 08:49 PM
Really? I do have Adobe Acrobat. It would be great if that is possible. May I please know how? Thanks... :)

mohanvijay
01-31-2012, 10:40 PM
try this

You have reference the Acrobat libaray in Tools->References


Dim FSO As Object
Dim F_Fol As Object
Dim F_File As Object
Dim T_Str As String
Dim Dlg_Fol As FileDialog
Dim Ac_Fi As Acrobat.AcroPDDoc
Dim i As Long
Set Dlg_Fol = Application.FileDialog(msoFileDialogFolderPicker)
If Dlg_Fol.Show = -1 Then
T_Str = Dlg_Fol.SelectedItems(1)
Else
Set Dlg_Fol = Nothing
End If
Set Dlg_Fol = Nothing
Set FSO = CreateObject("Scripting.FileSystemObject")
Set F_Fol = FSO.getfolder(T_Str)
i = 2
For Each F_File In F_Fol.Files

T_Str = UCase(F_File.Path)
If Right(T_Str, 4) = ".PDF" Then

Set Ac_Fi = New Acrobat.AcroPDDoc

Ac_Fi.Open T_Str
Cells(i, 1).Value = T_Str
Cells(i, 2).Value = Ac_Fi.GetNumPages
i = i + 1
Ac_Fi.Close
Set Ac_Fi = Nothing

End If
Next
Set F_File = Nothing
Set F_Fol = Nothing
Set FSO = Nothing

Kenneth Hobs
02-01-2012, 09:30 AM
I had problems pasting the code with line feeds. Maybe this can be copied and pasted a bit easier. With Distiller, this worked fine. I just added a line to autofit the results.

I am not sure if I have the Acrobat object in my home computer but I will check. I used to have Distiller on it but I don't think that I reinstalled it after the last hard drive failure.

Sub GetPDFNumberOfPages()
Dim FSO As Object
Dim F_Fol As Object
Dim F_File As Object
Dim T_Str As String
Dim Dlg_Fol As FileDialog
'In VBE, add reference: Tools > References... > Acrobat > OK
Dim Ac_Fi As Acrobat.AcroPDDoc
Dim i As Long

Set Dlg_Fol = Application.FileDialog(msoFileDialogFolderPicker)
If Dlg_Fol.Show = -1 Then
T_Str = Dlg_Fol.SelectedItems(1)
Else: Set Dlg_Fol = Nothing
End If
Set Dlg_Fol = Nothing
Set FSO = CreateObject("Scripting.FileSystemObject")
Set F_Fol = FSO.getfolder(T_Str)
i = 2
For Each F_File In F_Fol.Files
T_Str = UCase(F_File.Path)
If Right(T_Str, 4) = ".PDF" Then
Set Ac_Fi = New Acrobat.AcroPDDoc
Ac_Fi.Open T_Str
Cells(i, 1).Value = T_Str
Cells(i, 2).Value = Ac_Fi.GetNumPages
i = i + 1
Ac_Fi.Close
Set Ac_Fi = Nothing
End If
Next

Range("A:B").Columns.AutoFit

Set F_File = Nothing
Set F_Fol = Nothing
Set FSO = Nothing
End Sub

Kenneth Hobs
02-01-2012, 09:01 PM
For the binary file reading method 4, see: http://www.mrexcel.com/forum/showthread.php?t=347911 Another is here, http://www.freevbcode.com/ShowCode.asp?ID=8153, but it always returned 0 for my files.

I would advise testing as the MrExcel routines worked fine for one pdf that was 2 pages and for the same number of pages in another, one routine returned 0 pages while the other returned an error at runtime. It is probably a matter or how the pdf files were created. e.g. Create a pdf by a method such as Excel's save/print to pdf or scan.

I would be surprised if you have Adobe Acrobat and not just the reader. This is why I did not show that method in my first response.

A 5th method would be similar to my method 2 but use vb.net to create a DLL that VBA can use directly. Of course vb.net's regasm.exe is needed to register the DLL once it was created. I explain all of this in another vb.net lesson at WPUniverse. I doubt that you would care to use that method.