public OrionRecognize() { // TestDirectCallsToDLL(); //minimal code to demonstrate how to use DLL directly, without COM //it is a good idea to create NSOCR instance here instead of creting it in the same line with "NsOCR" definition //this way we can handle possible errors if NSOCR is not installed try { NsOCR = new NSOCRLib.NSOCRClass(); } catch (Exception exc) //some error { string msg = "Cannot create NSOCR COM object instance. Possible reasons:\r\n - NSOCR.dll is missed.\r\n - NSOCR.dll was not registered with Regsvr32.\r\n - NSOCR.dll is x32 but this application is x64.\r\n"; msg = msg + "\r\n Exception message:\r\n\r\n" + exc.Message; System.Windows.Forms.MessageBox.Show(msg); //Close(); return; } // gr = PicBox.CreateGraphics(); Inited = true; //NSOCR instance created successfully //get NSOCR version string val; NsOCR.Engine_GetVersion(out val); //Text = Text + " [ NSOCR version: " + val + " ] "; //init engine and create ocr-related objects if (true /*false*/) //change to "false" to reduce code and initialize engine + create main objects in one line { NsOCR.Engine_Initialize(); NsOCR.Cfg_Create(out CfgObj); //load options, if path not specified, folder with NSOCR.dll will be checked NsOCR.Cfg_LoadOptions(CfgObj, "Config.dat"); NsOCR.Ocr_Create(CfgObj, out OcrObj); NsOCR.Img_Create(OcrObj, out ImgObj); } else //do the same in one line { NsOCR.Engine_InitializeAdvanced(out CfgObj, out OcrObj, out ImgObj); } NsOCR.Scan_Create(CfgObj, out ScanObj); //bkRecognize.Enabled = false; NoEvent = true; //cbScale.SelectedIndex = 0; NoEvent = false; //bkSave.Enabled = false; //copy some settings to GUI //if (NsOCR.Cfg_GetOption(CfgObj, TNSOCR.BT_DEFAULT, "ImgAlizer/AutoScale", out val) < TNSOCR.ERROR_FIRST) // cbScale.Enabled = (val == "1"); this.ListZoneName = new List <OrionRecognizeZoneDetail>(); }
/// <summary> /// Converts PDF to DOCX, RTF, HTML, Text with OCR engine. /// </summary> public void ConvertPdfToAllWithOCR(string pdfPath) { // To perform OCR we'll use free OCR library by Nicomsoft. // https://www.nicomsoft.com/products/ocr/download/ // The library is freeware and can be used in commercial application. // Also you have to insert this key: AB2A4DD5FF2A. NsOCR = new NSOCRLib.NSOCRClass(); NsOCR.Engine_SetLicenseKey("AB2A4DD5FF2A"); //required for licensed version only NsOCR.Engine_InitializeAdvanced(out CfgObj, out OcrObj, out ImgObj); SautinSoft.PdfFocus f = new SautinSoft.PdfFocus(); f.OCROptions.Method += PerformOCRNicomsoft; f.OCROptions.Mode = PdfFocus.COCROptions.eOCRMode.AllImages; f.WordOptions.KeepCharScaleAndSpacing = false; string pdfFile = pdfPath; string outFile = String.Empty; f.OpenPdf(pdfFile); if (f.PageCount > 0) { // To Docx. outFile = "Result.docx"; f.WordOptions.Format = PdfFocus.CWordOptions.eWordDocument.Docx; if (f.ToWord(outFile) == 0) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true }); } // To HTML. outFile = "Result.html"; f.HtmlOptions.KeepCharScaleAndSpacing = false; if (f.ToHtml(outFile) == 0) { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true }); } } else { Console.WriteLine("Error: {0}!", f.Exception.Message); Console.ReadLine(); } }
public static byte[] PerformOCRNicomsoft(byte[] image) { NSOCRLib.NSOCRClass NsOCR; int CfgObj = 0; int OcrObj = 0; int ImgObj = 0; int SvrObj = 0; NsOCR = new NSOCRLib.NSOCRClass(); NsOCR.Engine_SetLicenseKey("AB2A4DD5FF2A"); //required for licensed version only NsOCR.Engine_InitializeAdvanced(out CfgObj, out OcrObj, out ImgObj); // Scale NsOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "ImgAlizer/AutoScale", "0"); NsOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "ImgAlizer/ScaleFactor", "4.0"); NsOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Languages/English", "1"); try { int res = 0; Array imgArray = null; using (MemoryStream ms = new MemoryStream(image)) { ms.Flush(); imgArray = ms.ToArray(); } res = NsOCR.Img_LoadFromMemory(ImgObj, ref imgArray, imgArray.Length); if (res > TNSOCR.ERROR_FIRST) { return(null); } NsOCR.Svr_Create(CfgObj, TNSOCR.SVR_FORMAT_PDF, out SvrObj); NsOCR.Svr_NewDocument(SvrObj); res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_FIRST, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE); if (res > TNSOCR.ERROR_FIRST) { return(null); } res = NsOCR.Svr_AddPage(SvrObj, ImgObj, TNSOCR.FMT_EXACTCOPY); if (res > TNSOCR.ERROR_FIRST) { return(null); } Array outPdf = null; NsOCR.Svr_SaveToMemory(SvrObj, out outPdf); return((byte[])outPdf); } finally { } }