public static void SaveAsPPTFromImages(string imageFolder, string pptName) { var task = Task.Factory.StartNew(() => { Application pptApp = new Application(); Presentations pptPres = pptApp.Presentations; Presentation pptPre = pptPres.Add(MsoTriState.msoFalse); var imagesName = GetImageFilesPath(imageFolder); for (int i = 0; i < imagesName.Count; i++) { // Slide start from 1 var slide = pptPre.Slides.Add(i + 1, PpSlideLayout.ppLayoutObject); var imageSize = ImageHelper.GetImageSize(imagesName[i]); var slideSize = new Size((int)slide.Master.Width, (int)slide.Master.Height); var scaleSize = FileUtils.GetScaleSize(imageSize, slideSize); AddPictureShape(slide, imagesName[i], scaleSize); } // Save it as a ppt file pptPre.SaveAs(pptName, PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoCTrue); }); task.Wait(); }
public static void createPresentation(List <KeyValuePair <string, float> > titles) { Microsoft.Office.Interop.PowerPoint.Application oPowerPoint = new Microsoft.Office.Interop.PowerPoint.Application(); oPowerPoint.Visible = Microsoft.Office.Core.MsoTriState.msoTrue; System.Console.WriteLine("PowerPoint application created."); Presentations oPres = oPowerPoint.Presentations; Presentation oPre = oPres.Add(MsoTriState.msoTrue); Slides oSlides = oPre.Slides; int nr = 1; foreach (var title in titles) { //Console.WriteLine(title.Key+" - "+ title.Value); createNewSlide(oSlides, nr, title.Key, title.Value); nr += 1; } Console.WriteLine("Checking for three lines..."); TwoLines.twoLinesFilter(oSlides); //additionally to avoid three lines Console.WriteLine("Additionally checking for three lines..."); TwoLines.twoLinesFilter(oSlides); FilesController.saveSlides(oPre); //oPre.Close(); //oPowerPoint.Quit(); //System.Console.WriteLine("PowerPoint application quitted."); //Clean up the unmanaged COM resource. if (oSlides != null) { Marshal.FinalReleaseComObject(oSlides); oSlides = null; } if (oPre != null) { Marshal.FinalReleaseComObject(oPre); oPre = null; } if (oPres != null) { Marshal.FinalReleaseComObject(oPres); oPres = null; } if (oPowerPoint != null) { Marshal.FinalReleaseComObject(oPowerPoint); oPowerPoint = null; } }
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); } }
public override void Initialize() { // all views in the assembly decorated with // ViewContainerInitializerAttribute or // ViewModelContainerInitializerAttribute.cs // are loaded into the container in base.Initialize base.Initialize(); this.Container.RegisterType(typeof(IBusinessService), typeof(BusinessService), new ContainerControlledLifetimeManager()); this.Container.RegisterType(typeof(IDialogService), typeof(DialogService)); _presentations.Add(new Presentation("Introduction", typeof(IntroductionView).FullName)); _presentations.Add(new Presentation("What Is XAML", typeof(WhatIsXamlView).FullName)); _presentations.Add(new Presentation("Why Use XAML", typeof(WhyUseXamlView).FullName)); _presentations.Add(new Presentation("XAML Is Concise", typeof(XamlIsConciseView).FullName)); _presentations.Add(new Presentation("XAML Is Composable", typeof(XamlIsComposableView).FullName)); _presentations.Add(new Presentation("XAML Is Styleable", typeof(XamlIsStyleableView).FullName)); _presentations.Add(new Presentation("XAML Controls Are Lookless", typeof(XamlControlsAreLooklessView).FullName)); _presentations.Add(new Presentation("XAML Is Themeable", typeof(XamlIsThemeableView).FullName)); _presentations.Add(new Presentation("XAML Has Multimedia Capabilities", typeof(XamlHasMultiMediaCapabilitiesView).FullName)); _presentations.Add(new Presentation("XAML Has Rich Data Binding API's", typeof(XamlHasRichDataBindingStackView).FullName)); _presentations.Add(new Presentation("XAML DataContext Is Inheritable", typeof(XamlDataContentIsInheritedView).FullName)); _presentations.Add(new Presentation("XAML Enables M-V-VM", typeof(XamlMVVMView).FullName)); _presentations.Add(new Presentation("XAML M-V-VM Demo", typeof(XamlMVVMDemoCodeView).FullName)); }