Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Optical Character Recognition (OCR) extracts text and layout information from document images. With the help of Microsoft Office Document Imaging Library (MODI), which is contained in the Office 2003 package, you can easily integrate OCR functionality into your own applications. In combination with the MODI Document Viewer control, you will have complete OCR support with only a few lines of code.

Important note: MS Office XP does not contain MODI, MS Office 2003 is required!

Getting Started

Adding the Library

First of all, you need to add the library's reference to your project: Microsoft Office Document Imaging 11.0 Type Library (located in MDIVWCTL.DLL).

Create a Document Instance and Assign an Image File

Supported image formats are TIFF, multi-page TIFF, and BMP.

_MODIDocument = new MODI.Document(); 
_MODIDocument.Create(filename);

Call the OCR Method

The OCR process is started by the MODIDocument.OCR method.

// The MODI call for OCR 

_MODIDocument.OCR(_MODIParameters.Language, 
                  _MODIParameters.WithAutoRotation, 
                  _MODIParameters.WithStraightenImage);

With the Document.OCR call, all the contained pages of the document are processed. You can also call the OCR method for each page separately, by calling the MODIImage.OCR method in the very same way. As you can see, the OCR method has three parameters:

The use of these parameters depend on your specific imaging scenario.

Screenshot - modiSettings.JPG

Tracking the OCR Progress

Since the whole recognition process can take a few seconds, you may want to keep an eye on the progress. Therefore, the OnOCRProgress event can be used.

// add event handler for progress visualisation

_MODIDocument.OnOCRProgress += 
  new MODI._IDocumentEvents_OnOCRProgressEventHandler(this.ShowProgress);
public void ShowProgress(int progress, ref bool cancel)
{
    statusBar1.Text = progress.ToString() + "% processed.";
}

The Document Viewer

Together with the MODI document model comes the MODI viewer component AxMODI.AxMiDocView. The viewer is contained in the same library as the document model (MDIVWCTL.DLL). With a single statement, you can assign the document to the viewer. The viewer offers you many operations like selection, pan etc..

axMiDocView1.Document = _MODIDocument;

To make the component available in Visual Studio, just go to the Toolbox Explorer, open the context menu, select Add/Delete Elements.., and choose the COM Controls tab. Then, search for Microsoft Office Document Imaging Viewer 11.0, and enable it.

Processing the Recognition Result

Working on the result structure is pretty straightforward. If you just want to use the full text, you simply need the image's Layout.Text property. As an example for further processing, here is a little statistic method:

private void Statistic()
{    
    // iterating through the document's structure doing some statistics.

    string statistic = "";
    for (int i = 0 ; i < _MODIDocument.Images.Count; i++)
    {
        int numOfCharacters = 0;
        int charactersHeights = 0;
        MODI.Image image = (MODI.Image)_MODIDocument.Images[i];
        MODI.Layout layout = image.Layout;
        // getting the page's words

        for (int j= 0; j< layout.Words.Count; j++)
        {
            MODI.Word word = (MODI.Word) layout.Words[j];
            // getting the word's characters

            for (int k = 0; k < word.Rects.Count; k++)
            {
                MODI.MiRect rect = (MODI.MiRect) word.Rects[k];
                charactersHeights  += rect.Bottom-rect.Top;
                numOfCharacters++;                        
            }
        }
        float avHeight = (float )charactersHeights/numOfCharacters;
        statistic += "Page "+i+ ": Avarage character height is: "+
                         "avHeight.ToString("0.00") +" pixel!"+ "\r\n";
    }
    MessageBox.Show("Document Statistic:\r\n"+statistic);
}

Searching

MODI also offers a full featured built-in search. Since a document may contain several pages, you can use the search method to browse through the pages.

Screenshot - modiSearch.JPG

MODI offers several arguments to customize your search.

// convert our search dialog properties to corresponding MODI arguments

object PageNum = _DialogSearch.Properties.PageNum;
object WordIndex = _DialogSearch.Properties.WordIndex;
object StartAfterIndex = _DialogSearch.Properties.StartAfterIndex;
object Backward = _DialogSearch.Properties.Backward;
bool MatchMinus = _DialogSearch.Properties.MatchMinus;
bool MatchFullHalfWidthForm = _DialogSearch.Properties.MatchFullHalfWidthForm;
bool MatchHiraganaKatakana = _DialogSearch.Properties.MatchHiraganaKatakana;
bool IgnoreSpace =_DialogSearch.Properties.IgnoreSpace;

To use the search function, you need to create an instance of the type MiDocSearchClass, where all search arguments take place:

// initialize MODI search

MODI.MiDocSearchClass search = new MODI.MiDocSearchClass();
search.Initialize(
    _MODIDocument,
    _DialogSearch.Properties.Pattern,
    ref PageNum,
    ref WordIndex,
    ref StartAfterIndex,
    ref Backward,
    MatchMinus,
    MatchFullHalfWidthForm,
    MatchHiraganaKatakana,
    IgnoreSpace);

After the initialization call of the search instance, the process call itself is simple:

MODI.IMiSelectableItem SelectableItem = null;
// the one and only search call

search.Search(null,ref SelectableItem);

You will find the search results in the referenced SelectableItem argument. The MODI search has impressive features, and works very well. Sure, it is restricted to search for plain text. In most real world applications, you will need some kind of fuzzy searching since your text results may be corrupted by single OCR errors. But for a few lines of integration code, it is an impressive functionality.

MODI, Office 2007 and Vista

Good news: Office 2007 and Vista, both support MODI! It's not installed by default, but you can easily add the package via installing options of your Office 2007. You just need to rerun the setup.exe (of your Office installation) again and choose the package as in the screenshot below.

Screenshot - modi_vista.jpg

About Document Processing

OCR is only one step in document processing. To get a more qualified access to your paper based document information, usually a couple steps and techniques are required:

Scanning

Before documents are available as images, they have to be digitalized. This process is called 'scanning.' There are two important standards used for interacting with the scanning hardware: TWAIN and WIA. There are (at least) two good articles in CodeProject on how to use these APIs.

Image Processing

Although the scanning devices are getting better, a couple of methods can be used to increase the image quality. These pre-processing functions include noise reduction and angle correction, for instance.

OCR Itself

As a next step, OCR itself interprets pixel-based images to layout and text elements. OCR can be called the 'highest' bottom up technology, where the system has no or only little knowledge about the business context. Recognizing hand written documents is often called ICR (intelligent Character Recognition).

Document Classification

In most business cases, you have certain target structures you want to fill with the document information. That is called 'Document Classification and Detail Extraction.' For instance, you might want to process invoices, or you have certain table structures to fill. In Document Processing Part II, you can see how this kind of content knowledge can be used.

Beyond

After that, you might have an address database you want to match the document addresses with. Due to 'noisy' environments or disordered information, you need more sophisticated techniques than simple SQL. In the last step, the extracted information is given to the client application (like an ERP backbone) where customized workflow activities are triggered. The sector creates new names for that every couple of months: ECM (Enterprise Content Management), DMS (Document Management System), IDP (Intelligent Document Processing), (DLC) Document Life Cycle.

References

Versions

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionException HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)
tirex2009
6:55 29 Jan '10  
Hello!
I have a problem .
throw Exception HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG) in this line

((System.ComponentModel.ISupportInitialize)(this.axMiDocView1)).EndInit();



I have Windows 7 and Office 2007. Help me pleace.
QuestionMODI 2003 and 2007
lmontanez
23:15 29 Nov '09  
Is it possible avoid the incompatibility problems with MODI 2003 and 2007?
I mean, I am developing a small tool using MODI 2003.
But when I install the tool in a computer with MS 2007, it doesn't work, because the version are differents, how can avoid that, I mean, it works with ms 2003 and 2007

thanks a lot
GeneralMODI
yerroju
19:10 18 Nov '09  
Can we use MODI.dll without office installed.Is there any way ?If so pleases susggest.

Thankyou
GeneralRe: MODI
windrago
14:47 2 Dec '09  
the EULA says that you can't. Legally.

amok

GeneralSecurity Patch
PatilVL
22:11 11 Nov '09  
Thanks for the infornmation, security patch (KB973507)that prevents loading com component in vb toolbox.
But if i remove the said patch, and use the MODI component, should the user of the software package should also delete the security patch?
GeneralMS Security patch KB973507 blocks MODI Viever DLL in COM enviroment
olaf rappe
1:47 20 Aug '09  
Hello,

we are developing applications using the preview/ocr functionality of MODI in VBA (MSAccess /VB6 enviroment) and have started to redesign these applications using VB dotnet.
Now we were informed by our customers that our application no longer works. We discovered that the MODI viewer control that was placed on a access form generates a runtime error: 'this control doesn't contain an Automation object'. It seemed that the control was no longer registered. But a reregistering with regsvr32 did not help.
When we tested a simple VB6 form with the viewer control on it we got the same result - runtime error.
I discovered that the problem was caused by MS Security Update KB973507 which provides a new atl.dll.
Workstations which received this security patch via autoupdate failed to run our application.

The only workaround we found was to uninstall the security update. The problem was immediately solved.

First tests in DOTNET endviroment show that the control seems to work there even with Security Update KB973507 installed.

So if you see strange behavior of your MODI application maybe you should have to uninstall KB973507 security patch.

Regards

Olaf Rappe
Visibelle IT Services
www.visibelle.de
GeneralRe: MS Security patch KB973507 blocks MODI Viever DLL in COM enviroment
PatilVL
22:14 11 Nov '09  
Thanks for the infornmation, security patch (KB973507)that prevents loading com component in vb toolbox.
But if i remove the said patch, and use the MODI component, should the user of the software package should also delete the security patch
GeneralRe: MS Security patch KB973507 blocks MODI Viever DLL in COM enviroment
olaf rappe
0:03 23 Nov '09  
The security patch has to be uninstalled on each client on which MODI is intended to run.
For a possible workaround and for the announcement of a hotfix for the security patch see:
http://social.msdn.microsoft.com/Forums/en-US/securelm/thread/4a33bf3e-096d-48bc-b716-b58c011faf58

Also note that MODI is deprecated for Office2010 16 and 32 bit and is already missing in Office2010 beta:
http://bhandler.spaces.live.com/Blog/cns!70F64BC910C9F7F3!7040.entry

Regards

Olaf Rappe
Visibelle IT Services
GeneralMore generic version...
jarek.lukaszewicz
8:15 19 Aug '09  
Hi!

I'd like to thank Martin for writing such a good article on MODI. There are currently two MS Office versions which provide MODI components and in my organization we use both msoffice versions. So I started to wondering if is it possible to write a generic version of this application which would "find" - using some kind of magic - MODI v11(MS office 2003) or v12 office 2007, without fixing it in project/solution code? What I need to do to get it work? Any help would be appreciated...

Regards,
Jarek
AnswerRe: More generic version...
ondrejb
23:25 26 Aug '09  
This should work:
As the reference is checked at the time it is used, you may create two custom libraries, one referencing v11 and the other referencing v12. These libraries would just provide a wrapper for the OCR functions; the first use of each of the library should be inside a try-catch block to detect whether it works (if not, you will just switch to use the other library).

An easier solution would be distributing the MODI v11 library with your app (this works), but there might be an issue with the license (you must insure that the app won't run on a machine without Office).
GeneralDemo DocumentProcessing1 doesn't give consistent results
eljainc
8:22 7 May '09  
Hello,

I have tried the DocumentProcessing1 demo from CodeProject. I have tried with some bitmaps that have the same general information in them. However sometimes when I try to define a region of interest that surrounds the characters, it fails to recognize anything.

Is there any setting that can be changed so that the OCR works more reliably. I wanted to post a couple of samples but there is not an option to add attachments to the posting.

Thanks
Mike
QuestionError Releasing COM object & RPC_E_SERVERFAULT
foxbat_vv
20:38 1 Apr '09  
Hi,

I am developing an automated application that uses Microsoft Office Document
Imaging (MODI) to perform OCR on a large number of TIFF files.

In the code, I have a loop that goes through all the tiff files that need to
be processed. On each run of the loop I call a function (see below) which
creates a new MODI.Document COM object to OCR the TIFF File. At the end of
the function I release the COM object.

Occasionally the automated application encounters one of the following issues:

1.Sometimes the TIFF file is not released by the OCR process, thus it can’t
be moved or deleted until the application closes.

2.When performing OCR on an image object, sometimes a RPC_E_SERVERFAULT
(code: -2147417851) exception is thrown.

When the application performs OCR on 60 TIFF files, usually 1 random file
will not be released when Marshal.FinalReleaseComObject is called.


How can I modify the code so that the errors mentioned above do not occur?

private void SimpleOCR(FileInfo objFileName)
{
try
{
//SetImage
m_objMODIDocument = new MODI.Document();
m_objMODIDocument.Create(objFileName.FullName);

try //Perform OCR
{
for (int i = 0; i &lt; m_objMODIDocument.Images.Count; ++i)
{
MODI.IImage objImage = null;
try
{
objImage =
(MODI.IImage)(m_objMODIDocument.Images[i]);
objImage.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,
true, true);
}
catch
{
LogFile.WriteLine("Cannot OCR page " + (i + 1));
}
finally
{

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objImage);
objImage = null;
}
}

LogFile.WriteLine(objFileName.FullName + ", OCR OK! ");
}
catch (Exception ex)
{
LogFile.WriteLine(objFileName.FullName + ", OCR FAIL! EX
= " + ex.Message);
}

m_objMODIDocument.Save(); //(This causes the object not to
get relased some times)
System.Threading.Thread.Sleep(1000);

m_objMODIDocument.Close(false);

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objMODIDocument);

for (int i = 0; i &lt; 5; ++i)
{
if (move(objFileName))
break;
else
{
System.Threading.Thread.Sleep(1000); // if cannot
move the document sleep and try again

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objMODIDocument);
GC.Collect();
GC.WaitForPendingFinalizers();
}
if (i == 4)
{
LogFile.WriteLine("The file was not released.");
}
}

m_objMODIDocument = null;
}
catch (Exception ex)
{
LogFile.WriteLine("The file was skipped. EX: " + ex.Message);
}


}


public bool move(FileInfo objFileName)
{
try //DeleteMoveFiles(objFileName, true);
{
System.IO.File.Move(objFileName.FullName,
DocScan.Properties.Settings.Default.OCRMoveTifsFolder + "\\" +
objFileName.Name);
LogFile.WriteLine(objFileName.FullName + " was successfully
moved");
return true;
}
catch (Exception ex)
{
LogFile.WriteLine("*** WARNING: Could not Move or Delete
file. (" + ex.Message + ") ");
return false;
}
}


Thanks,

Vagram
AnswerRe: Error Releasing COM object & RPC_E_SERVERFAULT
foxbat_vv
21:34 14 Apr '09  
After some further investigation i had found that another component (not mentioned here) was accessing the TIFF files. The controls behavior caused the errors that were described.
GeneralRe: Error Releasing COM object & RPC_E_SERVERFAULT
hero82
6:43 19 Aug '09  
Hi,

I have the same issue. Randomly, a RPC_E_SERVERFAULT (code: -2147417851) exception is thrown.

I already looked up if another process has a handle on the file, but there is none.

Has anyone experienced the same problem? (I use VS2008 and MODI12.0 on Win XP SP3)

Many thanks
Hendrik
GeneralRe: Error Releasing COM object & RPC_E_SERVERFAULT
ondrejb
23:04 26 Aug '09  
I've solved this by ensuring to properly release all references to COM objects:

Dim doc As MODI.Document = Nothing
Dim layout As MODI.ILayout = Nothing
Dim page As MODI.IImage = Nothing
Dim pages As MODI.IImages = Nothing

doc = New MODI.Document
doc.Create(TiffFile)

pages = doc.Images
For x = 0 To pages.Count - 1

page = DirectCast(pages(x), MODI.IImage)
page.OCR()

layout = page.Layout
' work with the layout.Text property
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(layout)

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(page)

Next

doc.Close()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(pages)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc)

Generalcharacter recognition
chandupatel
0:34 6 Feb '09  
hello...

We would like to carry on our project regarding the character recognition. Could anybody get me the concerned details of techniques used for character recognition...plz.....
QuestionMODI 2007 better than MODI 2003 for OCR??
ChadFolden1
12:43 29 Oct '08  
Anyone have any insight as to if MODI 2007 is any better than MODI 2003 for OCR? From what I can tell it almost seems like they are the same, but curious if anyone has any thoughts as to whether or not it's worth upgrading or not
AnswerRe: MODI 2007 better than MODI 2003 for OCR??
Sike Mullivan
15:21 20 Feb '09  
MODI 2007 is the exact same thing as MODI 2003 with the exception of the version.
GeneralIs it possible to OCR read only a zone of the picture??
andredani
8:30 16 Oct '08  
Hi all!
Is it possible to only read for example in the right corner of the picture?
I´m trying to make a pattern of words. Or if MODI can tell me where he founded that word in pixel height and windht of the picture?

Waiting for awnser!!

Take care // André
GeneralRe: Is it possible to OCR read only a zone of the picture??
Sike Mullivan
15:20 20 Feb '09  
Yes, MODI provides you with a rectangle object that describes it's location.
QuestionCan I deal with the text OCRed?
shkirin
17:39 10 Sep '08  
Excuse me, can I deal with the OCRed text one by one according to this program.
Actually I want translate each piece of word recognized from English to Chinese, that is one part of my task
Can you help me with this problem?
Thank you!

It's me, Frank

modified on Wednesday, September 10, 2008 10:51 PM

GeneralHow do i get all the OCR reading to a txt file, without marking the picture?
andredani
22:26 20 Jul '08  
My question are after i started search for OCR at a picture, i have to select a text in the picture to copy it to clipboard.
I want it to be atomatic, by using streamwriter right after it has done the OCR reading, it will save all the text it founds in the picture in a file.

Please help me with this!!

//Thanks!!
GeneralRe: How do i get all the OCR reading to a txt file, without marking the picture?
amgadhs
3:59 13 Aug '08  
I used this code to display the text in a textbox, you could use it to write the text to a file:

//loop thru the pages, and desplaying the text in each
for (int i = 0; i < modiDoc.Images.Count; i++)
{
MODI.Image image = (MODI.Image)modiDoc.Images[i];
MODI.Layout layout = (MODI.Layout)image.Layout;

destinationTextBox.Text += image.Layout.Text;
}


GeneralNamespace name 'AxMODI' could not be found
KBM73
20:32 25 May '08  
HI,

I am using VS2005 for writing program for OCR. I have included the "Microsoft Office Document Imaging 12.0 Type Library" into reference. But while running the project I am getting Error "Namespace name 'AxMODI' could not be found".I seen solution for this in which I got a solution to add "MDIVWCTL.DLL" through command prompt, but that is still not working for me.
Can anybody have any idea to install the same ( I also tried to register it through regsvr32.exe but it is also not working)

With Regards,
KBM
GeneralNamespace name 'AxMODI' could not be found
KBM73
20:26 25 May '08  
HI,

I am using VS2005 and getting error while runing the project "Namespace name 'AxMODI' could not be found".I tried to add component for AxModi but it was not sucessful. Can anybody have any idea to install the same ( I also tried to register it through regsvr32.exe but it is also not working)

With Regards,
KBM


Last Updated 26 Oct 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010