I am trying to perform OCR using syncfusion OCR and i am loading the document from my server together with all the required libraries. I am getting an error saying object reference not set to an instance of an object on the line where am performing the actual OCR with the language data and i debugged the code there is no error or missing OCR libraries but still am getting the error. Below is the code:
try
{
string binaries = Path.Combine(this._hostingEnvironment.ContentRootPath, "Tesseractbinaries", "Windows");
//Initialize OCR processor with tesseract binaries.
OCRProcessor processor = new OCRProcessor(binaries);
//Set language to the OCR processor.
processor.Settings.Language = Languages.English;
string path = Path.Combine(this._hostingEnvironment.ContentRootPath, @"Data\font", "times.ttf");
FileStream fontStream = new FileStream(path, FileMode.Open);
//Create a true type font to support unicode characters in PDF.
processor.UnicodeFont = new PdfTrueTypeFont(fontStream, 8);
//Set temporary folder to save intermediate files.
processor.Settings.TempFolder = Path.Combine(this._hostingEnvironment.ContentRootPath, "Data");
//Load a PDF document.
FileStream inputDocument = new FileStream(Path.Combine(this._hostingEnvironment.ContentRootPath, "Data", "test.pdf"), FileMode.Open);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);
//Here is where am getting the error//
string tessdataPath = Path.Combine(this._hostingEnvironment.ContentRootPath, "tessdata");
//string tessdataPath = Path.Combine(@"tessdata");
processor.PerformOCR(loadedDocument, tessdataPath);
//Save the PDF document.
MemoryStream outputDocument = new MemoryStream();
loadedDocument.Save(outputDocument);
outputDocument.Position = 0;
//Dispose OCR processor and PDF document.
processor.Dispose();
loadedDocument.Close(true);
//Download the PDF document in the browser.
FileStreamResult fileStreamResult = new FileStreamResult(outputDocument, "application/pdf");
fileStreamResult.FileDownloadName = document.Name +".pdf";
//setting a path for saving it to my server and copying it to the folder downloads
string filePath = Path.Combine("wwwroot/documents/", fileStreamResult.FileDownloadName);
using (Stream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
{
fileStreamResult.FileStream.CopyTo(fileStream);
}
//return fileStreamResult;
}
catch (Exception ex)
{
throw;
}