Пример #1
0
        public void createFile(string filepath)
        {
            if (filepath.EndsWith(".xlsx"))
            {
                Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                Workbook ExcelWorkBook = null;
                ExcelApp.Visible       = false;
                ExcelApp.DisplayAlerts = false;

                ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                ExcelWorkBook.SaveAs(filepath);
                ExcelWorkBook.Close();
                ExcelApp.Quit();
            }
            else if (filepath.EndsWith(".pptx"))
            {
                Application pptApplication = new Application();
                // Create the Presentation File
                Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue);
                Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];

                Microsoft.Office.Interop.PowerPoint.Slides slides = pptPresentation.Slides;
                Microsoft.Office.Interop.PowerPoint.Slide  slide  = slides.AddSlide(1, customLayout);
                pptPresentation.SaveAs(filepath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
                pptPresentation.Close();
                pptApplication.Quit();
            }
            else
            {
                File.Create(filepath).Close();
            }
        }
Пример #2
0
        public void CreateNewPPT()
        {
            application        = new PowerPoint.Application();
            pptPresentationSet = application.Presentations;
            pptPresentation    = pptPresentationSet.Open(_fileName, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
            //customLayout = pptPresentationSet.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];
            pptSlides = pptPresentation.Slides;
            pptSlide  = pptSlides.AddSlide(1, customLayout);

            textRng           = pptSlide.Shapes[1].TextFrame.TextRange;
            textRng.Font.Name = textDefaultFontName;
            textRng.Font.Size = textDefaultFontSize;
        }
Пример #3
0
        internal void CreateNewPPT(string title)
        {
            // Create the Presentation File
            pptPresentation = pptApplication.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);

            pptPresentation.PageSetup.SlideSize   = Powerpoint.PpSlideSizeType.ppSlideSizeCustom; //equals 16*9, but also fits the typical DIN A4 Width (excluding DIN A4 Border)
            pptPresentation.PageSetup.SlideWidth  = (float)MathHelper.ConvertCmToPoint(SlideWidthCm);
            pptPresentation.PageSetup.SlideHeight = (float)MathHelper.ConvertCmToPoint(SlideHeightCm);

            pptPresentation.PageSetup.SlideOrientation = Microsoft.Office.Core.MsoOrientation.msoOrientationVertical;
            var customLayout = pptPresentation.SlideMaster.CustomLayouts[7];

            // Create new Slide
            slides = pptPresentation.Slides;
            slide  = slides.AddSlide(1, customLayout);
            //Test
            slide.FollowMasterBackground        = Microsoft.Office.Core.MsoTriState.msoFalse;
            slide.Background.Fill.ForeColor.RGB = 0;
        }
Пример #4
0
        private void createslides()
        {
            Powerpoint.Slides newslides = newpres.Slides;
            string            filename;

            Bitmap tempimage = new Bitmap(directorypathway + "\\1.jpg");

            float outputwidth;
            float outputheight;

            float ratio = tempimage.Width * 1f / tempimage.Height * 1f;

            if (newpres.PageSetup.SlideHeight * ratio > newpres.PageSetup.SlideWidth)
            {
                outputwidth  = newpres.PageSetup.SlideWidth - 10;
                outputheight = outputwidth / ratio;
            }

            else
            {
                outputheight = newpres.PageSetup.SlideHeight - 10;
                outputwidth  = outputheight * ratio;
            }


            for (int i = System.IO.Directory.GetFiles(directorypathway).Length; i >= 1; i--)
            {
                filename = directorypathway + "\\" + i.ToString() + ".jpg";
                newslide = newslides.AddSlide(1, newcustomlayout);
                newslide.Background.Fill.Background();
                newslide.FollowMasterBackground        = MsoTriState.msoFalse;
                newslide.Background.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                newslide.Shapes.AddPicture(filename, MsoTriState.msoFalse, MsoTriState.msoTrue, newpres.PageSetup.SlideWidth * .5f - outputwidth * .5f, newpres.PageSetup.SlideHeight * .5f - outputheight * .5f, outputwidth, outputheight);
            }
            string nametosave = directorypathway + "\\images";

            newpres.SaveAs(nametosave, Powerpoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoFalse);
            //  newpres.Close();
            //  newapp.Quit();
        }
        private void createSlide(ArrayList selectedUrls)
        {
            // Create new Slide
            slides = pptPresentation.Slides;
            slide  = slides.AddSlide(slideNumber, customLayout);
            //increment slide number
            slideNumber++;
            // Add title
            objText           = slide.Shapes[1].TextFrame.TextRange;
            objText.Text      = this.MainWindowTitleText.Text;
            objText.Font.Name = "Arial";
            objText.Font.Size = 32;

            System.Windows.Documents.TextRange textRange = new System.Windows.Documents.TextRange(this.MainWindowSlideText.Document.ContentStart, this.MainWindowSlideText.Document.ContentEnd);

            objText      = slide.Shapes[2].TextFrame.TextRange;
            objText.Text = textRange.Text;

            Microsoft.Office.Interop.PowerPoint.Shape shape = slide.Shapes[2];
            foreach (string url in selectedUrls)
            {
                slide.Shapes.AddPicture(url, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, shape.Left, shape.Top, shape.Width, shape.Height);
            }
        }
Пример #6
0
        /// <summary>
        /// Generates PowerPoint Slide with given Title, Body and selected images
        /// </summary>
        public static string generatePowerPointSlide(string titleText, string bodyText, List <ImageToUse> imagesToPPT)
        {
            string status         = "Success";
            string powerPointPath = System.IO.Path.Combine(Environment.CurrentDirectory, "ppSample.pptx");

            Microsoft.Office.Interop.PowerPoint.Application  pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
            Microsoft.Office.Interop.PowerPoint.Presentation pptPresentation;

            if (File.Exists(powerPointPath))
            {
                // Append to existing PowerPoint
                Microsoft.Office.Interop.PowerPoint.Presentations pres = pptApplication.Presentations;
                pptPresentation = pres.Open(powerPointPath);
            }
            else
            {
                // Create new PowerPoint
                pptPresentation = pptApplication.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue);
            }

            Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout =
                pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText];

            // Acquire slides from presentation
            Microsoft.Office.Interop.PowerPoint.Slides slides = pptPresentation.Slides;

            // Append new slide to end of presentation
            Microsoft.Office.Interop.PowerPoint._Slide slide = slides.AddSlide(slides.Count + 1, customLayout);

            Microsoft.Office.Interop.PowerPoint.TextRange objText;

            // Add title from titleTextArea
            objText           = slide.Shapes[1].TextFrame.TextRange;
            objText.Text      = titleText;
            objText.Font.Name = "Arial";
            objText.Font.Size = 32;

            // Add body from bodyTextArea
            objText           = slide.Shapes[2].TextFrame.TextRange;
            objText.Text      = bodyText;
            objText.Font.Name = "Arial";
            objText.Font.Size = 20;

            Microsoft.Office.Interop.PowerPoint.Shapes shapes = slide.Shapes;
            string imagePath = System.IO.Path.Combine(Environment.CurrentDirectory, "image.jpg");
            int    imageX    = 50;

            // Go through all images, if image was highlighted in grid, post it inside PPT
            foreach (ImageToUse imageToUse in imagesToPPT)
            {
                if (imageToUse.use)
                {
                    // Save all images being used
                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imageToUse.bitmapImage));
                    using (FileStream stream = new FileStream(imagePath, FileMode.Create)) encoder.Save(stream);

                    // Insert all images into PPT
                    shapes.AddPicture(imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, imageX, 350, 100, 100);
                    imageX += 100;

                    // Delete all images when done with them
                    File.Delete(imagePath);
                }
            }

            try
            {
                // Save Power Point
                pptPresentation.SaveAs(powerPointPath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Microsoft.Office.Core.MsoTriState.msoTrue);
            }
            catch (Exception exception)
            {
                status = "Problem while trying to save PowerPoint:\n" + exception.ToString();
            }

            return(status);
            // pptPresentation.Close();
            // pptApplication.Quit();
        }