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