Jump to content

insert all pdf pages


Guest

Recommended Posts

what happens if you try this?

(defun c:testatall (/ fn n pt)
  (setq fn (getfiled "Select PDF File" "" "pdf" 8))
  (setq pagecount (PDFPageCount fn))
  (setq n 1)
  (setq pt (getpoint "Insertion Point"));;prompt for insertion point here
  (repeat pagecount
    (setq n (1+ n))
  )
  (princ);quietly exit
)

 

Link to comment
Share on other sites

 I try the new code but the same

 

Insertion Point; error: bad argument type: fixnump: nil

 

 

(vl-load-com) (princ)

(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
)


(defun c:testatall (/ fn n pt)
  (setq fn (getfiled "Select PDF File" "" "pdf" 8))
  (setq pagecount (PDFPageCount fn))
  (setq n 1)
  (setq pt (getpoint "Insertion Point"));;prompt for insertion point here
  (repeat pagecount
    (setq n (1+ n))
  )
  (princ);quietly exit
)

 

Link to comment
Share on other sites

so next try it without the line " (setq pt (getpoint "Insertion Point"));;prompt for insertion point here " - slowly narrowing it down to where the problem is

Link to comment
Share on other sites

So your problem is going to be " (setq pagecount (PDFPageCount fn)) ", try the whole lot again but replace that with a 1 just to check. Unusual since PDFPageCount is one of Lee Macs, maybe complacency but I just assume they work perfectly or better - he hasn't failed me yet.

 

Below will insert just 1 page.

 

(vl-load-com) (princ)

(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
)


;;Insert all pdf pages of a file
;;assumes single row at 8-1/2" spacing
;;Demo only
;;D.C. Broad, Jr. 2012
;;Warning: Once started, cannot be escaped from till done.
(defun c:atall (/ fn n pt)
  (setq fn (getfiled "Select PDF File" "" "pdf" 8))
  (setq pagecount 1)
  (setq n 1)
  (setq pt (getpoint "Insertion Point"));;prompt for insertion point here

  (repeat pagecount
;;  (while
    (not (vl-catch-all-error-p ;avoid error trigger
        (vl-catch-all-apply
          '(lambda ()
            (command "-pdfattach" fn (itoa n) pt 1 0)
            )
          nil
        )
      )
    )
    (setq n (1+ n))
    (setq pt (polar pt 0.0 8.5))
  )
  (command);cancel out of index pdf underlay
  (princ);quietly exit
)

 

Link to comment
Share on other sites

Now work but insert only one page. The first code before the _countpage work too but need an esc to stop. Is any other way to fix the first code?

 

The first code is

 

  

;;Insert all pdf pages of a file
;;assumes single row at 8-1/2" spacing
;;Demo only
;;D.C. Broad, Jr. 2012
;;Warning: Once started, cannot be escaped from till done.
(defun c:atall (/ fn n pt)
(setq fn (getfiled "Select PDF File" "" "pdf" 8))
(setq n 1)
(setq pt '(0.0 0.0 0.0));;prompt for insertion point here
(while
(not (vl-catch-all-error-p ;avoid error trigger
(vl-catch-all-apply
'(lambda ()
(command "-pdfattach" fn (itoa n) pt 1 0)
)
nil
)
)
)
(setq n (1+ n))
(setq pt (polar pt 0.0 8.5))
)
(command);cancel out of index pdf underlay
(princ);quietly exit
)

 

Link to comment
Share on other sites

Work in progress - narrowing down where the problem is. Could be that the result from PDFPagecount gives a string and not an integer.. make it an integer and it will work... I'll look at that idea later if you can't get a solution

  • Like 1
Link to comment
Share on other sites

So you could try adding this function:

 

(defun texttoint ( mytext / )
  (if (= (type mytext) 'STR)(setq mytext (atof mytext))) ;;String to Real
  (if (= (type mytext) 'REAL)(setq mytext (fix (+ mytext (if (minusp mytext) -0.5 0.5))))) ;;From Lee Mac, real to Int
  mytext
)

 

and change the line 

  (setq pagecount ( PDFPageCount fn))

to

   (setq pagecount ( texttoint (PDFPageCount fn)))

in the Atall function, see what that does.

Link to comment
Share on other sites

4 minutes ago, prodromosm said:

I don't know how to do it.if someone can fix the code please help

 

Thanks

 

 

(vl-load-com)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun texttoint ( mytext / )
  (if (= (type mytext) 'STR)(setq mytext (atof mytext))) ;;String to Real
  (if (= (type mytext) 'REAL)(setq mytext (fix (+ mytext (if (minusp mytext) -0.5 0.5))))) ;;From Lee Mac, real to Int
  mytext
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;Insert all pdf pages of a file
;;assumes single row at 8-1/2" spacing
;;Demo only
;;D.C. Broad, Jr. 2012
;;Warning: Once started, cannot be escaped from till done.
(defun c:atall (/ fn n pt)
  (setq spacing 8.5) ;; spacing between sheets in inches. Modify as required.
  (setq fn (getfiled "Select PDF File" "" "pdf" 8)) ;;fn: PDF File Name
  (setq pagecount ( texttoint (PDFPageCount fn))) ;; Number of pages from function PDFPageCount
  (setq n 1)
  (setq pt (getpoint "Insertion Point"));;prompt for insertion point here

  (repeat pagecount
    (not (vl-catch-all-error-p ;avoid error trigger
        (vl-catch-all-apply
          '(lambda ()
            (command "-pdfattach" fn (itoa n) pt 1 0)
            )
          nil
        )
      )
    )
    (setq n (1+ n))
    (setq pt (list (+ (car pt) spacing) (cadr pt) (caddr pt)))
  )
  (command);cancel out of index pdf underlay
  (princ);quietly exit
)

 

Link to comment
Share on other sites

http://lee-mac.com/errormessages.html

bad argument type: fixnump: <value>

A function requiring an integer argument has been passed an argument of incorrect data type with the value noted in the error message.

 

The point "pt" isn't being set by getpoint for some reason.

I have a feeling whats going on. you have old/duplicate code somewhere with the same command that is not setting pt. when you type atall its running that code instead. try changing the command and see if it works.

 

(defun c:atall (/ fn n pt)
change to
(defun c:TEST (/ fn n pt)

 

  • Like 1
Link to comment
Share on other sites

43 minutes ago, Steven P said:

 

 



(vl-load-com)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun texttoint ( mytext / )
  (if (= (type mytext) 'STR)(setq mytext (atof mytext))) ;;String to Real
  (if (= (type mytext) 'REAL)(setq mytext (fix (+ mytext (if (minusp mytext) -0.5 0.5))))) ;;From Lee Mac, real to Int
  mytext
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;Insert all pdf pages of a file
;;assumes single row at 8-1/2" spacing
;;Demo only
;;D.C. Broad, Jr. 2012
;;Warning: Once started, cannot be escaped from till done.
(defun c:atall (/ fn n pt)
  (setq spacing 8.5) ;; spacing between sheets in inches. Modify as required.
  (setq fn (getfiled "Select PDF File" "" "pdf" 8)) ;;fn: PDF File Name
  (setq pagecount ( texttoint (PDFPageCount fn))) ;; Number of pages from function PDFPageCount
  (setq n 1)
  (setq pt (getpoint "Insertion Point"));;prompt for insertion point here

  (repeat pagecount
    (not (vl-catch-all-error-p ;avoid error trigger
        (vl-catch-all-apply
          '(lambda ()
            (command "-pdfattach" fn (itoa n) pt 1 0)
            )
          nil
        )
      )
    )
    (setq n (1+ n))
    (setq pt (list (+ (car pt) spacing) (cadr pt) (caddr pt)))
  )
  (command);cancel out of index pdf underlay
  (princ);quietly exit
)

 

 

More information in the hope that it will be helpful.
If landscape pages are rotated so that all pages are now portrait, all pages are inserted without overlap.
The original PDF, with a first page in landscape orientation, stops, meaning program terminates, after inserting the first page.

 

Steve

Edited by StevJ
clarification
Link to comment
Share on other sites

something else that might help pinpoint the error could be to put this after the 'getpoint' line:

Should show you in the command line all the variables used up to that point

 

(princ fn)
(princ " - ")
(princ pagecount)
(princ " - ")
(princ pt)
(princ n)

 

  • Like 1
Link to comment
Share on other sites

Insertion PointC:\Users\Admin\Desktop\test.pdf - nil - (121.389 176.454 0.0)1; error: bad argument type: fixnump: nil

 

This is the new error

 

(vl-load-com)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun texttoint ( mytext / )
  (if (= (type mytext) 'STR)(setq mytext (atof mytext))) ;;String to Real
  (if (= (type mytext) 'REAL)(setq mytext (fix (+ mytext (if (minusp mytext) -0.5 0.5))))) ;;From Lee Mac, real to Int
  mytext
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;Insert all pdf pages of a file
;;assumes single row at 8-1/2" spacing
;;Demo only
;;D.C. Broad, Jr. 2012
;;Warning: Once started, cannot be escaped from till done.
(defun c:atall (/ fn n pt)
  (setq spacing 8.5) ;; spacing between sheets in inches. Modify as required.
  (setq fn (getfiled "Select PDF File" "" "pdf" 8)) ;;fn: PDF File Name
  (setq pagecount ( texttoint (PDFPageCount fn))) ;; Number of pages from function PDFPageCount
  (setq n 1)
  (setq pt (getpoint "Insertion Point"));;prompt for insertion point here
(princ fn)
(princ " - ")
(princ pagecount)
(princ " - ")
(princ pt)
(princ n)
  (repeat pagecount
    (not (vl-catch-all-error-p ;avoid error trigger
        (vl-catch-all-apply
          '(lambda ()
            (command "-pdfattach" fn (itoa n) pt 1 0)
            )
          nil
        )
      )
    )
    (setq n (1+ n))
    (setq pt (list (+ (car pt) spacing) (cadr pt) (caddr pt)))
  )
  (command);cancel out of index pdf underlay
  (princ);quietly exit
)

 

Link to comment
Share on other sites

Yup, that helps.

So the message, I asked if you could put in something to give us details.

Insertion Point - all OK, it asks for insertion point, the LISP works to that point OK

C:\Users\Admin\Desktop\test.pdf - the file you selected

nil - this is the problem - this is from 'pagecount'

(121.389 176.454 0.0) - insertion point, all OK

1 - as expected

; error: bad argument type: fixnump: nil - error telling us that something expected to be a number isn't a number - that 'nil' above

 

 

Maybe try a different PDF just in case that is the problem - that's the only thing that should be different to what you and I are doing.

 

If it all goes wrong replace the (setq pagecount ...) line with (setq pagecount (getint "Enter Number of Pages")) to add the number of pages manually,

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...