Jump to content

insert all pdf pages


prodromosm
 Share

Recommended Posts

Hi .I am using this code to a multi-page pdf at once. I want two changes in this code

 

1) I want to select the insert point of the pdf and not start all the time from the 0,0

2) When iinsert all the pages the need to press esc to stop the code

 

Can ane one fix this code??

 

Thanks

 

 

;;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

if you look through it, question 1 is answered in the comments. Use the getpoint command instead of coordinates, google is your friend here.

You could change the line to  (setq pt (getpoint "Insertion Point")) 

  • Like 1
Link to comment
Share on other sites

Hi Steven P. Thanks for the replay. I did the change in the code and the first question solved. 

 

When i insert all the pages then need to press esc to stop the code. Why ? CAn any one fix it?

 

Thanks

 

 

;;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
(setq pt (getpoint "Insertion Point"))  
(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

It will be the while loop not finishing as it should, not sure why - but I haven't looked for you, someone might see something obvious though

Link to comment
Share on other sites

Google more pretty sure it was Lee-mac you can get how many pages in a pdf. So do a repeat not a while.  number of pdf pages autocad lisp

 

I found the answer as the 1st result and went 2 links down and found the number of pages in a pdf.lsp

Edited by BIGAL
Link to comment
Share on other sites

I find this but how to use it ? Is the Lee Mac code

 

http://www.theswamp.org/index.php?PHPSESSID=vo6pn2p86vt6mh6ab9khjfd3p7&topic=39001.msg441632#msg441632

 

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

 

Link to comment
Share on other sites

1 hour ago, prodromosm said:

I find this but how to use it ? Is the Lee Mac code

 

Read the top part of the lisp. you have to feed it a link to the pdf. Since your fn variable has that just feed it that.

 

(setq fn (getfiled "Select PDF File" "" "pdf" 8))
(setq n 1)
(setq pt (getpoint "Insertion Point"))  
(while

to

(setq fn (getfiled "Select PDF File" "" "pdf" 8)) ;might want to make this an if statment
(setq n 1)
(setq pt (getpoint "Insertion Point"))  
(repeat (_PDFPageCount fn)

 

Link to comment
Share on other sites

Neat programs.

Is there a way to move the insertion point of the next PDF page the width of the previously inserted page rather than a fixed distance?

Landscape pages will overlap portrait pages and makes quite a mess.

I changed the point distance from 8.5 to 17.25, and that fixes overlaps, but leaves large gaps between adjacent portrait pages and makes for a really wide and difficult to manage drawing with a 40-page PDF.

 

Steve

Link to comment
Share on other sites

@StevJ

Their might be a quicker/better way. Using bounding box on the last entity in the drawing aka the PDF page that was just inserted to find out its width. This should do what you asked for.  remember to also load the _PDFPageCount function.

 

(defun c:atall (/ fn n pt LL UR objwid)
  (setq fn (getfiled "Select PDF File" "" "pdf" 8)
        n 1 
        pt (getpoint "Insertion Point")
  )
  (repeat (_PDFPageCount fn)
    (command "-pdfattach" fn (itoa n) "_non" pt 1 0)
    (vla-getboundingbox (vlax-ename->vla-object (entlast)) 'minpt 'maxpt)
    (setq LL (vlax-safearray->list minpt)
          UR (vlax-safearray->list maxpt)
          objwid (- (car UR) (car LL))
          pt (polar LL 0 (+ objwid 1)) ;1 is the spacing you want between pages.
          n (1+ n)
    )
  )
  (command)
  (princ) 
)

 

Edited by mhupp
added notes
Link to comment
Share on other sites

Hi mhupp.  Thanks for the repay. I use the code and gives me this error !!!!

 

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

 

here is the code i use

 

  ;; 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

(defun _PDFPageCount ( filename / fob fso mat reg res str ) 
  (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)

  
(defun c:atall (/ fn n pt LL UR objwid)
  (setq fn (getfiled "Select PDF File" "" "pdf" 8)
        n 1 
        pt (getpoint "Insertion Point")
  )
  (repeat (_PDFPageCount fn)
    (command "-pdfattach" fn (itoa n) "_non" pt 1 0)
    (vla-getboundingbox (vlax-ename->vla-object (entlast)) 'minpt 'maxpt)
    (setq LL (vlax-safearray->list minpt)
          UR (vlax-safearray->list maxpt)
          objwid (- (car UR) (car LL))
          pt (polar LL 0 (+ objwid 1)) ;1 is the spacing you want between pages.
          n (1+ n)
    )
  )
  (command)
  (princ) 
)

 

Link to comment
Share on other sites

Copied what you posted and everything works on this end. Sounds like the error is happening when trying to insert the pdf. Finding out a lot of things work in Bricscad but not so well in Autocad.  (itoa n) is converting a integer into a string. Maybe Autocad needs a integer only?

 

(defun c:atall (/ fn n pt LL UR objwid)
  (setq fn (getfiled "Select PDF File" "" "pdf" 8)
        n 1
  )
  (setvar 'cmdecho 0)
  (if (setq pt (getpoint "Insertion Point"))
    (repeat (_PDFPageCount fn)
      (command "-pdfattach" fn n "_non" pt 1 0)
      (vla-getboundingbox (vlax-ename->vla-object (entlast)) 'minpt 'maxpt)
      (setq LL (vlax-safearray->list minpt)
            UR (vlax-safearray->list maxpt)
            objwid (- (car UR) (car LL))
            pt (polar LL 0 (+ objwid 1))  ;1 is the spacing you want between pages.
            n (1+ n)
      )
    )
    (prompt "\nAn Insertion Point is Needed")
  )
  (command)
  (setvar 'cmdecho 1)
  (princ)
)

 

 

Link to comment
Share on other sites

Is not working. Not insert the pdf

 

  ;; 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

(defun _PDFPageCount ( filename / fob fso mat reg res str ) 
  (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)

  
  

(defun c:atall (/ fn n pt LL UR objwid)
  (setq fn (getfiled "Select PDF File" "" "pdf" 8)
        n 1
  )
  (setvar 'cmdecho 0)
  (if (setq pt (getpoint "Insertion Point"))
    (repeat (_PDFPageCount fn)
      (command "-pdfattach" fn n "_non" pt 1 0)
      (vla-getboundingbox (vlax-ename->vla-object (entlast)) 'minpt 'maxpt)
      (setq LL (vlax-safearray->list minpt)
            UR (vlax-safearray->list maxpt)
            objwid (- (car UR) (car LL))
            pt (polar LL 0 (+ objwid 1))  ;1 is the spacing you want between pages.
            n (1+ n)
      )
    )
    (prompt "\nAn Insertion Point is Needed")
  )
  (command)
  (setvar 'cmdecho 1)
  (princ)
)

 

Link to comment
Share on other sites

This worked for me in AutoCAD, some subtle differences from what you had

 

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

I use autocad 2022 and gives me this this error with your code

 

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

 

Link to comment
Share on other sites

Not sure what part would cause that, it could be pagecount or could be pt, try commenting out the "(repeat pagecount" and the ")" the line after the "(setq pt (polar... "line

 

If that doesn't work maybe change 

(setq pt (list (+ (car pt) 8.5) (cadr pt) (caddr pt)))

instead of 

(setq pt (polar pt 0.0 8.5))

 

give a clue where the problem might be

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
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.

 Share

×
×
  • Create New...