void Application_PresentationSave(PowerPoint.Presentation Prs) { Prs.ApplyTheme( @"C:\Program Files\Microsoft Office\root\Document Themes 16\Wisp.thmx"); PowerPoint.CustomLayout pptLayout = Prs.Slides[1].CustomLayout; Prs.Slides.AddSlide(1, pptLayout); Prs.RemovePersonalInformation = Office.MsoTriState.msoTrue; }
private void buttonCreateSlides_Click(object sender, EventArgs e) { DataRowView fromBook = this.selectBookFrom.SelectedItem as DataRowView; DataRowView fromChap = this.selectChapterFrom.SelectedItem as DataRowView; DataRowView fromVerse = this.selectVerseFrom.SelectedItem as DataRowView; string fB = fromBook.Row["book_number"].ToString(); string fC = fromChap.Row["chapter"].ToString(); string fV = fromVerse.Row["verse"].ToString(); DataRowView toBook = this.selectBookTo.SelectedItem as DataRowView; DataRowView toChap = this.selectChapterTo.SelectedItem as DataRowView; DataRowView toVerse = this.selectVerseTo.SelectedItem as DataRowView; string tB = toBook.Row["book_number"].ToString(); string tC = toChap.Row["chapter"].ToString(); string tV = toVerse.Row["verse"].ToString(); if (Int32.Parse(tB) < Int32.Parse(fB)) { MessageBox.Show("You selected a book in the To part that is before the one in the from :-/ Try again"); return; } if ((Int32.Parse(tB) == Int32.Parse(fB)) && (Int32.Parse(tC) < Int32.Parse(fC))) { MessageBox.Show("You selected a chapter in the To part that is before the one in the from :-/ Try again"); return; } if ((Int32.Parse(tB) < Int32.Parse(fB)) && (Int32.Parse(tC) < Int32.Parse(fC))) { MessageBox.Show("You selected a chapter or book in the To part that is before the one in the from :-/ Try again"); return; } if ((Int32.Parse(tB) == Int32.Parse(fB)) && (Int32.Parse(tC) == Int32.Parse(fC)) && (Int32.Parse(tV) < Int32.Parse(fV))) { MessageBox.Show("You selected a verse in the To part that is before the one in the from :-/ Try again"); return; } Queue <string> pagesOfVerses = new Queue <string>(); string currentPage = ""; int currentPageCharacterCount = 0; int maxCharactersOnAPage = 350; //get verses.. check there aren't too many using (SQLiteConnection conn = new SQLiteConnection(connString)) { try { string query = "SELECT text from verses WHERE absoluteVerseNumber >= " + "(SELECT absoluteVerseNumber from verses where book = '" + fB + "' AND chapter = '" + fC + "' AND verse = '" + fV + "')" + "AND absoluteVerseNumber <= (SELECT absoluteVerseNumber from verses where book = '" + tB + "' AND chapter = '" + tC + "' AND verse = '" + tV + "')" + "order by absoluteVerseNumber;"; SQLiteDataAdapter da = new SQLiteDataAdapter(query, conn); conn.Open(); DataSet ds = new DataSet(); da.Fill(ds, "Verses"); var allVerses = ds.Tables[0].AsEnumerable().Select(r => r.Field <string>("text")).ToList(); if (allVerses.Count > 100) { MessageBox.Show("Whoa there! You selected waaay tooo many verses.. over 100!! Try again, I'm not working this hard"); return; } foreach (string s in allVerses) { string better = s.Replace("; ; ; ;", ""); better = better.Trim(); better = s.Replace("<pb />", "\n"); better = s.Replace("<pb/>", "\n"); if ((currentPageCharacterCount + better.Length) < maxCharactersOnAPage) { currentPageCharacterCount = currentPageCharacterCount + better.Length; currentPage = currentPage + " " + better; } else { pagesOfVerses.Enqueue(currentPage); currentPage = better; currentPageCharacterCount = better.Length; } } pagesOfVerses.Enqueue(currentPage); } catch (Exception ex) { MessageBox.Show(ex.Message); } } //create slides // Create an instance of Microsoft PowerPoint PowerPoint.Application oPowerPoint = new PowerPoint.Application(); // Create a new Presentation PowerPoint.Presentation oPre = oPowerPoint.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue); oPre.ApplyTheme(this.templateFile); oPre.PageSetup.SlideSize = PowerPoint.PpSlideSizeType.ppSlideSizeOnScreen16x9; PowerPoint.CustomLayout titleMasterSlide = oPre.SlideMaster.CustomLayouts[Enums.SlidesMaster.ChromakeyScriptureTitle]; PowerPoint.CustomLayout verseSlide = oPre.SlideMaster.CustomLayouts[Enums.SlidesMaster.WhiteScriptureText]; //[PowerPoint.PpSlideLayout.ppLayoutBlank]; // Insert a new Slide and add some text to it. int countSlides = 1; //Insert the title PowerPoint.Slide titleSlide = oPre.Slides.AddSlide(countSlides, titleMasterSlide); string scriptureRef = fromBook.Row["long_name"].ToString() + " " + fC + ":" + fV + " - " + toBook.Row["long_name"].ToString() + " " + tC + ":" + tV; if (fromBook.Row["long_name"].ToString() == toBook.Row["long_name"].ToString()) { scriptureRef = fromBook.Row["long_name"].ToString() + " " + fC + ":" + fV + " - " + " " + tC + ":" + tV; } titleSlide.Shapes[1].TextFrame.TextRange.Text = scriptureRef; countSlides++; foreach (string aPage in pagesOfVerses) { PowerPoint.Slide oSlide = oPre.Slides.AddSlide(countSlides, verseSlide); oSlide.Shapes[1].TextFrame2.AutoSize = Office.MsoAutoSize.msoAutoSizeTextToFitShape; oSlide.Shapes[1].TextFrame2.TextRange.ParagraphFormat.SpaceAfter = 6; oSlide.Shapes[1].TextFrame2.TextRange.ParagraphFormat.SpaceBefore = 0; oSlide.Shapes[1].TextFrame2.TextRange.Paragraphs.ParagraphFormat.SpaceAfter = 6; oSlide.Shapes[1].TextFrame2.TextRange.Paragraphs.ParagraphFormat.SpaceBefore = 0; oSlide.Shapes[1].TextFrame2.TextRange.Text = aPage; countSlides++; } }
private async void Button_Click(object sender, RoutedEventArgs e) { try { ((Button)sender).IsEnabled = false; // Ouverture de Powerpoint, et création d'une nouvelle présentation app = new PowerPoint.Application(); pptx = app.Presentations.Add(Office.MsoTriState.msoTrue); // msoFalse permet de travailler en arrière plan sans afficher Office // On applique un thème string themePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase), "Circuit.thmx"); pptx.ApplyTheme(themePath); // Les 2 layouts de slides qu'on utilise var titleLayout = pptx.SlideMaster.CustomLayouts[1]; var contentLayout = pptx.SlideMaster.CustomLayouts[2]; // On parcours le texte pour créer les slides var content = pptcontent.Text; StringReader reader = new StringReader(content); int currentIndex = 1; string line; PowerPoint.Slide slide = null; while ((line = reader.ReadLine()) != null) { if (string.IsNullOrWhiteSpace(line)) { continue; } if (line.StartsWith("= ")) { // Slide de titre slide = pptx.Slides.AddSlide(currentIndex++, titleLayout); slide.Shapes[1].TextFrame.TextRange.Text = line.Substring(2); } else if (line.StartsWith("# ")) { // Le délais n'est là que pour ajouter un effet dramatique pendant la démo, enlevez le pour aller plus vite await Task.Delay(150); // Nouvelle slide de contenu slide = pptx.Slides.AddSlide(currentIndex++, contentLayout); slide.Shapes[1].TextFrame.TextRange.Text = line.Substring(2); } else { // On ajoute la ligne à la dernière slide slide.Shapes[2].TextFrame.TextRange.Text += line + Environment.NewLine; } } // Sauvegarde du document // pptx.SaveAs(@"C:\Temp\slides.pptx"); } finally { // Ici je garde le document ouvert pour la démo, // normalement on pense toujours à fermer Office //if (pptx != null) pptx.Close(); //if (app != null) app.Quit(); ((Button)sender).IsEnabled = true; } }