Search code examples
vb6acrobat

How can I use vb to get pdf context?


I have a function that can get the page's number of the pdf file.

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  

I want to get the last line's context in last page of pdf file.

I have found this API, but I don't know how to use it. Will it return the context that I want?

PDOCContext PDDocGetOCContext(PDDoc pdDoc)

I tried this way to use API, but it was fail.

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

Solution

  • Can call this Function to get the text of last page.

    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