Consulting

Results 1 to 12 of 12

Thread: Get page count of pdf files

  1. #1
    VBAX Regular
    Joined
    Dec 2011
    Posts
    33
    Location

    Get page count of pdf files

    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!...

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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/showthr...s-5-iTextSharp

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

  3. #3
    VBAX Regular
    Joined
    Dec 2011
    Posts
    33
    Location
    Sadly, I don't have Acrobat Distiller. So are you saying this would be impossible without it?

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  5. #5
    VBAX Regular
    Joined
    Dec 2011
    Posts
    33
    Location
    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.).

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  7. #7
    VBAX Regular
    Joined
    Dec 2011
    Posts
    33
    Location
    Sounds worth a try sir. Thanks. I'll be waiting...

  8. #8
    VBAX Tutor mohanvijay's Avatar
    Joined
    Aug 2010
    Location
    MADURAI
    Posts
    268
    Location
    Do you have Only Adobe Reader Or Adobe Acrobat?

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

  9. #9
    VBAX Regular
    Joined
    Dec 2011
    Posts
    33
    Location
    Really? I do have Adobe Acrobat. It would be great if that is possible. May I please know how? Thanks...

  10. #10
    VBAX Tutor mohanvijay's Avatar
    Joined
    Aug 2010
    Location
    MADURAI
    Posts
    268
    Location
    try this

    You have reference the Acrobat libaray in Tools->References
    [vba]

    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
    [/vba]

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

    [VBA]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
    [/VBA]

  12. #12
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.
    Last edited by Kenneth Hobs; 02-01-2012 at 09:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •