尝试使用flattenPages()展平PDF但没有任何反应

时间:2016-12-29 00:38:22

标签: javascript vba pdf acrobat acrobat-sdk

我尝试使用以下代码从VBA调用Acrobat Javascript API以展平文档中的所有注释:

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时,仍然可以编辑所有注释 - 扁平化应该使它们成为只读,对吗?

编辑:我从" 1"更改了AcroDoc.Save的第一个参数to" PDSaveFull",如果我运行两次脚本,现在注释会变平。为什么他们第一次没有变平?

更新

我修改了脚本以获取页数,并根据joelgaraci的建议将其传递给flattenPages(),并将PDF路径传递给函数:

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

但是这得到了相同的结果:在我运行脚本两次后,注释只会变平。

1 个答案:

答案 0 :(得分:1)

以为我会添加我的解决方案,以防有人帮助... 我想要压缩文件夹中的所有PDF文件,这似乎可以解决问题。

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的问题。