getting number of pages in a pdf

i am trying to get the number of pages in a pdf so i can automaticaaly split them into emailable sizes with arts splitter via applescript.

my code so far:


set theFile to "Macintosh HD:Users:~:Desktop:address combined_.pdf" as alias
set size_check1 to size of (info for theFile)

set combined_page_count to (count PDPage of theFile)

if size_check1 < (2281835 * 1) then display dialog "this should be ok: it's about " & {size_check1 / 1048576} & " MB"
if size_check1 = (2281835 * 1) then display dialog "this should be ok: it's about " & {size_check1 / 1048576} & " MB"

if size_check1 > (2281835 * 1) then display dialog "• the size of this file is " & {size_check1 / 1048576} & " MB" & "  •  so, i would split it into about " & ({size_check1 / 1048576} / 2) & " parts" & "of " & (combined_page_count / ({size_check1 / 1048576} / 2)) & " pages"


and the error message is “Can’t get PDPage of alias …”

any hints?

beta code:

set x to (read alias "path:to:file.pdf" for 200)
set numPages to word 1 of (text ((offset of "/N " in x) + 3) thru -1 of x) as integer

thanks. it gives me a number and the script runs but the number is not the number of pages and i’m afraid i do not understand your code so i cannot fix it.

If you open a pdf file with a plain text editor (eg, Tex-Edit Plus), you will see there lots of useful info. Those two lines simply extracted some of this info.

Ok. Here is a new beta version with support for various PDF documents I found in my disk:

set x to (read alias "path:to:file.pdf" for 200)

set PDFVersion to x's paragraph 1

if PDFVersion = "%PDF-1.3" or PDFVersion = "%PDF-1.5" then
	set pagesFlag to "/N "
else if PDFVersion = "%PDF-1.2" then
	set pagesFlag to "/Pages "
end if

set numPages to word 1 of (text ((offset of pagesFlag in x) + (pagesFlag's length)) thru -1 of x) as integer

I don’t know the PDF spec, so this may work or not, or may work only for some files, depending on its version, security settings and so on… But if you are the creator of the pdf files, you can open the first one and adjust this script to match the format you see, since all files will have the same structure.

Cheers!

thanks for the input. it really helped me think this through. after much trial and error, i have the code below:



set theFile to "Macintosh HD:Users:~:Desktop:address combined_.pdf" as alias
set size_check1 to size of (info for theFile)

tell application "Acrobat 5.0" to set combined_page_count to (count PDPage of document 1)

-- if the pdfs are about 2.1 MB, they should be easily sent via email

if size_check1 < (2281835 * 1) then display dialog "this should be ok: it's about " & {size_check1 / 1048576} & " MB"
if size_check1 = (2281835 * 1) then display dialog "this should be ok: it's about " & {size_check1 / 1048576} & " MB"

-- if the pdfs are bigger than 2.1 MB, they should be split into sections of about 2 MB each; arts splitter needs to be told how many pages it should make each of the fragments

-- so, find the total MB of the file; divide that total MB by 2 MB and get the number of fragments so each fragment will be about 2 MB

-- then, get the total page count of the pdf; divide the total page count by the number of fragments and get the number of pages in each fragment 

if size_check1 > (2281835 * 1) then display dialog "• the size of this file is " & {size_check1 / 1048576} & " MB" & "  •  so, i would split it into about " & ({size_check1 / 1048576} / 2) & " parts" & "  •  the total page count is " & combined_page_count & "  •  so, that's into fragments of " & combined_page_count / ({size_check1 / 1048576} / 2) & " pages"


now, i have to get the numbers to be whole numbers (or is it integers) – i have to get rid of the decimal places. the page numbers are, of course, ok, but the others all have 12 decimal places. the number of pages per fragment especially has to be without decimal places do it can be entered into arts splitter.

how to round to get integers is here:

http://bbs.applescript.net/viewtopic.php?t=4501&highlight=round