Click here to Skip to main content
12,401,173 members (62,778 online)
Click here to Skip to main content
Add your own
alternative version

Stats

1.3M views
28.7K downloads
367 bookmarked
Posted

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.

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 theDocument.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;
                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

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

License

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

Share

About the Author

Martin Welker
CEO Axonic Informationssysteme GmbH, Germany
Germany Germany

You may also be interested in...

Comments and Discussions

 
GeneralRe: My Last Try regarding PDF 2 MDI Pin
Martin Welker15-Feb-06 0:12
memberMartin Welker15-Feb-06 0:12 
Generalhi martin need your help ..... this time its pdf integration with vb.net Pin
Xkaliber29-Jan-06 19:35
memberXkaliber29-Jan-06 19:35 
GeneralFindNext Problem - Solved : Martin thanks for nothing Pin
Xkaliber19-Jan-06 20:41
memberXkaliber19-Jan-06 20:41 
GeneralThanks Pin
Martin Welker27-Jan-06 21:15
memberMartin Welker27-Jan-06 21:15 
Generalany one ... need help on MODI search Pin
Xkaliber18-Jan-06 19:22
memberXkaliber18-Jan-06 19:22 
Generalabout MODI.Layout.words Pin
xd12518-Jan-06 15:35
memberxd12518-Jan-06 15:35 
GeneralRe: about MODI.Layout.words Pin
Martin Welker15-Feb-06 0:07
memberMartin Welker15-Feb-06 0:07 
Questionneed help on Search... here is the last try Pin
Xkaliber16-Jan-06 21:31
memberXkaliber16-Jan-06 21:31 
QuestionNext Version Release? Pin
Xkaliber15-Jan-06 19:33
memberXkaliber15-Jan-06 19:33 
QuestionIs it legal to use it in commercial applications? Pin
Late22-Dec-05 3:14
memberLate22-Dec-05 3:14 
AnswerRe: Is it legal to use it in commercial applications? Pin
Martin Welker9-Jan-06 3:39
memberMartin Welker9-Jan-06 3:39 
QuestionOCR running error Pin
forget.it1234512-Dec-05 23:27
memberforget.it1234512-Dec-05 23:27 
AnswerRe: OCR running error Pin
Shriniwasan Viswanathan31-Jan-06 15:08
memberShriniwasan Viswanathan31-Jan-06 15:08 
Questionimg.Layout.Text Throws a COM Error Pin
Moshe Plotkin1-Dec-05 17:29
memberMoshe Plotkin1-Dec-05 17:29 
B"H

I am running the example on a comupter that has office 2003 and everthi9ng goes fine until runtime when if i call document.OCR or Image.Layout.Text I get cryptic COM errors.

Does anyone know whats happening?

Thank you!

-- modified at 23:44 Saturday 3rd December, 2005
AnswerRe: img.Layout.Text Throws a COM Error Pin
Martin Welker9-Jan-06 3:38
memberMartin Welker9-Jan-06 3:38 
GeneralMODI search Pin
Pupin30-Nov-05 5:35
memberPupin30-Nov-05 5:35 
GeneralRe: MODI search Pin
Martin Welker9-Jan-06 3:38
memberMartin Welker9-Jan-06 3:38 
GeneralMODI dll reference Pin
theindescribablehunk29-Oct-05 5:53
membertheindescribablehunk29-Oct-05 5:53 
GeneralRe: MODI dll reference Pin
Martin Welker24-Nov-05 10:55
memberMartin Welker24-Nov-05 10:55 
GeneralMODI without Reference (C#) Pin
E-E-R17-Oct-05 0:10
sussE-E-R17-Oct-05 0:10 
AnswerThanks Pin
Martin Welker24-Nov-05 10:52
memberMartin Welker24-Nov-05 10:52 
QuestionHow to create an instance of MODI Pin
celiuqui22-Sep-05 5:03
memberceliuqui22-Sep-05 5:03 
AnswerRe: How to create an instance of MODI Pin
Martin Welker23-Sep-05 5:48
memberMartin Welker23-Sep-05 5:48 
GeneralRe: How to create an instance of MODI Pin
celiuqui23-Sep-05 7:07
memberceliuqui23-Sep-05 7:07 
Generaldifficulty in modi.ImiDocSearch Pin
rizwantahir14-Sep-05 21:14
memberrizwantahir14-Sep-05 21:14 
GeneralRe: difficulty in modi.ImiDocSearch Pin
Martin Welker23-Sep-05 5:50
memberMartin Welker23-Sep-05 5:50 
QuestionMODI.IMiSelectableImage.ExportToImage question Pin
GibsonM10-Sep-05 9:14
sussGibsonM10-Sep-05 9:14 
GeneralWhy it isn't work on a number image Pin
newpant8-Sep-05 4:26
sussnewpant8-Sep-05 4:26 
GeneralRe: Why it isn't work on a number image Pin
Martin Welker9-Sep-05 20:56
memberMartin Welker9-Sep-05 20:56 
QuestionMODI, but how does it work? Pin
EmailSolidale6-Sep-05 1:23
memberEmailSolidale6-Sep-05 1:23 
QuestionUsing MODI lib from c++ Pin
Anonymous5-Sep-05 16:36
sussAnonymous5-Sep-05 16:36 
AnswerRe: Using MODI lib from c++ Pin
Martin Welker8-Sep-05 4:56
memberMartin Welker8-Sep-05 4:56 
GeneralRe: Using MODI lib from c++ Pin
Bernardo Falese26-Sep-05 17:31
memberBernardo Falese26-Sep-05 17:31 
GeneralRe: Using MODI lib from c++ Pin
Martin Welker26-Sep-05 23:25
memberMartin Welker26-Sep-05 23:25 
Questionhow to use modi component without office 2003 Pin
tiaozi24-Aug-05 16:40
membertiaozi24-Aug-05 16:40 
AnswerRe: how to use modi component without office 2003 Pin
Martin Welker31-Aug-05 12:32
memberMartin Welker31-Aug-05 12:32 
GeneralObject hasn't been initialized and can't be used yet Pin
unfielusuario21-Aug-05 13:13
memberunfielusuario21-Aug-05 13:13 
GeneralRe: Object hasn't been initialized and can't be used yet Pin
Anonymous23-Aug-05 5:15
sussAnonymous23-Aug-05 5:15 
GeneralRe: Object hasn't been initialized and can't be used yet Pin
Dilip Nilanka30-Dec-10 4:30
memberDilip Nilanka30-Dec-10 4:30 
GeneralRe: Object hasn't been initialized and can't be used yet Pin
jcent8-Jan-11 0:44
memberjcent8-Jan-11 0:44 
GeneralASP.Net Pin
kieseltest12-Aug-05 6:06
memberkieseltest12-Aug-05 6:06 
GeneralRe: ASP.Net Pin
Anonymous23-Aug-05 5:16
sussAnonymous23-Aug-05 5:16 
GeneralRe: ASP.Net Pin
Anonymous1-Sep-05 7:30
sussAnonymous1-Sep-05 7:30 
GeneralRe: ASP.Net Pin
timucin_kizilay31-Jan-06 22:40
membertimucin_kizilay31-Jan-06 22:40 
Generalgot some problems ... Pin
binov17-Jul-05 14:04
memberbinov17-Jul-05 14:04 
GeneralRe: got some problems ... Pin
Anonymous17-Jul-05 23:02
sussAnonymous17-Jul-05 23:02 
GeneralCool app Pin
weizhu1-Jul-05 16:10
memberweizhu1-Jul-05 16:10 
GeneralRe: Cool app Pin
Martin Welker3-Jul-05 23:58
memberMartin Welker3-Jul-05 23:58 
GeneralForm Processing Pin
Marcello Drewanz14-Jun-05 4:18
memberMarcello Drewanz14-Jun-05 4:18 
GeneralRe: Form Processing Pin
Martin Welker14-Jun-05 12:19
memberMartin Welker14-Jun-05 12:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.160721.1 | Last Updated 26 Oct 2007
Article Copyright 2005 by Martin Welker
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid