javascript - 在 Acrobat 类型库 10.0 JSObject 中使用 pageNum 属性返回 RunTime 错误 438
问题描述
我需要获取页码才能从 .PDF 文档中的特定页面中提取文本。我正在使用 Excel VBA 函数,该函数利用 Acrobat 类型库 10.0 中的 JSObject
这是代码片段,当我尝试从 Doc 对象引用 pageNum 属性时,代码会出现问题。我试图避免使用 AV 层并仅使用 PD 层,因此我的宏仅在后台运行并且不调用 Acrobat 应用程序。
Function getTextFromPDF_JS(ByVal strFilename As String) As String
Dim pdDoc As New AcroPDDoc
Dim pdfPage As Acrobat.AcroPDPage
Dim pdfBookmark As Acrobat.AcroPDBookmark
Dim jso As Object
Dim BookMarkRoot As Object
Dim vBookmark As Variant
Dim objSelection As AcroPDTextSelect
Dim objHighlight As AcroHiliteList
Dim currPage As Integer
Dim strText As String
Dim BM_flag As Boolean
Dim count As Integer
Dim word As Variant
strText = ""
If (pdDoc.Open(strFilename)) Then
Set jso = pdDoc.GetJSObject
Set BookMarkRoot = jso.BookMarkRoot
vBookmark = jso.BookMarkRoot.Children
'Add a function call to see if a particular bookmark exists within the .PDF
Set pdfBookmark = CreateObject("AcroExch.PDBookmark")
BM_flag = pdfBookmark.GetByTitle(pdDoc, "Title Page")
If (BM_flag) Then
For i = 0 To UBound(vBookmark)
If vBookmark(i).Name = "Title Page" Then
vBookmark(i).Execute
jso.pageNum
Set pdfPage = pdDoc.AcquirePage(pageNum)
Set objHighlight = New AcroHiliteList
objHighlight.Add 0, 10000 ' Adjust this up if it's not getting all the text on the page
Set objSelection = pdfPage.CreatePageHilite(objHighlight)
If Not objSelection Is Nothing Then
For tCount = 0 To objSelection.GetNumText - 1
strText = strText & objSelection.GetText(tCount)
Next tCount
End If
Exit For
End If
pdDoc.Close
End If
End If
getTextFromPDF_JS = strText
End Function
解决方案
jso.pageNum = 0; 设置页码
pageNo = jso.pageNum; 获取页码
编辑:3.3.19
嗯,看来您必须使用 AVDoc 才能通过 jso.pageNum 获取当前的实际页面。此外,如果您使用 AVdoc,Acobat 窗口将隐藏在后台。例子:
strFilename = "d:\Test2.pdf"
set avDoc = CreateObject("AcroExch.AVDoc")
If (avDoc.Open(strFilename,"")) Then
Set pdDoc = avDoc.getPDDoc()
Set jso = pdDoc.GetJSObject
pageNo = jso.pageNum
msgbox(pageNo)
end if
推荐阅读
- bootstrap-4 - 如何使这个日历插件响应
- javascript - 如何判断玩家是否距离敌人 2 个方格
- go - Terraform 自定义提供程序 - 数据源
- php - php中的多维数组(具有相同的键不同的值)如何为键创建一个新名称?
- php - 如何为 Apache 编写 .htaccess 文件以附加 PHP(如果未提供)并使用编写良好的 404 页面
- python - python正则表达式匹配点和括号
- javascript - 无法使用链接更改机器人的头像
- python - 在 PyTorch 中使用 BatchNorm 的准确率非常低
- mysql - 使用 Null 值将表连接到自身
- ios - Xcode 新更新 - 重用标识符问题