Click here to Skip to main content
Click here to Skip to main content

OCR with Microsoft® Office

, 26 Oct 2007 GPL3
Rate this:
Please Sign up or sign in to vote.
Coming with Microsoft Office 2003, the MODI library offers you an easy but effective way to integrate Optical Character Recognition (OCR) functionality into your own applications.


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(); 

Call the OCR Method

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

// The MODI call for OCR 

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:

  • Language
  • AutoRotation
  • StraightenImages

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;
        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);


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();
    ref PageNum,
    ref WordIndex,
    ref StartAfterIndex,
    ref Backward,

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:


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.


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.



  • 3 Apr 2007: Added Vista hints
  • 29 Sep 2006: Added search functions
  • 31 May 2005: Added references
  • 15 Apr 2005: Initial version


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Martin Welker
CEO Axonic Informationssysteme GmbH, Germany
Germany Germany

Comments and Discussions

QuestionOCR PinmemberMember 1088679029-Jul-14 4:43 
Question5+ Nice article Pinmemberkoolprasad20032-Apr-14 3:29 
Questionhelp me? PinmembermojtabaNava23-Sep-13 7:18 
QuestionCan't run yet the program: When trying to watch Form1 designer all my visual studio is crash and still can't solve the problem getting exception on axMiDocView1.FitMode = MODI.MiFITMODE.miByWindow; help please Pinmemberchocolade5-Sep-13 11:05 
Questionzonal ocr PinmemberAbdul Rahman El Habboub14-Jun-13 10:39 
Questionaxmodi reference PinmemberToN.FiER12-Dec-12 22:23 
Questionwin 7 and office 2010 Pinmemberkiquenet.com4-Oct-12 5:06 
AnswerRe: win 7 and office 2010 Pinmemberhedgehoginahaze7-Nov-12 22:38 
QuestionAttempted to read or write protected memory. This is often an indication that other memory is corrupt. PinmemberVitaliy.NET26-Jul-12 2:51 
QuestionC# SImple Sample PinmemberZamirF22-Sep-11 9:16 
Questionunable to load in win7 64bit PinmemberMember 41805504-Sep-11 4:19 
AnswerRe: unable to load in win7 64bit Pinmemberhedgehoginahaze7-Nov-12 22:36 
Questionwindows 7 unable to load Interop.modi.dll PinmemberMember 418055023-Aug-11 5:44 
AnswerRe: windows 7 unable to load Interop.modi.dll Pinmembersefarkas25-Aug-11 8:57 
GeneralRe: windows 7 unable to load Interop.modi.dll PinmemberMember 418055026-Aug-11 4:48 
GeneralRe: windows 7 unable to load Interop.modi.dll Pinmemberkiquenet.com4-Oct-12 5:11 
GeneralRe: windows 7 unable to load Interop.modi.dll Pinmembernwein16-Aug-13 13:50 
QuestionCannot run in Windows 7 and Vista! Pinmembereric_klyuen1-Aug-11 23:05 
QuestionLoading images PinmemberMember 41805501-Aug-11 9:51 
QuestionConverting document to grayscale before OCR Pinmembereljainc21-Jul-11 11:07 
QuestionHi Martin [modified] Pinmembergokuldas7-Jul-11 2:39 
QuestionZonal Recognition Pinmemberdevange220-Apr-11 16:17 
AnswerRe: Zonal Recognition [modified] PinmemberAbdul Rahman El Habboub14-Jun-13 10:40 
GeneralA simple example PinmemberZamirF30-Dec-10 9:49 
QuestionHow can we perform OCR on specific page out of the multipage tiff image using MODI PinmemberMember 24401751-Nov-10 23:36 
AnswerRe: How can we perform OCR on specific page out of the multipage tiff image using MODI Pinmembersefarkas25-Aug-11 8:55 
Generalusing MODI OCR in custom application [modified] PinmemberMember 244017526-Oct-10 21:19 
QuestionHow can I send text to word, using a MODI module? PinmemberHotShot427-Sep-10 1:47 
GeneralSet X and Y rectangle coordinate location Pinmembermonkeynote18-Sep-10 10:01 
GeneralMODI Required Pinmembermmalkar3-Sep-10 3:48 
QuestionException HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG) Pinmembertirex200929-Jan-10 6:55 
I have a problem .
throw Exception HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG) in this line

I have Windows 7 and Office 2007. Help me pleace.
AnswerRe: Exception HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG) Pinmemberjgricci21-Oct-10 10:37 
GeneralRe: Exception HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG) Pinmemberjgricci21-Oct-10 11:10 
QuestionMODI 2003 and 2007 Pinmemberlmontanez29-Nov-09 23:15 
GeneralMODI Pinmemberyerroju18-Nov-09 19:10 
GeneralRe: MODI Pinmemberwindrago2-Dec-09 14:47 
GeneralSecurity Patch PinmemberPatilVL11-Nov-09 22:11 
GeneralMS Security patch KB973507 blocks MODI Viever DLL in COM enviroment Pinmemberolaf rappe20-Aug-09 1:47 
GeneralRe: MS Security patch KB973507 blocks MODI Viever DLL in COM enviroment PinmemberPatilVL11-Nov-09 22:14 
GeneralRe: MS Security patch KB973507 blocks MODI Viever DLL in COM enviroment Pinmemberolaf rappe23-Nov-09 0:03 
GeneralMore generic version... Pinmemberjarek.lukaszewicz19-Aug-09 8:15 
AnswerRe: More generic version... Pinmemberondrejb26-Aug-09 23:25 
GeneralDemo DocumentProcessing1 doesn't give consistent results Pinmembereljainc7-May-09 8:22 
QuestionError Releasing COM object & RPC_E_SERVERFAULT Pinmemberfoxbat_vv1-Apr-09 20:38 
AnswerRe: Error Releasing COM object & RPC_E_SERVERFAULT Pinmemberfoxbat_vv14-Apr-09 21:34 
GeneralRe: Error Releasing COM object & RPC_E_SERVERFAULT Pinmemberhero8219-Aug-09 6:43 
GeneralRe: Error Releasing COM object & RPC_E_SERVERFAULT Pinmemberondrejb26-Aug-09 23:04 
Generalcharacter recognition Pinmemberchandupatel6-Feb-09 0:34 
QuestionMODI 2007 better than MODI 2003 for OCR?? PinmemberChadFolden129-Oct-08 12:43 
AnswerRe: MODI 2007 better than MODI 2003 for OCR?? PinmemberSike Mullivan20-Feb-09 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150326.1 | Last Updated 26 Oct 2007
Article Copyright 2005 by Martin Welker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid