You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET WinForms实现PDF文字高亮遇问题求助

VB WinForms PDF文字高亮功能问题修复指导

问题概述

  • 首次点击按钮生成的PDF,高亮仅显示在左下角
  • 多次点击后程序无响应,抛出Cannot create ActiveX component错误(关联代码gApp = CreateObject("AcroExch.App"))

原代码

Public Class Form1
Dim gApp As Acrobat.CAcroApp

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    gApp = CreateObject("AcroExch.App")
End Sub

Private Sub Form1_Closed(Cancel As Integer)
    If Not gApp Is Nothing Then
        gApp.Exit()
    End If
    gApp = Nothing
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim pdDoc As Acrobat.CAcroPDDoc
    Dim page As Acrobat.CAcroPDPage
    Dim jso As Object
    Dim path As String
    Dim point(1) As Integer
    Dim popupRect(3) As Integer
    Dim annot As Object
    Dim props As Object
    
    Dim no_of_pages As Integer
    Dim count As Integer
    Dim word As String
    Dim quads As System.Object
    Dim pageRect As Object
    Dim i, j, k As Integer

    
    pdDoc = CreateObject("AcroExch.PDDoc")

    Using OpenFileDialog1 As New OpenFileDialog()
        OpenFileDialog1.ShowDialog()

        path = OpenFileDialog1.FileName
        Debug.WriteLine(path)
        If pdDoc.Open(path) Then

            
            jso = pdDoc.GetJSObject
            If Not jso Is Nothing Then

                page = pdDoc.AcquirePage(0)
                pageRect = page.GetSize
                Debug.WriteLine(String.Format("pageRect {0}", pageRect.y))
                no_of_pages = pdDoc.GetNumPages
                For j = 0 To no_of_pages - 1
                    count = jso.getPageNumWords(j)
                    For i = 0 To count - 1
                        word = jso.getPageNthWord(j, i)
                        Debug.WriteLine(word)
                        If LCase(word) = "hello" Then
                            quads = (jso.getPageNthWordQuads(j, i))
                            Debug.WriteLine(quads.Length)
                            
                            popupRect(0) = quads(0)(0)
                            popupRect(1) = quads(0)(5)
                            popupRect(2) = quads(0)(2) - quads(0)(0)
                            popupRect(3) = quads(0)(1) - quads(0)(5)
                            
                        End If
                    Next
                Next

                annot = jso.AddAnnot
                props = annot.getProps

                props.Type = "Highlight"
                annot.setProps(props)

                props = annot.getProps
                props.page = 0
               
                props.popupRect = popupRect
                props.author = "John Doe"
               
                props.strokeColor = jso.Color.Yellow
                props.Contents = "I added this comment from Visual Basic!"
                annot.setProps(props)

            End If

            pdDoc.Close()
            MsgBox("Annotation added to " & path)

        Else
            MsgBox("Failed to open " & path)
        End If
        pdDoc = Nothing
    End Using
    gApp = Nothing
End Sub
End Class

问题分析与修复方案

问题1:高亮位置错误修复

  • 核心原因:未将目标单词的坐标quads赋值给高亮注释,反而错误使用popupRect(注释弹窗位置)定位高亮区域;且仅在循环外创建一个注释,无法处理多个匹配单词。
  • 修复步骤:
    1. 将创建高亮注释的逻辑移到找到目标单词的循环内部,每匹配一个"hello"就创建对应高亮。
    2. 设置注释的quads属性为目标单词的坐标,替代错误的popupRect定位逻辑。

问题2:ActiveX组件创建失败修复

  • 核心原因:Form1_Load事件错误绑定到Button1.Click,导致每次点击重复创建gApp实例;Button1_Click末尾错误销毁gApp,造成资源泄漏和实例创建失败。
  • 修复步骤:
    1. 修正Form1_Load的事件绑定为窗体加载事件,仅初始化一次gApp。
    2. 删除Button1_Click中gApp = Nothing的代码,仅在窗体关闭时释放gApp。
    3. 确保CAcroPDDoc实例正确关闭并置空,避免进程残留。

修复后完整代码

Public Class Form1
    Dim gApp As Acrobat.CAcroApp

    ' 窗体加载时初始化AcroApp实例,仅执行一次
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gApp = CreateObject("AcroExch.App")
    End Sub

    ' 窗体关闭时释放AcroApp资源
    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        If Not gApp Is Nothing Then
            gApp.Exit()
            gApp = Nothing
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pdDoc As Acrobat.CAcroPDDoc = Nothing
        Dim jso As Object = Nothing
        Dim path As String = String.Empty

        Using OpenFileDialog1 As New OpenFileDialog()
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                path = OpenFileDialog1.FileName
                Debug.WriteLine(path)

                pdDoc = CreateObject("AcroExch.PDDoc")
                If pdDoc.Open(path) Then
                    jso = pdDoc.GetJSObject
                    If Not jso Is Nothing Then
                        Dim no_of_pages As Integer = pdDoc.GetNumPages
                        For pageIndex = 0 To no_of_pages - 1
                            Dim wordCount As Integer = jso.getPageNumWords(pageIndex)
                            For wordIndex = 0 To wordCount - 1
                                Dim word As String = jso.getPageNthWord(pageIndex, wordIndex)
                                Debug.WriteLine(word)
                                If LCase(word) = "hello" Then
                                    ' 获取目标单词的四元组坐标
                                    Dim quads As Object = jso.getPageNthWordQuads(pageIndex, wordIndex)
                                    If quads IsNot Nothing AndAlso quads.Length > 0 Then
                                        ' 创建高亮注释
                                        Dim annot As Object = jso.AddAnnot
                                        Dim props As Object = annot.getProps

                                        ' 设置注释类型为高亮
                                        props.Type = "Highlight"
                                        ' 设置注释所在页码
                                        props.page = pageIndex
                                        ' 设置高亮区域的坐标
                                        props.quads = quads
                                        ' 设置注释作者和内容
                                        props.author = "John Doe"
                                        props.strokeColor = jso.Color.Yellow
                                        props.Contents = "I added this comment from Visual Basic!"

                                        annot.setProps(props)
                                    End If
                                End If
                            Next
                        Next
                        ' 保存修改(原代码未保存,需添加此步骤)
                        pdDoc.Save(PDSaveMode.PDSaveFull, path)
                        MsgBox("Annotation added to " & path)
                    Else
                        MsgBox("Failed to get JSObject from PDF document.")
                    End If
                    pdDoc.Close()
                Else
                    MsgBox("Failed to open " & path)
                End If
            End If
        End Using

        ' 释放资源
        pdDoc = Nothing
        jso = Nothing
    End Sub
End Class

额外注意事项

  • 确保已正确引用Adobe Acrobat的COM组件(在项目引用中添加"Adobe Acrobat xx.0 Type Library")。
  • 运行程序时需要安装对应的Adobe Acrobat完整版(Reader版本不支持ActiveX操作)。

内容的提问来源于stack exchange,提问作者hts123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 07:15:00