Moves the L_OcrPage to different index inside the OCR document.
#include "ltocr.h"
L_LTOCR_API L_INT EXT_FUNCTION L_OcrDocument_MovePage(document, page, index)
Handle to the OCR document.
L_OcrPage to move inside the document.
The new index of L_OcrPage inside the document.
Value | Meaning |
---|---|
SUCCESS | The function was successful. |
< 1 | An error occurred. Refer to Return Codes. |
Moves the L_OcrPage to different index inside the OCR document.
For more information on creating the OCR pages, refer to L_OcrPage_FromBitmap.
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_MovePageExample()
{
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);
// now we will try to swap first and second pages and then delete the last page and then re-save the document
// final document should have 3 pages instead of 4 and with the first page in place of the second one.
L_OcrPage firstPage = NULL;
retCode = L_OcrDocument_GetPageAt(ocrDocument, 0, &firstPage);
if(retCode != SUCCESS)
goto CLEANUP;
retCode = L_OcrDocument_MovePage(ocrDocument, firstPage, 1);
if(retCode != SUCCESS)
goto CLEANUP;
// Delete last page
L_UINT uPagesCount = 0;
retCode = L_OcrDocument_GetPageCount(ocrDocument, &uPagesCount);
if(retCode != SUCCESS)
goto CLEANUP;
L_OcrPage lastPage = NULL;
retCode = L_OcrDocument_GetPageAt(ocrDocument, uPagesCount - 1, &lastPage);
if(retCode != SUCCESS)
goto CLEANUP;
retCode = L_OcrDocument_RemovePage(ocrDocument, lastPage);
if(retCode != SUCCESS)
goto CLEANUP;
retCode = L_OcrDocument_Save(ocrDocument, MAKE_IMAGE_PATH(L_TEXT("Multipage2.pdf")), DOCUMENTFORMAT_PDF, NULL, NULL);
}
CLEANUP:
if(ocrDocument != NULL)
L_OcrDocument_Destroy(ocrDocument);
if(ocrEngine != NULL)
L_OcrEngine_Destroy(ocrEngine);
return retCode;
}