在链接代码
PartDocs
是一个数组,其中每个元素都是一个CAcroPDDoc对象。
粗略地看一眼后,代码基本上接受要合并的PDF文件名的逗号分隔字符串,将此字符串解析为数组
a
,并将
PartDocs数组的大小调整
为与
a相同的大小
。然后,对于
a 中的每个文件名
,如果该文件存在,请将 PDF 文档对象添加到
PartDocs
数组中。
if i
部分位于
For
循环测试
i 是否
大于零(因为 VBA 中的 zero=false),如果是这样,则将 PDF 文档合并到索引
i
与索引
0 处的 PDF 文档
。
应该把它定义为偏头痛:
;|
(Merge ; Creates a file, but it contains only the first PDF file (I was testing on single-paged PDFs)
(strcat (getenv "userprofile") "\\Desktop\\Hale 515\\PDFs") ; in
(strcat (getenv "userprofile") "\\Desktop\\" "MergedPDFS.pdf") ; out
)
|;
; in - folder that contains .pdf files
; out - path\\filename.pdf
(defun Merge ( in out / *error* L n PartDocs ni n r )
(defun *error* ( m )
(and PartDocs (vl-catch-all-apply 'vlax-invoke-method (list PartDocs 'Close)))
(and AcroApp (vl-catch-all-apply 'vlax-invoke-method (list AcroApp 'CloseAllDocs)))
(and AcroApp (vl-catch-all-apply 'vlax-invoke-method (list AcroApp 'Exit)))
(foreach x (list AcroApp AcroPDDoc) (and (eq 'VLA-OBJECT (type x)) (vl-catch-all-apply 'vlax-release-object (list x))))
(gc) (gc) (and m (princ m)) (princ)
); defun *error*
(setq AcroApp (vlax-get-or-create-object "AcroExch.App"))
(setq L ((lambda (pat) (mapcar '(lambda (x) (strcat pat "\\" x)) (vl-directory-files pat "*.pdf" 1))) in))
; Not Sure how to adjust 'n'
(setq n (length L))
; (setq n 0)
(foreach x L ; The issue is within this loop
(setq PartDocs (vlax-get-or-create-object "AcroExch.PDDoc"))
(eq :vlax-true (vlax-invoke-method PartDocs 'Open x))
(setq ni (vlax-invoke-method PartDocs 'GetNumPages))
; Function InsertPages(nInsertPageAfter As Long, iPDDocSource As Object, lStartPage As Long, lNumPages As Long, lInsertFlags As Long) As Boolean
(if (eq :vlax-true (vlax-invoke-method PartDocs 'InsertPages (1- n) PartDocs 0 ni :vlax-true)) ;
(progn
(setq n (+ n ni))
); progn
(progn
(setq n (+ 1 (vlax-invoke-method PartDocs 'GetNumPages)))
); progn
); if
; (vlax-invoke-method PartDocs 'Close)
; (vlax-release-object PartDocs)
); foreach
(setq r
(eq :vlax-true
(vlax-invoke-method PartDocs 'Save
1 ; PDSaveFull
out
); vlax-invoke-method
); eq
); setq r
(*error* nil) r
); defun Merge
顺便说一句,李,谢谢你的意见,但我明天会读的。因为这就像喝伏特加。
http://youtu.be/5qHNQxZGZKU
http://youtu.be/5qHNQxZGZKU
啊,完全不知道这个乐队的存在。
最终得到了什么是CAcroPDDoc对象:
(setq o (vlax-get-or-create-object "AcroExch.PDDoc")) ; returns #
Tho,没有意识到在那个VBA代码中PartDocs是一个数组 - 如果是这样:看起来它无法在LISP中获得。
哦,或者你的意思是他手动为vla对象创建了一个
safearray
,并在以后用CAcroPDDoc对象填充它?
我有点明白了(当我在那里看他的第一个代码建议时).虽然更喜欢使用完整的.pdf文件路径
,所以这是ReDim函数。
这是有价值的信息,起初我试图添加单个PDF文档对象 - 然后我认为这似乎很不寻常,在循环中创建相同的对象而不释放它。
这就是为什么我的lisp代码不同的原因......但是在看了一堆类似的vba-acrobat-scripting代码之后,我意识到我错了。
对我来说,这是代码中最令人沮丧的部分。
最后,我开始在网上寻找“最简单的建议”,只是为了看看InsertPages是如何工作的。
停止在这个简单的例子,关于如何合并两个PDF文档,Sub Button1_Click,看起来我得到了它!
所以现在伙计们,我们有一个LISP子来合并PDF:
;| Examples:
(MergePDFs
(strcat (getenv "userprofile") "\\Desktop\\Hale 515\\PDFs") ; in
(strcat (getenv "userprofile") "\\Desktop\\" "MergedPDFS.pdf") ; out
)
(MergePDFs
(list ; in
(getfiled "Specify PDF file" (strcat (getenv "userprofile") "\\Desktop\\") "pdf" 16)
(strcat (getenv "userprofile") "\\Desktop\\I_Dont_Exist.pdf")
(getfiled "Specify PDF file" (strcat (getenv "userprofile") "\\Desktop\\") "pdf" 16)
(getfiled "Specify PDF file" (strcat (getenv "userprofile") "\\Desktop\\") "pdf" 16) ; You can cancel and provide nil if you want to
); list
(strcat (getenv "userprofile") "\\Desktop\\" "MergedPDFS.pdf") ; out
)
|;
; in - folder that contains .pdf files, or a list of ("\\.pdf" "\\.pdf" ..)
; out - path\\filename.pdf - Overwrites the existing (if theres one)
; If successful, Returns assoc list of '(( PageCount) ..)
(defun MergePDFs ( in out / *error* AcroApp L PD r )
; http://www.theswamp.org/index.php?topic=53974.msg586248#msg586248
(defun *error* ( m )
(and L (foreach x (mapcar 'car L) (vl-catch-all-apply 'vlax-invoke-method (list x 'Close)) (vl-catch-all-apply 'vlax-release-object (list x))))
(and AcroApp
(vl-catch-all-apply 'vlax-invoke-method (list AcroApp 'CloseAllDocs))
(vl-catch-all-apply 'vlax-invoke-method (list AcroApp 'Exit))
(vl-catch-all-apply 'vlax-release-object (list AcroApp))
); and
(gc) (gc) (and m (princ m)) (princ)
); defun *error*
(and in out
(or (setq AcroApp (vlax-get-or-create-object "AcroExch.App")) (prompt "\nUnable to interfere with Acrobat object.") )
(cond
( (eq 'STR (type in)) ; folder provided
(setq L ((lambda (pat / tmp) (if (setq tmp (vl-directory-files pat "*.pdf" 1)) (mapcar (function (lambda (x) (list (vlax-get-or-create-object "AcroExch.PDDoc") (strcat pat "\\" x)))) tmp))) in))
)
( (vl-consp in) ; list of .pdf filepaths'n'names provided
(setq L (mapcar (function (lambda (x) (list (vlax-get-or-create-object "AcroExch.PDDoc") x))) in))
)
); cond
(setq L (apply (function append) (mapcar (function (lambda (x) (if (eq :vlax-true (vlax-invoke-method (car x) 'Open (cond ((cadr x)) ("")))) (list (append x (list (vlax-invoke-method (car x) 'GetNumPages))))))) L)))
(setq PD (caar L))
(progn
(foreach x (cdr L) ; L - assoc list of (#)
(if (eq :vlax-false (vlax-invoke-method PD 'InsertPages (1- (vlax-invoke PD 'GetNumPages)) (car x) 0 (caddr x) :vlax-true))
(prompt (strcat "\nCannot insert pages from \"" (cadr x) "\""))
)
)
(or (eq :vlax-true (vlax-invoke-method PD 'Save 1 out)) ; 1 = PDSaveFull
(prompt "\nCannot save the modified document")
); or
); progn
(setq r (mapcar 'cdr L))
); and
(*error* nil) r
); defun MergePDFs
再次非常感谢你的帮助,我会尝试调整我的眼睛更多的VBA编码,尽管在编写LISP时仍然更舒服。
页:
1
[2]