0% found this document useful (1 vote)
38 views5 pages

PDF and Image OCR Processing Code

This document contains code for a program that can read text from various file types including PDFs, images, and text files. It defines functions for opening files, extracting text using iTextSharp for PDFs, MODI for images, and Tesseract for OCR. The text is displayed in a text box. Buttons trigger the different file reading methods based on the file extension. It also contains functions for converting images to base64 strings and reading files into memory streams.

Uploaded by

info.glcom5161
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
38 views5 pages

PDF and Image OCR Processing Code

This document contains code for a program that can read text from various file types including PDFs, images, and text files. It defines functions for opening files, extracting text using iTextSharp for PDFs, MODI for images, and Tesseract for OCR. The text is displayed in a text box. Buttons trigger the different file reading methods based on the file extension. It also contains functions for converting images to base64 strings and reading files into memory streams.

Uploaded by

info.glcom5161
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Imports

Imports
Imports
Imports
Imports
Imports
Imports
Imports

System
[Link]
[Link]
[Link]
[Link]
SD = [Link]
VB = [Link]
[Link]

Imports
Imports
Imports
Imports
Imports

[Link]
[Link]
[Link]
[Link]
TS = tessnet2

Imports MODI
Public Class frmReadPDF
Dim sFileName, sFileExtn As String
Public cxBasePath, cxTempPath, cxDesktopPath, cxDesktopLog, cxMyDocPath As S
tring
Private DateTime = &H132 '306
Private Sub frmReadPDF_Load(ByVal sender As Object, ByVal e As [Link]
rgs) Handles [Link]
cxDesktopPath = [Link]([Link]
[Link])
cxBasePath = [Link]
End Sub
Private Sub btnPickFile_Click(ByVal sender As [Link], ByVal e As Syst
[Link]) Handles [Link]
Dim ofd As OpenFileDialog
[Link] = ""
Try
ofd = New OpenFileDialog
[Link] = "Please Select a PDF/Image file"
[Link] = cxBasePath
[Link] = "PDF & Image files (*.pdf, *.jpg, *.jpeg, *.gif, *.tif,
*.png) | *.pdf; *.jpg; *.jpeg; *.gif; *.tif; *.png"
If [Link] = [Link] Then
Dim fi As FileInfo = New FileInfo([Link])
If [Link] > 2000000 Then
'--// Limit file size upto 2MB
MsgBox("Please choose a file of max size upto 2MB !", MsgBox
[Link] + [Link], [Link])
Exit Sub
End If
[Link] = [Link]
[Link] = [Link]([Link]([Link]
Name))
sFileExtn = Replace([Link], ".", [Link])
Select Case sFileExtn
Case "pdf"
[Link] = True
[Link] = True
[Link] = False

[Link] = False
[Link] = False
Case "bmp", "tif", "gif", "jpg", "png", "jpeg", "tiff"
[Link] = False
[Link] = False
[Link] = True
[Link] = True
[Link] = True
End Select
End If
Catch ex As Exception
MsgBox([Link])
Finally
ofd = Nothing
End Try
End Sub
Private Sub btnReadMDI_Click(ByVal sender As [Link], ByVal e As Syste
[Link]) Handles [Link]
Dim out As String = ""
Dim md As New [Link]()
Try
[Link]([Link])
[Link]([Link].miLANG_ENGLISH, True, True)
Dim image As [Link] = DirectCast([Link](0), [Link])
Dim layout As [Link] = [Link]
For j As Integer = 0 To [Link] - 1
Dim word As [Link] = DirectCast([Link](j), [Link])
out += " " & [Link]
Next
[Link] = out
Catch ex As Exception
MsgBox([Link])
Finally
md = Nothing
End Try
End Sub
Private Sub btnReadPdf_Click(ByVal sender As [Link], ByVal e As Syste
[Link]) Handles [Link]
Try
Dim sb As String = GetTextFromPDF([Link])
[Link] = [Link]
Catch ex As Exception
MsgBox([Link])
End Try
End Sub
Public Function GetTextFromPDF(ByVal PdfFileName As String) As String
Dim oReader As New [Link](PdfFileName)
Dim sOut = ""
For i = 1 To [Link]
Dim its As New [Link]
gy
sOut &= [Link](
oReader, i, its)

Next
Return sOut
End Function
Private Sub btnReadImg_Click(ByVal sender As [Link], ByVal e As Syste
[Link]) Handles [Link]
Dim sTmp As String = ""
Dim validExtns() As String = {"bmp", "tif", "gif", "jpg", "png", "jpeg",
"tiff"}
Dim result As String() = [Link](validExtns, Function(s) [Link](
sFileExtn))
If ([Link] > 0) Then
Dim img As Bitmap = New Bitmap([Link])
Dim str As String = ConvertImageToBase64String(img)
[Link] = str
End Sub
Public Shared Function OCRImage(ByVal bm As [Link], ByVal lang
uage As String, ByVal path As String) As String
OCRImage = ""
Dim oOCR As New [Link]
Try
[Link](path, language, False)
Dim WordList As New List(Of [Link])
WordList = [Link](bm, [Link])
Dim LineCount As Integer = [Link](WordList)
For i As Integer = 0 To LineCount - 1
OCRImage &= [Link](WordList, i) & vbCrLf
Next
Catch ex As Exception
MsgBox([Link])
Finally
[Link]()
End Try
End Function
Private Sub btnReadTss_Click(ByVal sender As [Link], ByVal e As Syste
[Link]) Handles [Link]
'Dim img As Bitmap = New Bitmap([Link])
'Dim gfx As Graphics = [Link](img)
'[Link](New Point([Link].X + [Link].X + 4
, [Link].Y + [Link].Y + 30), New Point(0, 0), [Link])
'[Link] = img
Dim str2 As String = ReadImageTextUsingTESS()
[Link] = str2
End Sub
Public Function ReadImageTextUsingTESS() As String
Dim tsDataPath As String = cxBasePath & "\tessdata"
Dim str As String = ""
Dim ocr As New [Link]
Try
Dim img As Bitmap = New Bitmap([Link])

'[Link](Nothing, "eng", False)


'[Link]("tessedit_char_whitelist", "0123456790")
[Link](tsDataPath, "eng", False)
'To
Dim
Dim
For

use correct tessdata


result As List(Of [Link]) = [Link](img, [Link])
iLines = [Link](result)
i As Integer = 0 To iLines - 1
str &= [Link](result, i) & vbCrLf

Next
''--// WORKING; STOPPED
''For Each word As [Link] In result
''
str &= [Link] & " "
''
[Link]("{0} : {1}", [Link], [Link])
''Next
'Dim img2 As [Link] = [Link]([Link])
'Dim WordList As New List(Of [Link])
'WordList = [Link](img2, [Link])
'Dim LineCount As Integer = [Link](WordList)
'For i As Integer = 0 To LineCount - 1
'
str &= [Link](WordList, i) & vbCrLf
'Next
Catch ex As Exception
MsgBox([Link])
Finally
If ocr IsNot Nothing Then [Link]()
ocr = Nothing
End Try
Return str
End Function
Public Function ConvertImageToBase64String(ByVal value As [Link]
ge) As String
If value Is Nothing Then Return ""
Dim szResult As String = ""
Using ms As New MemoryStream
[Link](ms, [Link])
[Link]()
[Link] = 0
Dim buffer = [Link]
'szResult = [Link](buffer)
szResult = Convert.ToBase64String(buffer)
End Using
Return szResult
End Function
Private Sub btnReadFSM_Click(ByVal sender As [Link], ByVal e As Syste
[Link]) Handles [Link]
Dim bteRead() As Byte = Nothing
Dim lngRead As Long = 0
Dim strRead As String
Try
Using fs As FileStream = [Link]([Link])
Dim ms As New MemoryStream()
[Link]([Link])

lngRead = [Link]
ReDim bteRead(lngRead)
[Link](bteRead, 0, lngRead)
strRead = [Link](bteRead)
'Dim buffer = [Link]
'strRead = Convert.ToBase64String(buffer, Base64FormattingOption
[Link])
[Link] = strRead
End Using
Catch ex As Exception
MsgBox([Link])
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As [Link], ByVal e As System.
EventArgs) Handles [Link]
[Link]()
[Link]()
End Sub
End Class

You might also like