/// <summary> /// Initializes a new instance of the <see cref="EditToolsForm"/> class, styles all buttons and sets list of fonts. /// </summary> /// <param name="shapes">The shapes.</param> public EditToolsForm(RangeOfShapes shapes) { InitializeComponent(); this.shapes = shapes; MakeBoldButton.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Bold); MakeBoldButton.FlatAppearance.BorderColor = Color.White; MakeItalicButton.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Italic); MakeItalicButton.FlatAppearance.BorderColor = Color.White; MakeUnderlineButton.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Underline); MakeUnderlineButton.FlatAppearance.BorderColor = Color.White; MakeStrikeButton.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Strikeout); MakeStrikeButton.FlatAppearance.BorderColor = Color.White; UnformatTextButton.FlatAppearance.BorderColor = Color.White; List <string> fonts = new List <string>(); foreach (FontFamily font in System.Drawing.FontFamily.Families) { fonts.Add(font.Name); } ListOfFonts.Items.AddRange(fonts.ToArray()); flagBold = Office.MsoTriState.msoFalse; flagUnderline = Office.MsoTriState.msoFalse; flagStrike = Office.MsoTriState.msoFalse; flagItalic = Office.MsoTriState.msoFalse; }
/// <summary> /// Executes a shell command synchronously. /// </summary> /// <param name="command">string command</param> /// <returns>string, as output of the command.</returns> public static void OpenPowerPoint(string file) { Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application(); Microsoft.Office.Core.MsoTriState ofalse = Microsoft.Office.Core.MsoTriState.msoFalse; Microsoft.Office.Core.MsoTriState otrue = Microsoft.Office.Core.MsoTriState.msoTrue; pptApp.Visible = otrue; pptApp.Activate(); Microsoft.Office.Interop.PowerPoint.Presentations ps = pptApp.Presentations; Microsoft.Office.Interop.PowerPoint.Presentation p = ps.Open(file, ofalse, ofalse, otrue); System.Diagnostics.Debug.Print(p.Windows.Count.ToString()); MessageBox.Show(pptApp.ActiveWindow.Caption); }
/// <summary> /// Create the current argument in MS PowerPoint using COM /// </summary> /// <param name="a"></param> public void outputToPowerPoint(Argument a) { Node n; n = a.findHead(); // find main premise node a.referenceTree(); // reference tree index = 1; PPapp = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); object missing = System.Reflection.Missing.Value; Microsoft.Office.Core.MsoTriState mts = MsoTriState.msoTrue; PPapp.Presentations.Add(mts); PPapp.Visible = mts; doNode(n); // release the COM object. GC.Collect(); would be overkill System.Runtime.InteropServices.Marshal.ReleaseComObject(PPapp); }
/// <summary> /// Handles the Click event of the MakeStrikeButton control, makes font striked. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void MakeStrikeButton_Click(object sender, EventArgs e) { if (flagStrike == Office.MsoTriState.msoFalse) { flagStrike = Office.MsoTriState.msoTrue; } else { flagStrike = Office.MsoTriState.msoFalse; } foreach (PowerPoint.Shape shape in shapes) { try { shape.TextFrame2.TextRange.Font.StrikeThrough = flagStrike; } catch { continue; } } }
/// <summary> /// Handles the Click event of the MakeItalicButton control, formats text to italic. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void MakeItalicButton_Click(object sender, EventArgs e) { if (flagItalic == Office.MsoTriState.msoFalse) { flagItalic = Office.MsoTriState.msoTrue; } else { flagItalic = Office.MsoTriState.msoFalse; } foreach (PowerPoint.Shape shape in shapes) { try { shape.TextFrame.TextRange.Font.Italic = flagItalic; } catch { continue; } } }
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); } }
public static bool ToBool(this Office.MsoTriState state) => state == Office.MsoTriState.msoTrue;
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); } }