// 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(); } }
// 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 { } }
// 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(); } }
// 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(); }