示例#1
0
        // 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 { }
        }