Beispiel #1
0
        // Run the conversion
        public bool Run(ObjectCache cache, LEADDocument document, DocumentConverter converter, ConvertRedactionOptions redactionOptions)
        {
            this.ErrorMessage             = null;
            this.OutputFiles              = null;
            this.OutputDocumentFiles      = null;
            this.OutputDocumentExtraFiles = null;
            this.OutputAnnotationFiles    = null;

            // Show its info
            ShowInfo(document);

            var disposeConverter = false;

            try
            {
                // If the user did not specify a document converter, create one
                if (converter == null)
                {
                    converter        = new DocumentConverter();
                    disposeConverter = true;
                }

                // Set the options in the document converter from our data
                SetOptions(converter, cache, document, redactionOptions);

                // Create the document converter job
                var job = CreateConverterJob(converter, document);

                // Run it
                Trace.WriteLine("Running job...");
                var stopwatch = new Stopwatch();
                stopwatch.Start();
                converter.Jobs.RunJob(job);
                stopwatch.Stop();

                // Show the results
                ShowResults(job);

                // Handle the output files (if any)
                HandleOutputFiles(job);

                Trace.WriteLine("----------------------------");
                Trace.WriteLine("Total conversion time: " + stopwatch.Elapsed.ToString());

                // See if we need to open the final document. LTD format has no viewer ...
                ViewOutputFile(job);

                // Check the errors
                if (job.Status == DocumentConverterJobStatus.Aborted)
                {
                    // Get the first error
                    if (job.Errors.Count > 0)
                    {
                        this.ErrorMessage = job.Errors[0].Error.Message;
                    }
                }

                return(job.Status != DocumentConverterJobStatus.Aborted);
            }
            catch (OcrException ex)
            {
                this.ErrorMessage = ex.Message;
                Trace.WriteLine(string.Format("OCR error code: {0}\n{1}", ex.Code, ex.Message));
            }
            catch (RasterException ex)
            {
                this.ErrorMessage = ex.Message;
                Trace.WriteLine(string.Format("LEADTOOLS error code: {0}\n{1}", ex.Code, ex.Message));
            }
            catch (Exception ex)
            {
                this.ErrorMessage = ex.Message;
                Trace.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                // If we created the converter, dispose it
                if (converter != null && disposeConverter)
                {
                    converter.Dispose();
                }
            }

            return(false);
        }
Beispiel #2
0
        private void SetOptions(DocumentConverter converter, ObjectCache cache, LEADDocument document, ConvertRedactionOptions redactionOptions)
        {
            converter.SetAnnRenderingEngineInstance(this.AnnRenderingEngine);

            // Set the RasterCodecs instance, should go into the DocumentFactory class which will be used to load the document
            if (this.RasterCodecsInstance != null)
            {
                DocumentFactory.RasterCodecsTemplate = this.RasterCodecsInstance;
            }

            // Set the OCR engine
            if (this.OcrEngineInstance != null && this.OcrEngineInstance.IsStarted)
            {
                converter.SetOcrEngineInstance(this.OcrEngineInstance, false);
            }

            if (this.DocumentWriterInstance != null)
            {
                converter.SetDocumentWriterInstance(this.DocumentWriterInstance);
            }

            // Set pre-processing options
            converter.Preprocessor.Deskew = this.PreprocessingDeskew;
            converter.Preprocessor.Invert = this.PreprocessingInvert;
            converter.Preprocessor.Orient = this.PreprocessingOrient;

            // Enable trace
            converter.Diagnostics.EnableTrace = this.EnableTrace;

            // Setup the load document options
            var loadDocumentOptions = new LoadDocumentOptions();

            // Setup cache
            loadDocumentOptions.Cache    = cache;
            loadDocumentOptions.UseCache = cache != null;

            if (document == null)
            {
                // Set the input annotation mode or file name
                loadDocumentOptions.LoadEmbeddedAnnotations = this.LoadEmbeddedAnnotation;
                if (!this.LoadEmbeddedAnnotation && !string.IsNullOrEmpty(this.InputAnnotationsFileName) && File.Exists(this.InputAnnotationsFileName))
                {
                    // We will use this instead of DocumentConverterJobData.InputAnnotationsFileName (this will override it anyway if we give the
                    // document converter a loadDocumentOptions)
                    loadDocumentOptions.AnnotationsUri = new Uri(this.InputAnnotationsFileName);
                }
            }

            converter.LoadDocumentOptions = loadDocumentOptions;

            // Set options
            converter.Options.JobErrorMode = this.ErrorMode;
            if (!string.IsNullOrEmpty(this.PageNumberingTemplate))
            {
                converter.Options.PageNumberingTemplate = this.PageNumberingTemplate;
            }
            converter.Options.EnableSvgConversion      = this.EnableSvgConversion;
            converter.Options.SvgImagesRecognitionMode = (this.OcrEngineInstance != null && this.OcrEngineInstance.IsStarted) ? this.SvgImagesRecognitionMode : DocumentConverterSvgImagesRecognitionMode.Disabled;
            converter.Options.EmptyPageMode            = this.EmptyPageMode;
            converter.Options.UseThreads      = this.UseThreads;
            converter.Diagnostics.EnableTrace = this.EnableTrace;

            // Set Redaction Options
            if (redactionOptions != null)
            {
                var documentRedactionOptions = new DocumentRedactionOptions();
                documentRedactionOptions.ConvertOptions = redactionOptions;
                if (document != null)
                {
                    documentRedactionOptions.ViewOptions  = document.Annotations.RedactionOptions.ViewOptions;
                    document.Annotations.RedactionOptions = documentRedactionOptions;
                }
                else
                {
                    converter.LoadDocumentOptions.RedactionOptions = documentRedactionOptions;
                }
            }
        }
Beispiel #3
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);
        }