public void DoOCRAsync(WaitForm wait) { if (!isOpen) return; Thread thread = new Thread(() => DoOCR(this, wait)); thread.Start(); }
private void converter() { foreach (string file in fileList.Items) { WaitForm wait = new WaitForm(); wait.StartPosition = FormStartPosition.CenterParent; wait.Status = "Converting " + Path.GetFileName(file) + "..."; if (mdi.Open(file)) { string saveTo = ""; List<int> pages = (List<int>)Enumerable.Range(0, mdi.Doc.Images.Count).ToList(); if (toFolder.Checked) saveTo = outDir.Text + "\\"; else if (toOrigin.Checked) saveTo = Path.GetDirectoryName(file) + "\\"; saveTo += Path.GetFileNameWithoutExtension(file); saveTo += "." + formatBox.SelectedItem.ToString().ToLower(); if (formatBox.SelectedItem.ToString() == "TIFF") { mdi.SaveToDocAsync(pages, saveTo, MODI.MiFILE_FORMAT.miFILE_FORMAT_TIFF, wait); wait.ShowDialog(); } else if (formatBox.SelectedItem.ToString() == "PDF") { mdi.SaveToPDFAsync(pages, saveTo, wait); wait.ShowDialog(); } } else wait.MessageSafe("Unable to load " + file); } //wait.CloseSafe(); }
// Converting private void Save(IList<int> indices) { if (!mdi.IsOpen) { MessageBox.Show("You must open a file first!", title); return; } if(indices.Count < 1) { MessageBox.Show("No pages selected!", title); return; } // default to: filename.jpg saveFileDialog.FilterIndex = (int)fileType.FT_JPG + 1; // filter index starts from 1 saveFileDialog.FileName = Path.GetFileNameWithoutExtension(mdi.FileName) + ".jpg"; // default dir to where it was opened saveFileDialog.InitialDirectory = Path.GetDirectoryName(mdi.FileName); // show the save dialog DialogResult result = saveFileDialog.ShowDialog(); if (result == DialogResult.OK) { // filter index starts from 1 ... fileType type = (fileType)saveFileDialog.FilterIndex - 1; // process per page image conv (jpg, png, bmp...) if(imageFormats.ContainsKey(type)) { WaitForm wait = new WaitForm(); wait.StartPosition = FormStartPosition.CenterParent; mdi.SaveToImagesAsync(indices, saveFileDialog.FileName, imageFormats[type], wait); wait.ShowDialog(); } // process doc formats handled by MODI (mdi, tiff) else if (documentFormats.ContainsKey(type)) { WaitForm wait = new WaitForm(); wait.StartPosition = FormStartPosition.CenterParent; mdi.SaveToDocAsync(indices, saveFileDialog.FileName, documentFormats[type], wait); wait.ShowDialog(); } // plaintext else if(type == fileType.FT_TXT) { if (!mdi.IsOCRDone) DoOCR(); mdi.SaveToTxt(indices, saveFileDialog.FileName); } // PDF else if (type == fileType.FT_PDF) { WaitForm wait = new WaitForm(); wait.StartPosition = FormStartPosition.CenterParent; mdi.SaveToPDFAsync(indices, saveFileDialog.FileName, wait); wait.ShowDialog(); } } }
// Search private void DoOCR() { if (!mdi.IsOpen) return; if (mdi.IsOCRDone) return; WaitForm wait = new WaitForm(); wait.StartPosition = FormStartPosition.CenterParent; mdi.DoOCRAsync(wait); wait.ShowDialog(); // OCR might auto-rotate pages so re-render RenderThumbs(); }
// Output to images private static void SaveToImages(AxMODI.AxMiDocView docView, IList<int> pages, string outFileName, ImageFormat format, WaitForm wait) { try { // MODI.Document.Images[].Picture is broken... // It's supposed to be an IPictureDisp but it's members aren't usable // Current workaround involves making a copy off an invisible docView // For some reason a new MiDocView created locally doesnt't sem to work... wait.Status = "Converting images..."; foreach(int page in pages) { string curFile = Path.GetFileNameWithoutExtension(outFileName) + " " + (page + 1).ToString("D3") + Path.GetExtension(outFileName); wait.Status = "Saving " + curFile + "..."; string fullFileName = Path.GetDirectoryName(outFileName) + "\\" + curFile; docView.SelectAll(page); stdole.IPictureDisp pic = null; wait.Invoke((System.Windows.Forms.MethodInvoker)delegate { pic = docView.ImageSelection.ExportToPicture(); }); if(pic != null) { Image img = Image.FromHbitmap((IntPtr)pic.Handle, (IntPtr)pic.hPal); img.Save(fullFileName, format); } } wait.CloseSafe(); } catch { wait.MessageSafe("Conversion failed."); wait.CloseSafe(); } }
// Save to PDF // This is extremely slow.... private static void SaveToPDF(AxMODI.AxMiDocView docView, IList<int> pages, string outFileName, WaitForm wait, bool closeForm = true) { try { // MODI.Document.Images[].Picture is broken... // It's supposed to be an IPictureDisp but it's members aren't usable // Current workaround involves making a copy off an invisible docView // For some reason a new MiDocView created locally doesnt't sem to work... PdfDocument document = new PdfDocument(); wait.Status = "Converting document..."; foreach (int page in pages) { wait.Status = "Saving " + Path.GetFileName(outFileName) + " page " + (page + 1).ToString() + "..."; docView.SelectAll(page); stdole.IPictureDisp pic = null; wait.Invoke((System.Windows.Forms.MethodInvoker)delegate { pic = docView.ImageSelection.ExportToPicture(); }); if (pic != null) { PdfPage pdfPage = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(pdfPage); Image img = Image.FromHbitmap((IntPtr)pic.Handle, (IntPtr)pic.hPal); XImage ximg = XImage.FromGdiPlusImage(img); pdfPage.Width = ximg.PointWidth; pdfPage.Height = ximg.PointHeight; gfx.DrawImage(ximg, 0, 0); } } document.Save(outFileName); if (closeForm) wait.CloseSafe(); } catch { wait.MessageSafe("Conversion failed."); if (closeForm) wait.CloseSafe(); } }
// OCR private static void DoOCR(MODIHandler hand, WaitForm wait) { /* wait.Status = "Running OCR..."; try { // process all pages //hand.doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH); // process one by one to show progress int page = 1; foreach (MODI.IImage iimg in hand.doc.Images) { wait.Status = "Running OCR on page " + page.ToString() + "..."; iimg.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH); page++; } hand.isOCRDone = true; } catch { hand.isOCRDone = false; }*/ hand.isOCRDone = true; wait.CloseSafe(); }
// Save to MDI / TIFF private static void SaveToDoc(IList<int> pages, string modiFile, string outFile, MODI.MiFILE_FORMAT format, WaitForm wait, string tmpDir, bool closeForm=true) { MODI.Document modiDoc = null; try { wait.Status = "Saving to " + Path.GetFileName(outFile) + "..."; modiDoc = new MODI.Document(); modiDoc.Create(modiFile); // Can't find a way to add images to an empty doc... // doc.Images.Add doesn't like images from other docs // Probably the same bug that doesn't allow access to Image.Picture // This works backwards and removes unselected images from a complete doc for (int page = modiDoc.Images.Count - 1; page >= 0; page--) { if (!pages.Contains(page)) modiDoc.Images.Remove(modiDoc.Images[page]); } modiDoc.SaveAs(outFile, format); //modiDoc.Close(false); // closing still keeps the file locked... see below } catch { wait.MessageSafe("Failed to save " + Path.GetFileName(outFile)); if(closeForm) wait.CloseSafe(); } try { // The file gets locked for some reason until the program exits // or another save is made elsewhere // This unlocks the file on 2 of the 3 machines I tested on ... while (modiDoc.Images.Count > 1) // can't save w/ 0 imgs - leave 1 modiDoc.Images.Remove(modiDoc.Images[0]); // Needed new files for batch processing :/ This. Is. Ugly. // Not to mention slow if (!Directory.Exists(tmpDir)) Directory.CreateDirectory(tmpDir); modiDoc.SaveAs(tmpDir+"\\"+Path.GetRandomFileName()); modiDoc.Close(); if(closeForm) wait.CloseSafe(); } catch { } }
public void SaveToPDFAsync(IList<int> pages, string outFileName) { WaitForm dummy = new WaitForm(); SaveToPDFAsync(pages, outFileName, dummy); }
public void SaveToPDFAsync(IList<int> pages, string outFileName, WaitForm wait) { if (!isOpen) return; if (converter == null) return; // load it here to allow reusing a single view control converter.Document = doc; Thread thread = new Thread(() => SaveToPDF(converter, pages, outFileName, wait)); thread.Start(); }
public void SaveToImagesAsync(IList<int> pages, string outFileName, ImageFormat format) { WaitForm dummy = new WaitForm(); SaveToImagesAsync(pages, outFileName, format, dummy); }
public void SaveToDocAsync(IList<int> pages, string outFile, MODI.MiFILE_FORMAT format) { WaitForm dummy = new WaitForm(); SaveToDocAsync(pages, outFile, format, dummy); }
public void SaveToDocAsync(IList<int> pages, string outFile, MODI.MiFILE_FORMAT format, WaitForm wait) { if (!isOpen) return; doc.Save(); Thread thread = new Thread(() => SaveToDoc(pages, fileName, outFile, format, wait, tmpDir)); thread.Start(); }