gpt4 book ai didi

javascript - 尝试使用 flattenPages() 拼合 PDF 但没有任何反应

转载 作者:太空宇宙 更新时间:2023-11-04 16:12:12 27 4
gpt4 key购买 nike

我尝试使用以下代码从 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 时,所有注释仍然可以编辑 - 扁平化应该使它们变为只读,对吗?

编辑:我将 AcroDoc.Save 的第一个参数从“1”更改为“PDSaveFull”,现在如果我运行脚本两次,注释就会变平。为什么它们第一次不压平?

更新:

我修改了脚本以获取页数并将其传递给 flattenPages() 根据 joelgaraci 的建议,以及将 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

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

最佳答案

只是想我会添加我的解决方案,以防它对某人有帮助......我想将所有 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遇到的问题。

关于javascript - 尝试使用 flattenPages() 拼合 PDF 但没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41371693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com