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");
            }
        }
Beispiel #2
0
        private DocumentConverterJob CreateConverterJob(DocumentConverter converter, LEADDocument document)
        {
            // Set the maximum page
            var firstPage = this.InputFirstPage;

            if (firstPage == 0)
            {
                firstPage = 1;
            }

            var lastPage = this.InputLastPage;

            if (lastPage == 0)
            {
                lastPage = -1;
            }

            if (document != null && this.InputMaximumPages > 0)
            {
                if (lastPage == -1)
                {
                    lastPage = document.Pages.Count;
                }
                lastPage = Math.Min(lastPage, firstPage + this.InputMaximumPages - 1);
            }

            // Create a job
            var jobData = new DocumentConverterJobData
            {
                InputDocumentFileName = document == null ? this.InputDocumentFileName : null,
                Document = document,
                InputDocumentFirstPageNumber = firstPage,
                InputDocumentLastPageNumber  = lastPage,
                DocumentFormat            = this.DocumentFormat,
                RasterImageFormat         = this.RasterImageFormat,
                RasterImageBitsPerPixel   = this.RasterImageBitsPerPixel,
                OutputDocumentFileName    = this.OutputDocumentFileName,
                AnnotationsMode           = this.OutputAnnotationsMode,
                OutputAnnotationsFileName = this.OutputAnnotationsFileName,
                JobName  = this.JobName,
                UserData = null,
            };

            jobData.InputAnnotationsFileName = this.InputAnnotationsFileName;

            var job = converter.Jobs.CreateJob(jobData);

            return(job);
        }
Beispiel #3
0
        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);
        }