protected override void OnFormClosed(FormClosedEventArgs e) { if (!DesignMode) { _documentFormatOptionsControl.SelectedFormatChanged -= new EventHandler <EventArgs>(_documentFormatOptionsControl_SelectedFormatChanged); _docWriter.Progress -= new EventHandler <DocumentProgressEventArgs>(DocumentWriterInstance_Progress); // Save the last setting Properties.Settings settings = new Properties.Settings(); if (_docWriter != null) { using (MemoryStream ms = new MemoryStream()) { _docWriter.SaveOptions(ms); settings.FormatOptionsXml = Encoding.Unicode.GetString(ms.ToArray()); } } settings.ViewFinalDocument = _viewDocumentCheckBox.Checked; settings.OutputFileName = _outputFileNameTextBox.Text; settings.LTDDocumentTypeIndex = _ltdDocumentTypeComboBox.SelectedIndex; settings.Save(); } base.OnFormClosed(e); }
public void Convert(DocumentWriter docWriter, OcrEngineType engineType, string[] sourceFiles, string destinationDirectory, DocumentFormat format, bool loopContinuously) { _docWriter = docWriter; _engineType = engineType; _sourceFiles = sourceFiles; _destinationDirectory = destinationDirectory; _format = format; _loopContinuously = loopContinuously; _logFileName = Path.Combine(destinationDirectory, "_Log.txt"); // number of documents to process together maximum of 8 or number of cores int maxThreadCount = Math.Min(8, Environment.ProcessorCount); int documentCount = sourceFiles.Length; if (loopContinuously) { _lblInformation.Text = string.Format("Total number of documents is {0}, maximum number of threads is {1}, Iteration {2}", documentCount, maxThreadCount, _iteration + 1); } else { _lblInformation.Text = string.Format("Total number of documents is {0}, maximum number of threads is {1}", documentCount, maxThreadCount); } _pbProgress.Minimum = 0; _pbProgress.Maximum = documentCount; _pbProgress.Value = 0; _btnConvertMoreFiles.Enabled = false; _btnCancel.Enabled = true; _lbSuccess.Items.Clear(); if (!loopContinuously) { _lbError.Items.Clear(); } _aborted = false; byte[] docWriterOptions = null; using (MemoryStream ms = new MemoryStream()) { docWriter.SaveOptions(ms); docWriterOptions = ms.ToArray(); } try { _ocrEngine = CreateEngine(_engineType, docWriterOptions, false); } catch (Exception ex) { OnError(ex.Message); OnDone(); return; } ThreadPool.QueueUserWorkItem((object state) => { // Queue up to maxThreadCount of threads a time int sourceFileIndex = 0; int documentLeft = documentCount; while (documentLeft > 0 && !_aborted) { int batchCount = Math.Min(maxThreadCount, documentLeft); _workItemCount = batchCount; ClearQuarantine(); using (_batchFinishedEvent = new AutoResetEvent(false)) { for (int i = 0; i < batchCount; i++) { WorkItemData data = new WorkItemData(); data.DocumentWriterOptions = docWriterOptions; data.EngineType = engineType; data.OcrEngine = _ocrEngine; data.SourceFile = sourceFiles[i + sourceFileIndex]; data.DestinationDirectory = destinationDirectory; data.Format = format; data.FirstTry = true; ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), data); } _batchFinishedEvent.WaitOne(); //anything in the quarantine to retry? List <string> quarantineList = new List <string>(); GetQuarantine(quarantineList); for (int i = 0; i < quarantineList.Count; i++) { WorkItemData data = new WorkItemData(); data.DocumentWriterOptions = docWriterOptions; data.EngineType = engineType; data.OcrEngine = _ocrEngine; data.SourceFile = quarantineList[i]; data.DestinationDirectory = destinationDirectory; data.Format = format; data.FirstTry = false; ThreadProc(data); } sourceFileIndex += batchCount; documentLeft -= batchCount; } } OnDone(); }); }
public void UpdateDocumentWriterOptions() { // Save the options DocumentFormat format = this.SelectedDocumentFormat; Properties.Settings settings = new Properties.Settings(); settings.Format = format.ToString(); // Update the options DocumentOptions documentOptions = _docWriter.GetOptions(format); switch (format) { case DocumentFormat.Pdf: // Update the PDF options { PdfDocumentOptions pdfOptions = documentOptions as PdfDocumentOptions; pdfOptions.DocumentType = (PdfDocumentType)_pdfDocumentTypeComboBox.SelectedItem; pdfOptions.ImageOverText = _pdfImageOverTextCheckBox.Checked; pdfOptions.Linearized = _pdfLinearizedCheckBox.Checked; pdfOptions.PageRestriction = DocumentPageRestriction.Relaxed; // Description options pdfOptions.Title = _pdfOptions.Title; pdfOptions.Subject = _pdfOptions.Subject; pdfOptions.Keywords = _pdfOptions.Keywords; pdfOptions.Author = _pdfOptions.Author; pdfOptions.Creator = _pdfOptions.Creator; pdfOptions.Producer = _pdfOptions.Producer; // Fonts options pdfOptions.FontEmbedMode = _pdfOptions.FontEmbedMode; pdfOptions.Linearized = _pdfOptions.Linearized; // Security options pdfOptions.Protected = _pdfOptions.Protected; if (pdfOptions.Protected) { pdfOptions.UserPassword = _pdfOptions.UserPassword; pdfOptions.OwnerPassword = _pdfOptions.OwnerPassword; pdfOptions.EncryptionMode = _pdfOptions.EncryptionMode; pdfOptions.PrintEnabled = _pdfOptions.PrintEnabled; pdfOptions.HighQualityPrintEnabled = _pdfOptions.HighQualityPrintEnabled; pdfOptions.CopyEnabled = _pdfOptions.CopyEnabled; pdfOptions.EditEnabled = _pdfOptions.EditEnabled; pdfOptions.AnnotationsEnabled = _pdfOptions.AnnotationsEnabled; pdfOptions.AssemblyEnabled = _pdfOptions.AssemblyEnabled; } // Compression options pdfOptions.OneBitImageCompression = _pdfOptions.OneBitImageCompression; pdfOptions.ColoredImageCompression = _pdfOptions.ColoredImageCompression; pdfOptions.QualityFactor = _pdfOptions.QualityFactor; pdfOptions.ImageOverTextSize = _pdfOptions.ImageOverTextSize; pdfOptions.ImageOverTextMode = _pdfOptions.ImageOverTextMode; // Initial View Options pdfOptions.PageModeType = _pdfOptions.PageModeType; pdfOptions.PageLayoutType = _pdfOptions.PageLayoutType; pdfOptions.PageFitType = _pdfOptions.PageFitType; pdfOptions.ZoomPercent = _pdfOptions.ZoomPercent; pdfOptions.InitialPageNumber = _pdfOptions.InitialPageNumber; pdfOptions.FitWindow = _pdfOptions.FitWindow; pdfOptions.CenterWindow = _pdfOptions.CenterWindow; pdfOptions.DisplayDocTitle = _pdfOptions.DisplayDocTitle; pdfOptions.HideMenubar = _pdfOptions.HideMenubar; pdfOptions.HideToolbar = _pdfOptions.HideToolbar; pdfOptions.HideWindowUI = _pdfOptions.HideWindowUI; } break; case DocumentFormat.Doc: // Update the DOC options { DocDocumentOptions docOptions = documentOptions as DocDocumentOptions; docOptions.TextMode = (_cbFramedDoc.Checked) ? DocumentTextMode.Framed : DocumentTextMode.NonFramed; } break; case DocumentFormat.Docx: // Update the DOCX options { DocxDocumentOptions docxOptions = documentOptions as DocxDocumentOptions; docxOptions.TextMode = (_cbFramedDocX.Checked) ? DocumentTextMode.Framed : DocumentTextMode.NonFramed; } break; case DocumentFormat.Rtf: // Update the RTF options { RtfDocumentOptions rtfOptions = documentOptions as RtfDocumentOptions; rtfOptions.TextMode = (_cbFramedRtf.Checked) ? DocumentTextMode.Framed : DocumentTextMode.NonFramed; } break; case DocumentFormat.Html: // Update the HTML options { HtmlDocumentOptions htmlOptions = documentOptions as HtmlDocumentOptions; htmlOptions.FontEmbedMode = (DocumentFontEmbedMode)_htmlEmbedFontModeComboBox.SelectedItem; htmlOptions.UseBackgroundColor = _htmlUseBackgroundColorCheckBox.Checked; htmlOptions.BackgroundColor = ConvertColor(_htmlBackgroundColorValueLabel.BackColor); } break; case DocumentFormat.Text: // Update the TEXT options { TextDocumentOptions textOptions = documentOptions as TextDocumentOptions; textOptions.DocumentType = (TextDocumentType)_textDocumentTypeComboBox.SelectedItem; textOptions.AddPageNumber = _textAddPageNumberCheckBox.Checked; textOptions.AddPageBreak = _textAddPageBreakCheckBox.Checked; textOptions.Formatted = _textFormattedCheckBox.Checked; } break; case DocumentFormat.AltoXml: // Update the DOCX options { AltoXmlDocumentOptions altoXmlOptions = documentOptions as AltoXmlDocumentOptions; altoXmlOptions.FileName = _altoXmlFileNameTextBox.Text; altoXmlOptions.SoftwareCreator = _altoXmlSoftwareCreatorTextBox.Text; altoXmlOptions.SoftwareName = _altoXmlSoftwareNameTextBox.Text; altoXmlOptions.ApplicationDescription = _altoXmlApplicationDescriptionTextBox.Text; altoXmlOptions.Formatted = _altoXmlFormattedCheckBox.Checked; altoXmlOptions.Indentation = _altoXmlIndentationTextBox.Text; altoXmlOptions.Sort = _altoXmlSort.Checked; altoXmlOptions.PlainText = _altoXmlPlainText.Checked; altoXmlOptions.ShowGlyphInfo = _altoXmlShowGlyphInfo.Checked; altoXmlOptions.ShowGlyphVariants = _altoXmlShowGlyphVariants.Checked; altoXmlOptions.MeasurementUnit = (AltoXmlMeasurementUnit)_altoXmlMeasurementUnit.SelectedItem; } break; case DocumentFormat.Emf: case DocumentFormat.Xls: case DocumentFormat.Pub: case DocumentFormat.Mob: case DocumentFormat.Svg: default: // These formats have no options break; } if (documentOptions != null) { _docWriter.SetOptions(format, documentOptions); } using (MemoryStream ms = new MemoryStream()) { _docWriter.SaveOptions(ms); settings.FormatOptionsXml = Encoding.Unicode.GetString(ms.ToArray()); } settings.Save(); }