public IOcrPage CreatePage(
RasterImage image,
OcrImageSharingMode sharingMode
)
image
Source raster image. This parameter cannot be null.
sharingMode
Options to determine the ownership of image.
An IOcrPage ready to be used.
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's 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. |
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.
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:\LEADTOOLS23\Resources\Images";
public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime";
}