AutoCAD LISP 獲取PDF的頁數
在這裡介紹Lee Mac所寫的函數。
來源至 Re: PDF Page Count
來源至 Re: PDF Page Count
程式碼如下:
(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)
範例:
(_PDFPageCount "C:\\Folder\\Filename.pdf")返回:值
留言
張貼留言