←Select platform

CreatePage Method

Summary
Create an IOcrPage from a raster image.
Syntax
C#
Objective-C
C++/CLI
Java
Python
- (nullable LTOcrPage *)createPage:(LTRasterImage *)image sharingMode:(LTOcrImageSharingMode)sharingMode error:(NSError **)error 
public OcrPage createPage(RasterImage image, OcrImageSharingMode sharingMode) 
def CreatePage(self,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#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Document.Writer; 
 
private static void CreatePageExample() 
{ 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      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:\LEADTOOLS22\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.3.31
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Ocr Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.