如何使用vb获取pdf上下文?

时间:2017-10-20 03:16:51

标签: vb6 acrobat

我有一个可以获取pdf文件页面编号的函数。

Public Function GetNumPages(ByVal PdfFile As String) As Long  
    Dim objTempDoc As Object  
    Dim fso As FileSystemObject  
    Set fso = New FileSystemObject  

    If fso.FileExists(PdfFile ) Then  
        Set objTemp = CreateObject("AcroExch.PDDoc")  
        objTemp.Open pstrPdfFilename  
        GetNumPages = objTemp.GetNumPages  
        objTemp.Close  
        Set objTemp = Nothing  
    End If  

    Set fso = Nothing  
End Function  

我想在pdf文件的最后一页获取最后一行的上下文。

我找到了这个API,但我不知道如何使用它。 它会返回我想要的上下文吗?

  

PDOCContext PDDocGetOCContext(PDDoc pdDoc)

我试过这种方式使用API​​,但它失败了。

Set objTempDoc = CreateObject("AcroExch.PDDoc")
objTempDoc.Open PdfFile
myPDFPage = objTempDoc.GetOCContext

1 个答案:

答案 0 :(得分:0)

可以调用此函数来获取最后一页的文本。

Public Function GetPDFText(ByVal pstrPdfFilename As String) As String

        Dim PDDoc As Object
        Dim CAcroRect As New Acrobat.AcroRect
        Dim PDPage As Acrobat.AcroPDPage
        Dim PDTxtSelect As Acrobat.AcroPDTextSelect
        Dim CArcoPoint As Acrobat.AcroPoint
        Dim iNumWords As Integer
        Dim iMax As Long
        Dim arPdfLines() As String
        Dim i As Integer
        Dim fso As FileSystemObject

        Set fso = New FileSystemObject
        If fso.FileExists(pstrPdfFilename) Then
            Set PDDoc = CreateObject("AcroExch.PDDoc")
            PDDoc.Open pstrPdfFilename
            Set PDPage = PDDoc.AcquirePage(PDDoc.GetNumPages() - 1)
            Set CArcoPoint = PDPage.GetSize()
            CAcroRect.Top = CArcoPoint.y
            CAcroRect.Left = 0
            CAcroRect.Right = CArcoPoint.x
            CAcroRect.bottom = 0
            Set PDTxtSelect = PDDoc.CreateTextSelect(PDDoc.GetNumPages() - 1, CAcroRect)
            If PDTxtSelect Is Nothing Then
                iNumWords = 0
                iMax = 0
                GetPDFLastLineText = ""
            Else
                iNumWords = PDTxtSelect.GetNumText
                iMax = iNumWords - 1
                Dim ii As Long
                For ii = 0 To iMax
                GetPDFLastLineText = GetPDFLastLineText & PDTxtSelect.GetText(ii)
            Next
        End If
        PDDoc.Close
    End If

    Set fso = Nothing
    Set PDDoc = Nothing
    Set CAcroRect = Nothing
    Set PDPage = Nothing
    Set PDTxtSelect = Nothing
    Set CArcoPoint = Nothing

End Function