Author Topic: PDF Page Count  (Read 4718 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
PDF Page Count
« 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?

Andrea

  • Water Moccasin
  • Posts: 2372
Re: PDF Page Count
« Reply #1 on: July 26, 2011, 12:44:31 PM »
use VBScript

HERE
Keep smile...

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: PDF Page Count
« Reply #2 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.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: PDF Page Count
« Reply #3 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-)
« Last Edit: July 26, 2011, 03:19:47 PM by Lee Mac »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: PDF Page Count
« Reply #4 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.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: PDF Page Count
« Reply #5 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)

BlackBox

  • King Gator
  • Posts: 3770
Re: PDF Page Count
« Reply #6 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.
"How we think determines what we do, and what we do determines what we get."

Andrea

  • Water Moccasin
  • Posts: 2372
Re: PDF Page Count
« Reply #7 on: July 26, 2011, 04:20:53 PM »
well,..
as always i'm late..sorry.

Thank you Lee for your sharing your knowledge.

:)
Keep smile...

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: PDF Page Count
« Reply #8 on: July 26, 2011, 04:29:19 PM »
Thank you all, you guys never cease to amaze me.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: PDF Page Count
« Reply #9 on: July 26, 2011, 05:47:57 PM »
Cheers guys, glad it helps  8-)