←Select platform

CreatePage Method

Summary

Create an IOcrPage from a raster image.

Syntax

C#
VB
Java
Objective-C
C++

Function CreatePage( _ 
   ByVal image As RasterImage, _ 
   ByVal sharingMode As OcrImageSharingMode _ 
) As IOcrPage 

- (nullable LTOcrPage *)createPage:(LTRasterImage *)image sharingMode:(LTOcrImageSharingMode)sharingMode error:(NSError **)error 

public OcrPage createPage(RasterImage image, OcrImageSharingMode 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.Ocr; 
using Leadtools.Document.Writer; 
 
private static void CreatePageExample() 
{ 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false)) 
   { 
      // 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:\Users\Public\Documents\LEADTOOLS Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime"; 
} 

Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Ocr 
Imports Leadtools.Document.Writer 
 
Private Shared Sub CreatePageExample() 
   ' Create an instance of the engine 
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, False) 
      ' Start the engine using default parameters 
      ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrLEADRuntimeDir) 
 
      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 OcrLEADRuntimeDir As String = "C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime" 
End Class 

Requirements

Target Platforms

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

Leadtools.Ocr Assembly