#include "ltocr.h"
L_LTOCR_API L_INT EXT_FUNCTION L_OcrDocumentManager_AppendDocuments(srcDocument, dstDocument)
Appends two OCR document handles while they are still in memory.
Source document handle.
Destination document handle.
Value | Meaning |
---|---|
SUCCESS | The function was successful. |
< 1 | An error occurred. Refer to Return Codes. |
This function appends two OCR document handles that was previously created by calling L_OcrDocumentManager_CreateDocument while they are still in memory. For more information on creating OCR document files, refer to L_OcrDocumentManager_CreateDocument.
After appending the document files you can convert them to any of LEADTOOLS supported document formats like PDF, DOC, DOCX, TXT, and many other formats.
Required DLLs and Libraries
L_INT L_OcrDocumentManager_AppendDocumentsExample()
{
BITMAPHANDLE bitmap1 = { 0 };
BITMAPHANDLE bitmap2 = { 0 };
L_OcrEngine ocrEngine = NULL;
L_OcrPage ocrPage1 = NULL;
L_OcrPage ocrPage2 = NULL;
L_OcrDocumentManager ocrDocumentManager = NULL;
L_OcrDocument ocrDocument1 = NULL;
L_OcrDocument ocrDocument2 = NULL;
// Create an instance of the engine
L_INT retCode = L_OcrEngineManager_CreateEngine(L_OcrEngineType_Advantage, &ocrEngine);
if(retCode == SUCCESS)
{
// Start the engine using default parameters
retCode = L_OcrEngine_Startup(ocrEngine, NULL, OCR_ADVANTAGE_RUNTIME_DIR);
if(retCode != SUCCESS)
return retCode;
// Load first image
retCode = L_LoadBitmap(MAKE_IMAGE_PATH(L_TEXT("Ocr1.tif")), &bitmap1, sizeof(BITMAPHANDLE), 0, ORDER_RGB, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Add an image to OCR page. Transfer ownership of the bitmap to the page
retCode = L_OcrPage_FromBitmap(ocrEngine, &ocrPage1, &bitmap1, L_OcrBitmapSharingMode_AutoFree, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// We have a valid page and bitmap ownership has transfered. So, we do not need to free the bitmap anymore.
// Bitmap will be freed when ocrPage1 is destroyed.
bitmap1.Flags.Allocated = 0;
// Recognize the page
// Note: Recognize can be called without calling AutoZone or manually adding zones.
// The engine will check and automatically auto-zones the page.
retCode = L_OcrPage_Recognize(ocrPage1, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Load second image
retCode = L_LoadBitmap(MAKE_IMAGE_PATH(L_TEXT("Ocr2.tif")), &bitmap2, sizeof(BITMAPHANDLE), 0, ORDER_RGB, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Add an image to OCR page. Transfer ownership of the bitmap to the page
retCode = L_OcrPage_FromBitmap(ocrEngine, &ocrPage2, &bitmap2, L_OcrBitmapSharingMode_AutoFree, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// We have a valid page and bitmap ownership has transfered. So, we do not need to free the bitmap anymore.
// Bitmap will be freed when ocrPage1 is destroyed.
bitmap2.Flags.Allocated = 0;
// Recognize the page
// Note: Recognize can be called without calling AutoZone or manually adding zones.
// The engine will check and automatically auto-zones the page.
retCode = L_OcrPage_Recognize(ocrPage2, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
//Get the document manager
retCode = L_OcrEngine_GetDocumentManager(ocrEngine, &ocrDocumentManager);
if(retCode != SUCCESS)
goto CLEANUP;
retCode = L_OcrDocumentManager_CreateDocument(ocrDocumentManager, &ocrDocument1, L_OcrCreateDocumentOptions_AutoDeleteFile, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
retCode = L_OcrDocumentManager_CreateDocument(ocrDocumentManager, &ocrDocument2, L_OcrCreateDocumentOptions_AutoDeleteFile, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Adding pages to OCR documents. Note: When using the OcrDocument in file mode,
// adding the page to the document must come after recognition. If the OcrPage,
// is added before it is recognized, no recognition data will be associated with
// the OcrPage in the OcrDocument.
// Add ocrPage1 to the ocrDocument1.
retCode = L_OcrDocument_AddPage(ocrDocument1, ocrPage1);
if(retCode != SUCCESS)
goto CLEANUP;
// Add ocrPage2 to the ocrDocument2.
retCode = L_OcrDocument_AddPage(ocrDocument2, ocrPage2);
if(retCode != SUCCESS)
goto CLEANUP;
// Now append ocrDocument1 to ocrDocument2, in this case ocrDocument2 is the one that contains both documents (both pages)
retCode = L_OcrDocumentManager_AppendDocuments(ocrDocument1, ocrDocument2);
if(retCode != SUCCESS)
goto CLEANUP;
// Adding the page to a file based document will take a snap shot of the recognition data and store it in the document. At this
// point, the page is no longer needed. So destroy it to free up memory not used anymore
L_OcrPage_Destroy(ocrPage1);
L_OcrPage_Destroy(ocrPage2);
// Set the handle to NULL so we do not free it in our clean-up code
ocrPage1 = NULL;
ocrPage2 = NULL;
// Save the accumulative document (ocrDocument2) to PDF
retCode = L_OcrDocument_Save(ocrDocument2, MAKE_IMAGE_PATH(L_TEXT("final.pdf")), DOCUMENTFORMAT_PDF, NULL, NULL);
}
CLEANUP:
if(bitmap1.Flags.Allocated)
L_FreeBitmap(&bitmap1);
if(bitmap2.Flags.Allocated)
L_FreeBitmap(&bitmap2);
if(ocrPage1 != NULL)
L_OcrPage_Destroy(ocrPage1);
if(ocrPage2 != NULL)
L_OcrPage_Destroy(ocrPage2);
if(ocrDocument1 != NULL)
L_OcrDocument_Destroy(ocrDocument1);
if(ocrDocument2 != NULL)
L_OcrDocument_Destroy(ocrDocument2);
if(ocrEngine != NULL)
L_OcrEngine_Destroy(ocrEngine);
return retCode;
}