예제 #1
0
        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);
            }
        }
예제 #2
0
        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);
        }
예제 #3
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);
            }
        }