private void saveToolStripMenuItem_Click(object sender, EventArgs e) { var converter = new DocumentConverter(); converter.SetOcrEngineInstance(_ocrEngine, false); var jobData = new DocumentConverterJobData() { Document = _documentViewer.Document, DocumentFormat = Leadtools.Forms.DocumentWriters.DocumentFormat.Docx, JobName = "SaveToDocx", OutputDocumentFileName = "sample.docx" }; var job = converter.Jobs.CreateJob(jobData); converter.Jobs.RunJob(job); if (job.Status == DocumentConverterJobStatus.Success) { MessageBox.Show("Word Document Created"); } else { MessageBox.Show("Word Document Creation Failed"); } }
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; } } }
private void DoRecognizeAndSave(OcrData ocrData) { // Read and retrieve the Ocr job parameters from the JobMetadata XML string sent through Job.JobMetadata using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false)) { try { string executingAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string ocrEnginePath = null; if (!String.IsNullOrEmpty(executingAssemblyPath)) { ocrEnginePath = Path.Combine(executingAssemblyPath, @"..\..\common\OcrLEADRuntime"); if (!Directory.Exists(ocrEnginePath)) { ocrEnginePath = null; } } ocrEngine.Startup(null, null, null, ocrEnginePath); DocumentFormat documentFormat = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), ocrData.DocumentFormat, true); if (ocrEngine.DocumentWriterInstance != null) { SetDocumentWriterOptions(ocrEngine.DocumentWriterInstance, documentFormat); } using (DocumentConverter documentConverter = new DocumentConverter()) { documentConverter.SetOcrEngineInstance(ocrEngine, true); documentConverter.Diagnostics.EnableTrace = true; var jobData = DocumentConverterJobs.CreateJobData(ocrData.ImageFileName, ocrData.DocumentFileName, documentFormat); var job = documentConverter.Jobs.CreateJob(jobData); if (null != documentConverter.OcrEngineInstance) { documentConverter.OcrEngineInstance.AutoRecognizeManager.JobOperation += new EventHandler <OcrAutoRecognizeJobOperationEventArgs>(AutoRecognizeManager_JobOperation); documentConverter.OcrEngineInstance.AutoRecognizeManager.JobCompleted += new EventHandler <OcrAutoRecognizeRunJobEventArgs>(AutoRecognizeManager_JobCompleted); } documentConverter.Jobs.RunJob(job); if (null != documentConverter.OcrEngineInstance) { documentConverter.OcrEngineInstance.AutoRecognizeManager.JobOperation -= new EventHandler <OcrAutoRecognizeJobOperationEventArgs>(AutoRecognizeManager_JobOperation); documentConverter.OcrEngineInstance.AutoRecognizeManager.JobCompleted -= new EventHandler <OcrAutoRecognizeRunJobEventArgs>(AutoRecognizeManager_JobCompleted); } //Update job metadata with new filename SetCompletedStatus(_jobId, OcrData.SerializeToString(ocrData)); } } catch (RasterException ex) { SetFailureStatus(_jobId, (int)ex.Code, ex.Message, null); } catch (OcrException ex) { SetFailureStatus(_jobId, (int)ex.Code, ex.Message, null); } catch (Exception ex) { SetFailureStatus(_jobId, 0, ex.Message, null); } finally { ocrEngine.Shutdown(); } } }
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); } }