예제 #1
0
        public static new int Convert(String inputFile, String outputFile, Hashtable options, ref List <PDFBookmark> bookmarks)
        {
            // Check for password protection
            if (Converter.IsPasswordProtected(inputFile))
            {
                Console.WriteLine("Unable to open password protected file");
                return((int)ExitCode.PasswordFailure);
            }

            Boolean running = (Boolean)options["noquit"];

            try
            {
                Microsoft.Office.Interop.PowerPoint.Application   app = null;
                Microsoft.Office.Interop.PowerPoint.Presentation  activePresentation = null;
                Microsoft.Office.Interop.PowerPoint.Presentations presentations      = null;
                try
                {
                    try
                    {
                        app = (Microsoft.Office.Interop.PowerPoint.Application)Marshal.GetActiveObject("PowerPoint.Application");
                    }
                    catch (System.Exception)
                    {
                        int tries = 10;
                        app     = new Microsoft.Office.Interop.PowerPoint.Application();
                        running = false;
                        while (tries > 0)
                        {
                            try
                            {
                                // Try to set a property on the object
                                app.DisplayAlerts = PpAlertLevel.ppAlertsNone;
                            }
                            catch (COMException)
                            {
                                // Decrement the number of tries and have a bit of a snooze
                                tries--;
                                Thread.Sleep(500);
                                continue;
                            }
                            // Looks ok, so bail out of the loop
                            break;
                        }
                        if (tries == 0)
                        {
                            Converter.releaseCOMObject(app);
                            return((int)ExitCode.ApplicationError);
                        }
                    }
                    MSCore.MsoTriState nowrite = (Boolean)options["readonly"] ? MSCore.MsoTriState.msoTrue : MSCore.MsoTriState.msoFalse;
                    bool pdfa = (Boolean)options["pdfa"] ? true : false;
                    if ((Boolean)options["hidden"])
                    {
                        // Can't really hide the window, so at least minimise it
                        app.WindowState = PpWindowState.ppWindowMinimized;
                    }
                    PpFixedFormatIntent quality = PpFixedFormatIntent.ppFixedFormatIntentScreen;
                    if ((Boolean)options["print"])
                    {
                        quality = PpFixedFormatIntent.ppFixedFormatIntentPrint;
                    }
                    if ((Boolean)options["screen"])
                    {
                        quality = PpFixedFormatIntent.ppFixedFormatIntentScreen;
                    }
                    Boolean includeProps = !(Boolean)options["excludeprops"];
                    Boolean includeTags  = !(Boolean)options["excludetags"];
                    app.FeatureInstall = MSCore.MsoFeatureInstall.msoFeatureInstallNone;
                    app.DisplayDocumentInformationPanel = false;
                    app.DisplayAlerts        = PpAlertLevel.ppAlertsNone;
                    app.Visible              = MSCore.MsoTriState.msoTrue;
                    app.AutomationSecurity   = MSCore.MsoAutomationSecurity.msoAutomationSecurityLow;
                    presentations            = app.Presentations;
                    activePresentation       = presentations.Open2007(inputFile, nowrite, MSCore.MsoTriState.msoTrue, MSCore.MsoTriState.msoTrue, MSCore.MsoTriState.msoTrue);
                    activePresentation.Final = false;

                    // Sometimes, presentations can have restrictions on them that block
                    // access to the object model (e.g. fonts containing restrictions).
                    // If we attempt to access the object model and fail, then try a more
                    // sneaky method of getting the presentation - create an empty presentation
                    // and insert the slides from the original file.
                    var fonts = activePresentation.Fonts;
                    try
                    {
                        var fontCount = fonts.Count;
                    }
                    catch (System.Runtime.InteropServices.COMException)
                    {
                        Converter.releaseCOMObject(fonts);
                        // This presentation looked read-only
                        activePresentation.Close();
                        Converter.releaseCOMObject(activePresentation);
                        // Create a new blank presentation and insert slides from the original
                        activePresentation = presentations.Add(MSCore.MsoTriState.msoFalse);
                        // This is only a band-aid - backgrounds won't come through
                        activePresentation.Slides.InsertFromFile(inputFile, 0);
                    }
                    Converter.releaseCOMObject(fonts);
                    activePresentation.ExportAsFixedFormat(outputFile, PpFixedFormatType.ppFixedFormatTypePDF, quality, MSCore.MsoTriState.msoFalse, PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PpPrintOutputType.ppPrintOutputSlides, MSCore.MsoTriState.msoFalse, null, PpPrintRangeType.ppPrintAll, "", includeProps, true, includeTags, true, pdfa, Type.Missing);

                    // Determine if we need to make bookmarks
                    if ((bool)options["bookmarks"])
                    {
                        loadBookmarks(activePresentation, ref bookmarks);
                    }
                    activePresentation.Saved = MSCore.MsoTriState.msoTrue;
                    activePresentation.Close();

                    return((int)ExitCode.Success);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return((int)ExitCode.UnknownError);
                }
                finally
                {
                    Converter.releaseCOMObject(activePresentation);
                    Converter.releaseCOMObject(presentations);

                    if (app != null && !running)
                    {
                        app.Quit();
                    }
                    Converter.releaseCOMObject(app);
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                Console.WriteLine(e.Message);
                return((int)ExitCode.UnknownError);
            }
        }