Xtractor 2015

Extract text from PDF

' Copyright (c) 2017 ActivePDF, Inc.
' ActivePDF Xtractor 2015
' Example generated 11/22/17 

Imports System

' Make sure to add the ActivePDF product .NET DLL(s) to your application.
' .NET DLL(s) are typically found in the products 'bin' folder.

Public Class Examples
  Sub Example()
    Dim strPath As String, results As XDK.Results.XtractorResult, ExtractedText As XDK.Results.TextResult

    strPath = AppDomain.CurrentDomain.BaseDirectory

    ' Instantiate Object
    Dim oXT As APXtractor.Xtractor = New APXtractor.Xtractor()
    
    ' Open a PDF to work with it
    ' Use OpenPDFData to open a PDF byte array
    results = oXT.OpenPDF(strPath & "DOC.pdf")
    If results.XtractorStatus <> XDK.Results.XtractorStatus.Success Then
      ErrorHandler("OpenPDF", results, results.XtractorStatus.ToString())
    Else
      ' Extract text from all pages to a file
      oXT.ExtractTextToFile(strPath & "xt-allpages.txt")
      
      ' Extract the text from a specific page to a file
      oXT.ExtractTextToFile(strPath & "xt-page2.txt", 2)
      
      ' GetTextLocation
      ExtractedText = oXT.GetTextByLocation(4, 100, 400, 200)
      WriteResults(ExtractedText.Text)
      
      ' GetTextLeft
      ExtractedText = oXT.GetTextLeft("Praesent", 3, 1, 10, APXtractor.Xtractor.UnitType.Character)
      WriteResults(ExtractedText.Text)
      
      ' GetTextRight
      ExtractedText = oXT.GetTextRight("Praesent", 3, 1, 10, APXtractor.Xtractor.UnitType.Character)
      WriteResults(ExtractedText.Text)
      
      ' Close the PDF when finished with it
      results = oXT.ClosePDF()
      If results.XtractorStatus <> XDK.Results.XtractorStatus.Success Then
        ErrorHandler("ClosePDF", results, results.XtractorStatus.ToString())
      End If
    End If
    
    ' Release Object
    oXT = Nothing
    
    ' Process Complete
    WriteResults("Done!")
  End Sub
  
  ' Error Handling
  Sub ErrorHandler(ByVal strMethod As String, ByVal results As ADK.Results.Result, ByVal errorStatus As String)
    WriteResults("Error with " + strMethod)
    WriteResults(errorStatus)
    WriteResults(results.Details)
    If results.Origin.Function <> strMethod Then
      WriteResults(results.Origin.Class + "." + results.Origin.Function)
    End If
    If Not results.ResultException Is Nothing Then
      ' To view the stack trace on an exception uncomment the line below
      'WriteResults(results.ResultException.StackTrace)
    End If
    Environment.Exit(1)
  End Sub
  
  ' Write output data
  Sub WriteResults(content As String)
    ' Choose where to write out results
  
    ' Debug output
    'System.Diagnostics.Debug.WriteLine("ActivePDF: * " + content)
  
    ' Console
    Console.WriteLine(content)
  
    ' Log file
    'Using tw = New System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory & "application.log", True)
    '   tw.WriteLine("[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "]: => " + content)
    'End Using
  End Sub
End Class

Copyright © 2017 ActivePDF, Inc