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