←Select platform

CreatePage Method

Summary

Create an IOcrPage from a raster image.

Syntax

C#
VB
Java
Objective-C
WinRT C#
C++
- (nullable LTOcrPage *)createPage:(LTRasterImage *)image sharingMode:(LTOcrImageSharingMode)sharingMode error:(NSError **)error 
public OcrPage createPage(RasterImage image, OcrImageSharingMode sharingMode) 
function Leadtools.Forms.Ocr.IOcrEngine.CreatePage(  
   image , 
   sharingMode  
) 

Parameters

image
Source raster image. This parameter cannot be null.

sharingMode
Options to determine the ownership of image.

Return Value

An IOcrPage ready to be used.

Remarks

Use CreatePage to quickly create an IOcrPage from a RasterImage directly, call the necessary method (such as IOcrPage.Recognize) and then obtain the text directly using IOcrPage.GetText or IOcrPage.GetRecognizedCharacters.

To save the result IOcrPage to a document file such as PDF or Microsoft Word by directly, you must create an IOcrDocument object in file mode and add the page to it using IOcrDocument.Add.

The IOcrPage interface implements IDisposable. It is highly recommended that you call Dispose (or use the using statement in C# or Using statement in VB) after you finish using this IOcrPage instance.

The value of sharingMode determines what happens to the raster image object after it has been used to create the page as follows:

Option Description
OcrImageSharingMode.None

The result IOcrPage does not own the image. When IOcrPage is disposed, the image is not disposed. This mode is useful for quickly creating an OCR page (and getting the recognition result) from an image that is already used in other parts of your application. For example, the image is being viewed in the image viewer. Using this option will save memory because you do not have to create a copy of the image and the page will use the same image data when performing recognition.

Important: In this mode, it is the user responsibility to keep the raster image object alive for the whole duration the page is alive. Only dispose the raster image after the IOcrPage object is disposed and no longer used.

OcrImageSharingMode.AutoDispose

The result IOcrPage owns the image. When IOcrPage is disposed, the image is disposed as well. This mode is useful when the image is no longer used by other parts of your application. For example, the image is obtained from scanning or the camera and is only to be used for OCRing. Using this option will transfer the ownership of the image object to the page and it will be disposed when the page is disposed.

Example

This example will load a raster image from disk, OCR it to obtain the results and print it to the console. It will then add the page to a document to create a PDF file.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Forms.Ocr; 
using Leadtools.Forms.DocumentWriters; 
 
private static void CreatePageExample() 
{ 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrAdvantageRuntimeDir); 
 
      string[] tifFileNames = 
      { 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"), 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr2.tif"), 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr3.tif"), 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr4.tif") 
      }; 
 
      string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf"); 
 
      // Create an OCR document to save the result as PDF 
      // Create the document using file mode since we are not keeping the OCR pages in memory 
      using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.None)) 
      { 
         foreach (string tifFileName in tifFileNames) 
         { 
            // Load the TIFF file as image 
            RasterImage rasterImage = ocrEngine.RasterCodecsInstance.Load(tifFileName, 1); 
 
            // Create an IOcrPage instance from this image 
            using (IOcrPage ocrPage = ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose)) 
            { 
               // We told the page to dispose the image when no longer needed, so rasterImage should not be used anymore 
               rasterImage = null; 
 
               // Recognize the page 
               ocrPage.Recognize(null); 
 
               // Show the results 
               string text = ocrPage.GetText(-1); 
               Console.WriteLine("Recognition text for " + tifFileName); 
               Console.WriteLine(text); 
 
               // Add this recognized page 
               ocrDocument.Pages.Add(ocrPage); 
 
               // The method above took a snapshot of the recognition data of the page and added it to the document 
               // The page is no longer needed 
            } 
         } 
 
         // Now we can save the document 
         ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null); 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
   public const string OcrAdvantageRuntimeDir = @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Forms.Ocr 
Imports Leadtools.Forms.DocumentWriters 
 
Private Shared Sub CreatePageExample() 
   ' Create an instance of the engine 
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False) 
      ' Start the engine using default parameters 
      ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrAdvantageRuntimeDir) 
 
      Dim tifFileNames() As String = 
      { 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"), 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr2.tif"), 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr3.tif"), 
         Path.Combine(LEAD_VARS.ImagesDir, "Ocr4.tif") 
      } 
 
      Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf") 
 
      ' Create an OCR document to save the result as PDF 
      ' Create the document using file mode since we are not keeping the OCR pages in memory 
      Using ocrDocument As IOcrDocument = ocrEngine.DocumentManager.CreateDocument(Nothing, OcrCreateDocumentOptions.None) 
         For Each tifFileName As String In tifFileNames 
            ' Load the TIFF file as image 
            Dim rasterImage As RasterImage = ocrEngine.RasterCodecsInstance.Load(tifFileName, 1) 
 
            ' Create an IOcrPage instance from this image 
            Using ocrPage As IOcrPage = ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose) 
               ' We told the page to dispose the image when no longer needed, so rasterImage should not be used anymore 
               rasterImage = Nothing 
 
               ' Recognize the page 
               ocrPage.Recognize(Nothing) 
 
               ' Show the results 
               Dim text As String = ocrPage.GetText(-1) 
               Console.WriteLine("Recognition text for " + tifFileName) 
               Console.WriteLine(text) 
 
               ' Add this recognized page 
               ocrDocument.Pages.Add(ocrPage) 
 
               ' The method above took a snapshot of the recognition data of the page and added it to the document 
               ' The page is no longer needed 
            End Using 
         Next 
 
         ' Now we can save the document 
         ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, Nothing) 
      End Using 
   End Using 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
   Public Const OcrAdvantageRuntimeDir As String = "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime" 
End Class 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Forms.Ocr Assembly