static int Main(string[] args) { var preferencesFileName = args.Length == 1 ? args[0] : null; try { return(Run(preferencesFileName)); } catch (Exception ex) { try { // Load it from the file specified by the user if (!string.IsNullOrEmpty(preferencesFileName)) { var preferences = DocumentConverterPreferences.Load(preferencesFileName); preferences.ErrorMessage = ex.Message; preferences.Save(preferencesFileName); } } catch { } return(1); } }
// Initialize the OCR engine used throughout the application private static IOcrEngine InitOcrEngine(DocumentConverterPreferences preferences) { // try to start the OCR engine IOcrEngine ocrEngine = null; var ocrEngineType = preferences.OCREngineType; try { Trace.WriteLine("Starting OCR engine"); ocrEngine = OcrEngineManager.CreateEngine(ocrEngineType, false); #if LT_CLICKONCE var ocrEngineRuntimePath = Path.Combine(Application.StartupPath, @"OCR Engine"); #else var ocrEngineRuntimePath = preferences.OCREngineRuntimePath; if (string.IsNullOrEmpty(ocrEngineRuntimePath)) { // Maybe in a local folder under this EXE? if (ocrEngineType == OcrEngineType.LEAD) { ocrEngineRuntimePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"LEADTOOLS\OcrLEADRuntime"); } else if (ocrEngineType == OcrEngineType.OmniPage) { if (IntPtr.Size == 8) { ocrEngineRuntimePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"LEADTOOLS\OcrOmniPageRuntime64"); } else { ocrEngineRuntimePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"LEADTOOLS\OcrOmniPageRuntime"); } } } if (!Directory.Exists(ocrEngineRuntimePath)) { // No, check the registry if this machine has LEADTOOLS installed ocrEngineRuntimePath = null; } #endif ocrEngine.Startup(null, null, null, ocrEngineRuntimePath); } catch (Exception ex) { ocrEngine = null; var message = string.Format("Failed to start the OCR engine. The demo will continue running without OCR functionality and you will not be able to parse text from non-document files such as TIFF or Raster PDF.\n\nError message:\n{0}", ex.Message); Trace.WriteLine(message); } return(ocrEngine); }
private static string GetExtension(DocumentConverterPreferences preferences) { if (preferences.DocumentFormat != DocumentFormat.User) { return(DocumentWriter.GetFormatFileExtension(preferences.DocumentFormat)); } else { return(RasterCodecs.GetExtension(preferences.RasterImageFormat)); } }
private static int Run(string preferencesFileName) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Messager.Caption = "Document Converter Demo"; // Initialize Trace Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Console.WriteLine("LEADTOOLS " + Messager.Caption); DocumentConverterPreferences preferences; ConvertRedactionOptions convertRedactionOptions = null; if (!string.IsNullOrEmpty(preferencesFileName)) { // Load it from the file specified by the user preferences = DocumentConverterPreferences.Load(preferencesFileName); preferences.IsSilentMode = true; } else { // Load the preferences file DocumentConverterPreferences.DemoName = "Document Converter Demo"; DocumentConverterPreferences.XmlFileName = "DocumentConverterDemo"; preferences = DocumentConverterPreferences.Load(); preferences.IsSilentMode = false; } if (!Support.SetLicense(preferences.IsSilentMode)) { if (preferences.IsSilentMode) { throw new Exception("Your license file is missing, invalid or expired."); } return(-1); } // Create the rendering engine try { if (preferences.AnnRenderingEngine == null) { preferences.AnnRenderingEngine = new AnnWinFormsRenderingEngine(); preferences.AnnRenderingEngine.Resources = Tools.LoadResources(); } } catch { } if (!preferences.IsSilentMode) { // Show the OCR engine selection dialog to startup the OCR engine Trace.WriteLine("Starting OCR engine"); var engineType = preferences.OCREngineType; using (var dlg = new OcrEngineSelectDialog(DocumentConverterPreferences.DemoName, engineType.ToString(), true)) { dlg.AllowNoOcr = true; dlg.AllowNoOcrMessage = "The demo runs without OCR functionality but you will not be able to parse text from non-document files such as TIFF or Raster PDF. Click 'Cancel' to start this demo without an OCR engine."; if (dlg.ShowDialog() == DialogResult.OK) { preferences.OcrEngineInstance = dlg.OcrEngine; preferences.OCREngineType = dlg.OcrEngine.EngineType; Trace.WriteLine(string.Format("OCR engine {0} started", preferences.OCREngineType)); } } } else { // Initialize the default OCR engine preferences.OcrEngineInstance = InitOcrEngine(preferences); } // Initialize the RasterCodecs instance var rasterCodecs = new RasterCodecs(); rasterCodecs.Options = DocumentFactory.RasterCodecsTemplate.Options.Clone(); preferences.RasterCodecsInstance = rasterCodecs; if (!string.IsNullOrEmpty(preferences.RasterCodecsOptionsPath)) { preferences.RasterCodecsInstance.LoadOptions(preferences.RasterCodecsOptionsPath); } // Initialize the DocumentWriter instance preferences.DocumentWriterInstance = new DocumentWriter(); if (!string.IsNullOrEmpty(preferences.DocumentWriterOptionsPath)) { preferences.DocumentWriterInstance.LoadOptions(preferences.DocumentWriterOptionsPath); } // Cache to use ObjectCache cache = null; // Initialize the cache if (!string.IsNullOrEmpty(preferences.CacheDirectory)) { var fileCache = new FileCache(); fileCache.CacheDirectory = preferences.CacheDirectory; fileCache.DataSerializationMode = preferences.CacheDataSerializationMode; fileCache.PolicySerializationMode = preferences.CachePolicySerializationMode; cache = fileCache; } // Do conversions var more = true; while (more) { Console.WriteLine("Obtaining conversion options"); if (!preferences.IsSilentMode) { // Collect the options using (var dlg = new DocumentConverterDialog()) { dlg.Preferences = preferences.Clone(); dlg.RedactionOptions = convertRedactionOptions; if (dlg.ShowDialog() == DialogResult.OK) { preferences = dlg.Preferences.Clone(); convertRedactionOptions = dlg.RedactionOptions; } else { more = false; } } } if (more) { try { // Save the preferences if (!preferences.IsSilentMode) { preferences.Save(); } // Run the conversion if (preferences.DocumentId != null) { var loadFromCacheOptions = new LoadFromCacheOptions { Cache = cache, DocumentId = preferences.DocumentId, UserToken = preferences.DocumentUserToken }; using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions)) { if (document == null) { throw new Exception(string.Format("Could not load document with ID '{0}' from the cache", preferences.DocumentId)); } preferences.Run(cache, document, null, convertRedactionOptions); } } else { preferences.Run(null, null, null, convertRedactionOptions); } } catch (Exception ex) { if (!preferences.IsSilentMode) { Messager.ShowError(null, ex.Message); } else { preferences.ErrorMessage = ex.Message; } } } if (more) { if (!preferences.IsSilentMode) { // Ask if user wants to convert another document more = (Messager.ShowQuestion(null, "Convert more?", MessageBoxButtons.YesNo) == DialogResult.Yes); } else { more = false; } } } if (preferences.OcrEngineInstance != null) { preferences.OcrEngineInstance.Dispose(); } if (preferences.RasterCodecsInstance != null) { preferences.RasterCodecsInstance.Dispose(); } if (preferencesFileName != null) { preferences.Save(preferencesFileName); } if (preferences.ErrorMessage != null) { return(1); } return(0); }
static void Main() { if (!Support.SetLicense()) { return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Messager.Caption = "Document Converter Folder Demo"; // Initialize Trace Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Console.WriteLine("LEADTOOLS " + Messager.Caption); // Load the preferences file DocumentConverterPreferences.DemoName = "Document Converter Folder Demo"; DocumentConverterPreferences.XmlFileName = "DocumentConverterFolderDemo"; var preferences = DocumentConverterPreferences.Load(); preferences.OpenOutputDocumentAllowed = false; MyOptions.XmlFileName = "DocumentConverterFolderOptions"; var myOptions = MyOptions.Load(); var runConversion = false; using (var dlg = new MyOptionsDialog()) { dlg.MyOptions = myOptions.Clone(); if (dlg.ShowDialog() == DialogResult.OK) { runConversion = true; myOptions = dlg.MyOptions.Clone(); myOptions.Save(); } } if (!runConversion) { return; } // Initialize OCR engine // Show the OCR engine selection dialog to startup the OCR engine Trace.WriteLine("Starting OCR engine"); var engineType = preferences.OCREngineType; using (var dlg = new OcrEngineSelectDialog(DocumentConverterPreferences.DemoName, engineType.ToString(), true)) { dlg.AllowNoOcr = true; dlg.AllowNoOcrMessage = "The demo runs without OCR functionality but you will not be able to parse text from non-document files such as TIFF or Raster PDF. Click 'Cancel' to start this demo without an OCR engine."; if (dlg.ShowDialog() == DialogResult.OK) { preferences.OcrEngineInstance = dlg.OcrEngine; preferences.OCREngineType = dlg.OcrEngine.EngineType; Trace.WriteLine(string.Format("OCR engine {0} started", preferences.OCREngineType)); } } _handler += new EventHandler(Handler); SetConsoleCtrlHandler(_handler, true); try { // Initialize the RasterCodecs instance var rasterCodecs = new RasterCodecs(); rasterCodecs.Options = DocumentFactory.RasterCodecsTemplate.Options.Clone(); preferences.RasterCodecsInstance = rasterCodecs; // Initialize the DocumentWriter instance preferences.DocumentWriterInstance = new DocumentWriter(); // Get the options Console.WriteLine("Obtaining conversion options"); // Collect the options using (var dlg = new DocumentConverterDialog()) { // Create a dummy document so the options do not bug us about a input/output files using (var document = DocumentFactory.Create("Raster", new CreateDocumentOptions { MimeType = "image/tiff" })) { dlg.InputDocument = document; dlg.Preferences = preferences.Clone(); dlg.InputDocument = document; if (dlg.ShowDialog() == DialogResult.OK) { preferences = dlg.Preferences.Clone(); // Save the preferences preferences.Save(); } else { runConversion = false; } } } if (runConversion) { // Set the RasterCodecs instance, should go into the DocumentFactory class which will be used to load the document if (preferences.RasterCodecsInstance != null) { DocumentFactory.RasterCodecsTemplate = preferences.RasterCodecsInstance; } DocumentConverter converter = new DocumentConverter(); // Set the OCR engine if (preferences.OcrEngineInstance != null) { converter.SetOcrEngineInstance(preferences.OcrEngineInstance, false); } if (preferences.DocumentWriterInstance != null) { converter.SetDocumentWriterInstance(preferences.DocumentWriterInstance); } // Set pre-processing options converter.Preprocessor.Deskew = preferences.PreprocessingDeskew; converter.Preprocessor.Invert = preferences.PreprocessingInvert; converter.Preprocessor.Orient = preferences.PreprocessingOrient; // Enable trace converter.Diagnostics.EnableTrace = preferences.EnableTrace; // Set options converter.Options.JobErrorMode = preferences.ErrorMode; converter.Options.EnableSvgConversion = preferences.EnableSvgConversion; converter.Options.SvgImagesRecognitionMode = (preferences.OcrEngineInstance != null && preferences.OcrEngineInstance.IsStarted) ? preferences.SvgImagesRecognitionMode : DocumentConverterSvgImagesRecognitionMode.Disabled; converter.Diagnostics.EnableTrace = preferences.EnableTrace; try { RunConversion(converter, preferences, myOptions); } catch (Exception ex) { Trace.WriteLine("Error " + ex.Message); } } if (preferences.OcrEngineInstance != null) { preferences.OcrEngineInstance.Dispose(); } if (preferences.RasterCodecsInstance != null) { preferences.RasterCodecsInstance.Dispose(); } _handler -= new EventHandler(Handler); Console.WriteLine("\nDone, Press and key to close demo."); Console.ReadKey(); } finally { _handler -= new EventHandler(Handler); } }
private static bool Convert(DocumentConverter converter, string inFile, string outFile, DocumentConverterPreferences preferences) { // Setup the load document options var loadDocumentOptions = new LoadDocumentOptions(); // Not using cache loadDocumentOptions.UseCache = false; // Set the input annotation mode or file name loadDocumentOptions.LoadEmbeddedAnnotations = preferences.LoadEmbeddedAnnotation; converter.LoadDocumentOptions = loadDocumentOptions; if (preferences.DocumentFormat == DocumentFormat.Ltd && File.Exists(outFile)) { File.Delete(outFile); } // Create a job var jobData = new DocumentConverterJobData { InputDocumentFileName = inFile, InputDocumentFirstPageNumber = preferences.InputFirstPage, InputDocumentLastPageNumber = preferences.InputLastPage, DocumentFormat = preferences.DocumentFormat, RasterImageFormat = preferences.RasterImageFormat, RasterImageBitsPerPixel = preferences.RasterImageBitsPerPixel, OutputDocumentFileName = outFile, AnnotationsMode = preferences.OutputAnnotationsMode, JobName = preferences.JobName, UserData = null, }; // Create the job var job = converter.Jobs.CreateJob(jobData); var ret = true; // Run it try { Trace.WriteLine("Running job..."); var stopwatch = new Stopwatch(); stopwatch.Start(); converter.Jobs.RunJob(job); stopwatch.Stop(); var elapsed = stopwatch.ElapsedMilliseconds; _totalTime += elapsed; // If we have errors, show them Trace.WriteLine("----------------------------------"); Trace.WriteLine("Status: " + job.Status); Trace.WriteLine("----------------------------------"); Trace.WriteLine("Conversion modes: " + job.ConversionModes); Log(string.Format("{0} - {1} - {2}", job.Status, job.ConversionModes, inFile)); ret = job.Status == DocumentConverterJobStatus.Success; if (job.Errors.Count > 0) { ret = false; // We have errors, show them Trace.WriteLine("Errors found:"); Log("Errors found:"); foreach (var error in job.Errors) { var message = string.Format("Page: {0} - Operation: {1} - Error: {2}", error.InputDocumentPageNumber, error.Operation, error.Error); Trace.WriteLine(message); Log(message); } } Trace.WriteLine("Total conversion time: " + elapsed.ToString()); Log("Total conversion time: " + elapsed.ToString()); Trace.WriteLine("----------------------------"); } catch (OcrException ex) { var message = string.Format("OCR error code: {0} - {1}", ex.Code, ex.Message); Trace.WriteLine(message); Log(string.Format("{0} - {1}", message, inFile)); ret = false; } catch (RasterException ex) { var message = string.Format("LEADTOOLS error code: {0} - {1}", ex.Code, ex.Message); Trace.WriteLine(message); Log(string.Format("{0} - {1}", message, inFile)); ret = false; } catch (Exception ex) { var message = string.Format("Error: {0} - {1}", ex.GetType().FullName, ex.Message); Trace.WriteLine(message); Log(string.Format("{0} - {1}", message, inFile)); ret = false; } return(ret); }
private static void RunConversion(DocumentConverter converter, DocumentConverterPreferences preferences, MyOptions myOptions) { if (string.IsNullOrEmpty(myOptions.InputFolder) || !Directory.Exists(myOptions.InputFolder)) { Trace.WriteLine("Input filder does not exist"); return; } if (!Directory.Exists(myOptions.OutputFolder)) { Directory.CreateDirectory(myOptions.OutputFolder); } _logFile = Path.Combine(myOptions.OutputFolder, "_log.txt"); if (File.Exists(_logFile)) { File.Delete(_logFile); } _totalTime = 0; // Run here var filter = myOptions.Extension != null?myOptions.Extension.Trim() : null; if (string.IsNullOrEmpty(filter)) { filter = "*.*"; } var files = Directory.GetFiles(myOptions.InputFolder, filter); var temp = new List <string>(); foreach (var file in files) { if (Path.GetExtension(file).ToLower() != ".db") { temp.Add(file); } } files = temp.ToArray(); Trace.WriteLine(string.Format("{0} files", files.Length)); var extension = GetExtension(preferences); var index = 0; var count = files.Length; foreach (var inFile in files) { var tmp = Path.GetFileName(inFile); tmp = tmp.Remove(tmp.IndexOf(Path.GetExtension(inFile))); var outFile = (tmp + "_" + Path.GetExtension(inFile).Replace(".", "")).Replace(".", "_"); outFile = Path.Combine(myOptions.OutputFolder, Path.ChangeExtension(outFile, extension)); Trace.WriteLine(string.Format("{0}:{1}", index + 1, count)); Trace.WriteLine(inFile); Trace.WriteLine(outFile); index++; Convert(converter, inFile, outFile, preferences); } Log("Overall conversion time: " + _totalTime.ToString()); Trace.WriteLine("Overall conversion time: " + _totalTime.ToString()); }