コード例 #1
0
ファイル: BatchUI.cs プロジェクト: tostercx/mdiconv
        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();
        }
コード例 #2
0
ファイル: MODIHandler.cs プロジェクト: tostercx/mdiconv
        // 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();
            }
        }
コード例 #3
0
ファイル: MODIHandler.cs プロジェクト: tostercx/mdiconv
        // 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();
            }
        }
コード例 #4
0
ファイル: MODIHandler.cs プロジェクト: tostercx/mdiconv
        // 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 { }
        }