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);
            }
        }
示例#2
0
        public static new int Convert(String inputFile, String outputFile, Hashtable options, ref List <PDFBookmark> bookmarks)
        {
            // Check for password protection
            if (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;
                Presentation  activePresentation = null;
                Presentations presentations      = null;
                try
                {
                    try
                    {
                        app = (Microsoft.Office.Interop.PowerPoint.Application)Marshal.GetActiveObject("PowerPoint.Application");
                    }
                    catch (System.Exception)
                    {
                        int tries = 10;
                        // Create the application
                        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)
                        {
                            ReleaseCOMObject(app);
                            return((int)ExitCode.ApplicationError);
                        }
                    }
                    Boolean            includeProps = !(Boolean)options["excludeprops"];
                    Boolean            includeTags  = !(Boolean)options["excludetags"];
                    PpPrintOutputType  printType    = PpPrintOutputType.ppPrintOutputSlides;
                    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;
                    }
                    if (!String.IsNullOrWhiteSpace((String)options["powerpoint_output"]))
                    {
                        bool printIsValid = false;
                        printType = GetOutputType((String)options["powerpoint_output"], ref printIsValid);
                    }

                    // Powerpoint files can be protected by a write password, but there's no way
                    // of opening them in
                    if (nowrite == MSCore.MsoTriState.msoFalse && IsReadOnlyEnforced(inputFile))
                    {
                        // Seems like PowerPoint interop will ignore the read-only option
                        // when it is opening a document with a write password and still pop
                        // up a password input dialog. To prevent this freezing, don't open
                        // the file
                        throw new Exception("Presentation has a write password - this prevents it being opened");
                    }

                    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;
                    String filenameWithPasswords = inputFile;
                    if (!String.IsNullOrWhiteSpace((string)options["password"]) ||
                        !String.IsNullOrWhiteSpace((string)options["writepassword"]))
                    {
                        // seems we can use the passwords by appending them to the file name!
                        filenameWithPasswords = String.Format("{0}::{1}::{2}", inputFile,
                                                              (String.IsNullOrEmpty((string)options["password"]) ? "" : (string)options["password"]),
                                                              (String.IsNullOrEmpty((string)options["writepassword"]) ? "" : (string)options["writepassword"]));
                        Console.WriteLine(filenameWithPasswords);
                    }
                    activePresentation       = presentations.Open2007(FileName: filenameWithPasswords, ReadOnly: nowrite, Untitled: MSCore.MsoTriState.msoTrue, OpenAndRepair: 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 (COMException)
                    {
                        ReleaseCOMObject(fonts);
                        // This presentation looked read-only
                        ClosePowerPointPresentation(activePresentation);
                        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);
                    }
                    ReleaseCOMObject(fonts);

                    // Set up a delegate function for times we want to print
                    PrintDocument printFunc = delegate(string destination, string printer)
                    {
                        PrintOptions activePrintOptions = activePresentation.PrintOptions;
                        activePrintOptions.PrintInBackground = MSCore.MsoTriState.msoFalse;
                        activePrintOptions.ActivePrinter     = printer;
                        activePrintOptions.PrintInBackground = MSCore.MsoTriState.msoFalse;
                        activePresentation.PrintOut(PrintToFile: destination, Copies: 1);
                        ReleaseCOMObject(activePrintOptions);
                    };

                    if (String.IsNullOrEmpty((string)options["printer"]))
                    {
                        try
                        {
                            activePresentation.ExportAsFixedFormat(outputFile, PpFixedFormatType.ppFixedFormatTypePDF, quality, MSCore.MsoTriState.msoFalse, PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, printType, MSCore.MsoTriState.msoFalse, null, PpPrintRangeType.ppPrintAll, "", includeProps, true, includeTags, true, pdfa, Type.Missing);
                        }
                        catch (Exception) {
                            if (!String.IsNullOrEmpty((string)options["fallback_printer"]))
                            {
                                PrintToGhostscript((string)options["fallback_printer"], outputFile, printFunc);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    else
                    {
                        // Print via a delegate
                        PrintToGhostscript((string)options["printer"], outputFile, printFunc);
                    }

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

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

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

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            catch (COMException e)
            {
                Console.WriteLine(e.Message);
                return((int)ExitCode.UnknownError);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return((int)ExitCode.UnknownError);
            }
        }
示例#3
0
 public void ExportAsFixedFormat(string Path, PpFixedFormatType FixedFormatType, PpFixedFormatIntent Intent, Microsoft.Office.Core.MsoTriState FrameSlides, PpPrintHandoutOrder HandoutOrder, PpPrintOutputType OutputType, Microsoft.Office.Core.MsoTriState PrintHiddenSlides, PrintRange PrintRange, PpPrintRangeType RangeType, string SlideShowName, bool IncludeDocProperties, bool KeepIRMSettings, bool DocStructureTags, bool BitmapMissingFonts, bool UseISO19005_1, [System.Runtime.InteropServices.OptionalAttribute] object ExternalExporter)
 {
     throw new NotImplementedException();
 }
示例#4
0
 public void ExportAsFixedFormat(string Path, PpFixedFormatType FixedFormatType, PpFixedFormatIntent Intent, Microsoft.Office.Core.MsoTriState FrameSlides, PpPrintHandoutOrder HandoutOrder, PpPrintOutputType OutputType, Microsoft.Office.Core.MsoTriState PrintHiddenSlides, PrintRange PrintRange, PpPrintRangeType RangeType, string SlideShowName, bool IncludeDocProperties, bool KeepIRMSettings, bool DocStructureTags, bool BitmapMissingFonts, bool UseISO19005_1, [System.Runtime.InteropServices.OptionalAttribute]object ExternalExporter)
 {
     throw new NotImplementedException();
 }