Jump to content
Sign in to follow this  
albertmaathuis

Number of pages in PDF document

Recommended Posts

albertmaathuis

Hi There,

With the autoit window info executable (au3info.exe) which comes with the download it is possible to see the number of pages witin a PDF document. On the tab "summary" it gives i.e. 1 of 6

For my program i need to know how many pages a PDF document consists of.

Is there a simple way of reading it from the document?

If not perhaps i can extract it from au3info if the au3 file is available?

Albert

Share this post


Link to post
Share on other sites
weaponx

;' open the PDF  in binary mode & count the pages
;' search for "/N  xx"
;'             or "/Count xx"

MsgBox(0, "", pagecount("C:\Documents and Settings\mjhorn\My Documents\Documents\BRKCRT-1102_1213129245.pdf"))

Func pagecount($sfilename)
    Local $c = 0
    Local $pages = -1

    ;'OPEN the PDF file
    $handle = FileOpen($sfilename,0)
    If @ERROR Then Return SetError(1,0,0)
    
    ;Get the data from the file
    Do
        $s = FileReadLine($handle)
        $EOF = @ERROR
        
        $c += 1
        
        ;Look within the top 10 lines for /N
        If $c <= 10 Then
            If StringInStr($s, "/N") > 0 Then
                $array1 = StringRegExp($s,"/N\h(\d+)",3)
                $pages = Number($array1[0])     
                ExitLoop
            EndIf
        EndIf
        
        ;Check every line for /count
        If StringInStr($s, "/count") > 0 Then
            $array2 = StringRegExp($s,"/count\h(\d+)",3)
            $pages = Number($array2[0])
            ExitLoop
        EndIf
    Until $EOF = -1
    
    FileClose($handle)
    If $pages < 0 Then $pages = 1
    Return $pages
EndFunc   ;==>pagecount

Partially converted from:

http://www.freevbcode.com/ShowCode.asp?ID=8153

Share this post


Link to post
Share on other sites
albertmaathuis

;' open the PDF  in binary mode & count the pages
;' search for "/N  xx"
;'             or "/Count xx"

MsgBox(0, "", pagecount("C:\Documents and Settings\mjhorn\My Documents\Documents\BRKCRT-1102_1213129245.pdf"))

Func pagecount($sfilename)
    Local $c = 0
    Local $pages = -1

    ;'OPEN the PDF file
    $handle = FileOpen($sfilename,0)
    If @ERROR Then Return SetError(1,0,0)
    
    ;Get the data from the file
    Do
        $s = FileReadLine($handle)
        $EOF = @ERROR
        
        $c += 1
        
        ;Look within the top 10 lines for /N
        If $c <= 10 Then
            If StringInStr($s, "/N") > 0 Then
                $array1 = StringRegExp($s,"/N\h(\d+)",3)
                $pages = Number($array1[0])     
                ExitLoop
            EndIf
        EndIf
        
        ;Check every line for /count
        If StringInStr($s, "/count") > 0 Then
            $array2 = StringRegExp($s,"/count\h(\d+)",3)
            $pages = Number($array2[0])
            ExitLoop
        EndIf
    Until $EOF = -1
    
    FileClose($handle)
    If $pages < 0 Then $pages = 1
    Return $pages
EndFunc   ;==>pagecount

Partially converted from:

http://www.freevbcode.com/ShowCode.asp?ID=8153

Share this post


Link to post
Share on other sites
albertmaathuis

Thx very much, this works great!!!

I'm using adobe reader 8.0, my friend 7.0 so i'm going to test it also on his PC

Thanks again for the code.

Albert

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.