TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: cmwade77 on July 26, 2011, 12:18:23 PM

Title: PDF Page Count
Post 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?
Title: Re: PDF Page Count
Post by: Andrea on July 26, 2011, 12:44:31 PM
use VBScript

HERE (http://docs.ongetc.com/?q=content/pdf-pages-counting-using-vb-script)
Title: Re: PDF Page Count
Post by: cmwade77 on July 26, 2011, 01:55:42 PM
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.
Title: Re: PDF Page Count
Post by: Lee Mac on July 26, 2011, 03:14:31 PM
Translated to Visual LISP :

Code: [Select]
(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-)
Title: Re: PDF Page Count
Post by: alanjt on July 26, 2011, 03:18:31 PM
Translated to Visual LISP :

Don't know if I'll ever use this, but it's going in the toolbox. Nice work.
Title: Re: PDF Page Count
Post by: Lee Mac on July 26, 2011, 03:23:53 PM
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)
Title: Re: PDF Page Count
Post by: BlackBox on July 26, 2011, 03:52:26 PM
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.
Title: Re: PDF Page Count
Post by: Andrea on July 26, 2011, 04:20:53 PM
well,..
as always i'm late..sorry.

Thank you Lee for your sharing your knowledge.

:)
Title: Re: PDF Page Count
Post by: cmwade77 on July 26, 2011, 04:29:19 PM
Thank you all, you guys never cease to amaze me.
Title: Re: PDF Page Count
Post by: Lee Mac on July 26, 2011, 05:47:57 PM
Cheers guys, glad it helps  8-)