How To Create A PowerPoint Presentation Using C#

first in the new project we need to press “Add reference” go to “com” tag and choose the
1)microsoft graph 12.0 object library
2)microsoft PowerPoint 12.0 object library

in your form add:

using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;

second use that code :
(it’s winform with one button)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;



namespace testToCreatePttFileFromImages
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string pictureFileName = "C:\\picToVideo\\2.jpg";

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

            Microsoft.Office.Interop.PowerPoint.Slides slides;
            Microsoft.Office.Interop.PowerPoint._Slide slide;
            Microsoft.Office.Interop.PowerPoint.TextRange objText;

            // 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];

            // Create new Slide
            slides = pptPresentation.Slides;
            slide = slides.AddSlide(1, customLayout);

            // Add title
            objText = slide.Shapes[1].TextFrame.TextRange;
            objText.Text = "The Header Of The Presentation";
            objText.Font.Name = "Arial";
            objText.Font.Size = 32;

            //objText = slide.Shapes[2].TextFrame.TextRange;
            //objText.Text = "this text come after the heading with bullet";

            Microsoft.Office.Interop.PowerPoint.Shape shape = slide.Shapes[2];
            slide.Shapes.AddPicture(pictureFileName, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, shape.Left, shape.Top, shape.Width, shape.Height);

            //slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = "the text is for the notes to specific page";

            pptPresentation.SaveAs(@"c:\picToVideo\fppt.pptx", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue);
            pptPresentation.Close();
            pptApplication.Quit();

        }
    }
}

if you want to see project that i made with that check this link

Advertisements
This entry was posted in כללי and tagged . Bookmark the permalink.

3 Responses to How To Create A PowerPoint Presentation Using C#

  1. janewdaisy says:

    Nice article, sir.

    For those who cannot program with Microsoft.Office.Interop.PowerPoint, I suggest using a free Presentation component to create PowerPoint Slide in C#, the method is pretty easy

    //create PPT document
    Presentation presentation = new Presentation();
    //add new slide
    presentation.Slides.Append();
    //append new shape
    IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 150, 600, 200));
    shape.ShapeStyle.LineColor.Color = Color.White;
    shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;
    //add text to shape
    shape.AppendTextFrame(“The sample demonstrates how to set Animations in PPT using Spire.Presentation.”);
    //add new paragraph
    shape.TextFrame.Paragraphs.Append(new TextParagraph());
    //add text to paragraph
    shape.TextFrame.Paragraphs[1].TextRanges.Append(new TextRange(“Text String”));
    More details – http://www.e-iceblue.com/Tutorials/Spire.Presentation/Program-Guide/Add-new-slide-in-PPT-document.html

    Liked by 1 person

  2. Ugur says:

    Hi,
    How can i add music this example ?

    Thank u
    Uğur

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s