Clear all pages from the OCR document.
#include "ltocr.h"
L_LTOCR_API L_INT EXT_FUNCTION L_OcrDocument_ClearPages(document)
Handle to the OCR document.
Value | Meaning |
---|---|
SUCCESS | The function was successful. |
< 1 | An error occurred. Refer to Return Codes. |
Clear all pages from the OCR document.
Note: This method only works in memory-based documents.
static L_INT CreateMultiPageFile(L_TCHAR* outputFileName)
{
// Create a multi-page TIF from Ocr1.tif, Ocr2.tif, Ocr3.tif and Ocr4.tif
HBITMAPLIST bitmapList;
L_INT retCode = -1;
SAVEFILEOPTION saveOptions = {0};
retCode = L_CreateBitmapList(&bitmapList);
if(retCode != SUCCESS)
return retCode;
BITMAPHANDLE bitmaps[4];
L_TCHAR *imageFiles[] = {MAKE_IMAGE_PATH(L_TEXT("OCR1.tif")),
MAKE_IMAGE_PATH(L_TEXT("OCR2.tif")),
MAKE_IMAGE_PATH(L_TEXT("OCR3.tif")),
MAKE_IMAGE_PATH(L_TEXT("OCR4.tif"))};
for(L_INT i = 0; i < _countof(imageFiles); i++)
{
retCode = L_LoadBitmap(imageFiles[i], &bitmaps[i], sizeof(BITMAPHANDLE), 0, ORDER_RGB, NULL, NULL);
if(retCode != SUCCESS)
return retCode;
retCode = L_InsertBitmapListItem(bitmapList, (L_UINT)-1, &bitmaps[i]);
if(retCode != SUCCESS)
goto CLEANUP;
}
// Save the output file
retCode = L_GetDefaultSaveFileOption(&saveOptions, sizeof(SAVEFILEOPTION));
if(retCode != SUCCESS)
goto CLEANUP;
// Save all pages
saveOptions.PageNumber = -1;
retCode = L_SaveBitmapList(outputFileName, bitmapList, FILE_CCITT_GROUP4, 1, 0, &saveOptions);
CLEANUP:
//free all bitmaps in list & dispose of handle
L_DestroyBitmapList(bitmapList);
return retCode;
}
L_INT L_OcrDocument_ClearPagesExample()
{
L_INT retCode = -1;
L_OcrEngine ocrEngine = NULL;
L_OcrDocumentManager ocrDocumentManager = NULL;
L_OcrDocument ocrDocument = NULL;
L_OcrPage ocrPage = NULL;
LOADFILEOPTION loadOpts;
FILEINFO fileInfo;
// For this example, we need a multi-page TIF file.
// If you have a different sample file, replace the file name below
L_TCHAR* multiPageFile = MAKE_IMAGE_PATH(L_TEXT("Multipage.tif"));
if(CreateMultiPageFile(multiPageFile) != SUCCESS)
return FAILURE;
// Create an instance of the engine
retCode = L_OcrEngineManager_CreateEngine(L_OcrEngineType_LEAD, &ocrEngine);
if(retCode != SUCCESS)
return retCode;
// Start the engine using default parameters
retCode = L_OcrEngine_Startup(ocrEngine, NULL, OCR_LEAD_RUNTIME_DIR);
if(retCode == SUCCESS)
{
retCode = L_OcrEngine_GetDocumentManager(ocrEngine, &ocrDocumentManager);
if(retCode != SUCCESS)
goto CLEANUP;
// Create memory-based OCR document
retCode = L_OcrDocumentManager_CreateDocument(ocrDocumentManager, &ocrDocument, L_OcrCreateDocumentOptions_InMemory, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Load each page of the multi-page tif file we created and process them
L_GetDefaultLoadFileOption(&loadOpts, sizeof(LOADFILEOPTION));
retCode = L_FileInfo(multiPageFile, &fileInfo, sizeof(FILEINFO), FILEINFO_TOTALPAGES, &loadOpts);
if(retCode != SUCCESS)
goto CLEANUP;
for(L_INT pageNum = 1; pageNum <= fileInfo.TotalPages; pageNum++)
{
// Load page
BITMAPHANDLE bitmap = { 0 };
loadOpts.PageNumber = pageNum;
retCode = L_LoadBitmap(multiPageFile, &bitmap, sizeof(BITMAPHANDLE), 0, ORDER_RGB, &loadOpts, &fileInfo);
if(retCode != SUCCESS)
goto CLEANUP;
// Create Ocr page
retCode = L_OcrPage_FromBitmap(ocrEngine, &ocrPage, &bitmap, L_OcrBitmapSharingMode_AutoFree, NULL, NULL);
if(retCode != SUCCESS)
{
L_FreeBitmap(&bitmap);
goto CLEANUP;
}
// Transfer ownership to the OCR page
bitmap.Flags.Allocated = 0;
// Find zones in the page
retCode = L_OcrPage_AutoZone(ocrPage, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Recognize
retCode = L_OcrPage_Recognize(ocrPage, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// In Document File Mode, add OcrPage to OcrDocument after recognition
retCode = L_OcrDocument_AddPage(ocrDocument, ocrPage);
if(retCode != SUCCESS)
goto CLEANUP;
}
// Save
retCode = L_OcrDocument_Save(ocrDocument, MAKE_IMAGE_PATH(L_TEXT("Multipage.pdf")), DOCUMENTFORMAT_PDF, NULL, NULL);
L_UINT uPagesCount = 0;
retCode = L_OcrDocument_GetPageCount(ocrDocument, &uPagesCount);
if(retCode != SUCCESS)
goto CLEANUP;
// Delete the last page from the document and insert the first page in its place then re-save the document again.
retCode = L_OcrDocument_RemovePageAt(ocrDocument, uPagesCount - 1);
if(retCode != SUCCESS)
goto CLEANUP;
L_OcrPage ocrPage2 = NULL;
retCode = L_OcrDocument_GetPageAt(ocrDocument, 0, &ocrPage2);
if(retCode != SUCCESS)
goto CLEANUP;
retCode = L_OcrDocument_InsertPage(ocrDocument, uPagesCount, ocrPage2);
if(retCode != SUCCESS)
goto CLEANUP;
// double check that the page was inserted at the end of the document by getting its index
L_INT pageIndex = -1;
L_OcrDocument_IndexOfPage(ocrDocument, ocrPage2, &pageIndex);
assert(pageIndex == (L_INT)uPagesCount);
retCode = L_OcrDocument_Save(ocrDocument, MAKE_IMAGE_PATH(L_TEXT("Multipage2.pdf")), DOCUMENTFORMAT_PDF, NULL, NULL);
// Now clear OCR document pages
L_OcrDocument_ClearPages(ocrDocument);
}
CLEANUP:
if(ocrDocument != NULL)
L_OcrDocument_Destroy(ocrDocument);
if(ocrEngine != NULL)
L_OcrEngine_Destroy(ocrEngine);
return retCode;
}