0

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;
            }
codeninja
  • 315
  • 2
  • 10
  • Are you familiar with how to use the debugger? You can set a breakpoint and, when it's hit, just over each variable to see its value. Somewhere, one of your variables is null. You need to determine why and/or write code to check for the case where it's null. – Jonathan Wood Mar 29 '22 at 20:32
  • 1
    An NRE doesnt mean there are missing libraries. The problem is more basic. See [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Mar 29 '22 at 20:39

0 Answers0