TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: cmwade77 on July 26, 2011, 12:18:23 PM
-
Ok, so I used to use this program called PDF Tools to determine how many pages are in a PDF, so that I could the use a LISP routine and insert them. The problem is that the program will not run on 64-Bit systems.
I am wondering if anyone has a way to find how many pages are in a PDF using LISP?
-
use VBScript
HERE (http://docs.ongetc.com/?q=content/pdf-pages-counting-using-vb-script)
-
That does count the pages in PDFs, but how do I force the script to run from within a LISP routine, nothing seems to make it run, except double clicking it in Explorer.
-
Translated to Visual LISP :
(defun _PDFPageCount ( filename / fob fso mat reg res str )
;; Translation by Lee Mac of the VBScript code by Chanh Ong
;; found at http://docs.ongetc.com/?q=content/pdf-pages-counting-using-vb-script
;;
;; Call with fully qualified filename of PDF file:
;; (_PDFPageCount "C:\\Folder\\Filename.pdf")
;;
;; Returns integer describing number of pages in specified PDF file
(if
(and
(setq filename (findfile filename))
(eq ".PDF" (strcase (vl-filename-extension filename)))
)
(vl-catch-all-apply
(function
(lambda ( / _ReadAsTextFile _CountPage )
(defun _ReadAsTextFile ( fso fn / fob str res )
(setq fob (vlax-invoke fso 'getfile fn)
str (vlax-invoke fso 'opentextfile fn 1 0)
res (vlax-invoke str 'read (vlax-get fob 'size))
)
(vlax-invoke str 'close)
(vlax-release-object str)
(vlax-release-object fob)
res
)
(defun _CountPage ( rgx str / mat pag )
(vlax-put-property rgx 'pattern "/Type\\s*/Page[^s]")
(vlax-put-property rgx 'ignorecase actrue)
(vlax-put-property rgx 'global actrue)
(setq mat (vlax-invoke rgx 'execute str)
pag (vlax-get mat 'count)
)
(vlax-release-object mat)
(if (zerop pag) 1 pag)
)
(setq fso (vlax-create-object "Scripting.FileSystemObject")
reg (vlax-create-object "VBScript.RegExp")
str (_ReadAsTextFile fso filename)
res (_CountPage reg str)
)
)
)
)
)
(foreach obj (list str fob mat fso reg)
(vl-catch-all-apply 'vlax-release-object (list obj))
)
res
)
(vl-load-com) (princ)
8-)
-
Translated to Visual LISP :
Don't know if I'll ever use this, but it's going in the toolbox. Nice work.
-
Translated to Visual LISP :
Don't know if I'll ever use this, but it's going in the toolbox. Nice work.
Cheers Alan 8-)
Its a shame there isn't a faster method for this task, reading every byte of the PDF file is a slow process :-(
(BTW I've edited and tweaked a few things above)
-
Translated to Visual LISP :
Don't know if I'll ever use this, but it's going in the toolbox. Nice work.
1+ Very interesting.
-
well,..
as always i'm late..sorry.
Thank you Lee for your sharing your knowledge.
:)
-
Thank you all, you guys never cease to amaze me.
-
Cheers guys, glad it helps 8-)