尝试使用flattenPages()拼合PDF,但没有任何反应 [英] Trying to flatten PDF using flattenPages() but nothing happens
问题描述
我试图使用以下代码从VBA调用Acrobat Javascript API来展平文档中的所有注释:
I'm trying to call the Acrobat Javascript API from VBA to flatten all annotations in a document, using the following code:
Sub flattenPDF()
Dim AcroApp As Acrobat.AcroApp
Dim AcroDoc As Acrobat.AcroPDDoc
Dim jso As Object
Dim path As String
path = "C:\Users\userID\Desktop\thisfile.pdf"
Set AcroApp = CreateObject("AcroExch.App")
Set AcroDoc = CreateObject("AcroExch.PDDoc")
AcroDoc.Open path
Set jso = AcroDoc.GetJSObject
jso.flattenPages
AcroDoc.Save PDSaveFull, path
AcroDoc.Close
AcroApp.Exit
End Sub
代码成功运行,但是当我打开PDF时,所有注释仍然可以编辑-拼合应该使它们成为只读的,对吗?
The code runs successfully, but then when I open the PDF, all the annotations can still be edited--the flattening should have made them read-only, right?
我将AcroDoc.Save
的第一个参数从"1"更改为"PDSaveFull",现在,如果我运行脚本两次,注释将变平.他们为什么不第一次压扁?
I changed the first parameter of AcroDoc.Save
from "1" to "PDSaveFull", and now the annotations are flattened if I run the script twice. Why don't they flatten the first time?
更新:
我修改了脚本以获取页数,并根据joelgaraci的建议将其传递给flattenPages()
,以及将PDF路径传递给函数:
I modified the script to get the page count and pass it to flattenPages()
per joelgaraci's suggestion, as well as passing in the PDF path to the function:
Sub flattenPDF(pdfPath As String)
Dim AcroApp As Acrobat.AcroApp
Dim AcroDoc As Acrobat.AcroPDDoc
Dim pageCount As Integer
Dim jso As Object
Set AcroApp = CreateObject("AcroExch.App")
Set AcroDoc = CreateObject("AcroExch.PDDoc")
AcroDoc.Open pdfPath
pageCount = AcroDoc.GetNumPages
Set jso = AcroDoc.GetJSObject
jso.flattenPages 0, pageCount - 1
AcroDoc.Save PDSaveFull, pdfPath
AcroDoc.Close
AcroApp.Exit
End Sub
但这得到了相同的结果:注释只有在我两次运行脚本后才变平.
But this got the same result: the annotations only flatten after I run the script twice.
推荐答案
我以为我会添加解决方案,以防它对某人有所帮助... 我想将文件夹中的所有PDF文件展平,这似乎可以解决问题.
Just thought I would add my solution in case it helps someone... I wanted to flatten all PDF files in a folder and this seems to do the trick.
Sub Flatten_Folder()
Dim MyFile As String
Mypath = InputBox("Enter the path to the folder where the PDF files are
Located **MUST END WITH \**")
MyFile = Dir(Mypath)
Do While MyFile <> ""
If MyFile Like "*.PDF" Or MyFile Like "*.pdf" Then
Fullpath = Mypath & MyFile
Set App = CreateObject("AcroExch.app")
Set avdoc = CreateObject("AcroExch.AVDoc")
Set pdDoc = CreateObject("AcroExch.PDDoc")
Set AForm = CreateObject("AFormAut.App")
pdDoc.Open (Fullpath)
Set avdoc = pdDoc.OpenAVDoc(Fullpath)
js = "this.flattenPages();"
'//execute the js code
AForm.Fields.ExecuteThisJavaScript js
Set pdDoc = avdoc.GetPDDoc
pdDoc.Save PDSaveFull, Fullpath
pdDoc.Close
Set AForm = Nothing
Set avdoc = Nothing
Set App = Nothing
End If
MyFile = Dir
Loop
End Sub
在运行宏时,会出现一个消息框提示,以粘贴文件夹路径. 同样,这种方法似乎可以避免OP遇到的问题.
On running the macro you get a message box prompt to paste the folder path in. Also this method seems to avoid the issue the OP was having.
这篇关于尝试使用flattenPages()拼合PDF,但没有任何反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!