C#/VB.NET: Create a PowerPoint Document

Tuesday, 25 January 2022 07:43 Written by  support iceblue
Rate this item
(0 votes)

PowerPoint is a presentation document that is typically used for product introductions, performance reports, teaching, and other purposes. Since the design of PowerPoint is a visual behavior and needs constant fine-tuning, it is not recommended to create PowerPoint from scratch programmatically. But if you do have the requirement to create PowerPoint documents in C# or VB.NET, you can try this solution provided by Spire.Presentation for .NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

Create a Simple PowerPoint Document

Spire.Presentation for .NET offers the Presentation class and the ISlide interface to represent a PowerPoint document and a slide respectively. It is quite straightforward and simple for developers to use the properties and methods under them to create or manipulate PowerPoint files. The following are the steps to generate a simple PowerPoint document using it.

  • Create a Presentation object, and set the slide size type to screen 16x9 through the Presentation.SlideSize.Type property.
  • Get the first slide through the Presentation.Slides[] property.
  • Set the background image of the slide using ISlide.SlideBackground property.
  • Add a rectangle to the slide using ISlide.Shapes.AppendShape() method, positioning the shape at the center of the slide using IAutoShape.SetShapeAlignment() method.
  • Set the fill color, line style, font color, and text of the shape through other properties under the IAutoShape object.
  • Save the presentation to a .pptx file using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Drawing;

namespace CreatePowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation object
            Presentation presentation = new Presentation();

            //Set the slide size type to screen 16x9
            presentation.SlideSize.Type = SlideSizeType.Screen16x9;

            //Get the first slide
            ISlide slide = presentation.Slides[0];

            //Set the background image
            string imgPath = @"C:\Users\Administrator\Desktop\bgImage.jpg";
            IImageData imageData = presentation.Images.Append(Image.FromFile(imgPath));
            slide.SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
            slide.SlideBackground.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Picture;
            slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
            slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = imageData;

            //Insert a rectangle shape
            Rectangle rect = new Rectangle(100, 100, 500, 80);
            IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rect);

            //Position the shape at the center of the slide
            shape.SetShapeAlignment(ShapeAlignment.AlignCenter);
            shape.SetShapeAlignment(ShapeAlignment.DistributeVertically);

            //Set the fill color, line style and font color of the shape
            shape.Fill.FillType = FillFormatType.Solid;
            shape.Fill.SolidColor.Color = Color.BlueViolet;
            shape.ShapeStyle.LineStyleIndex = 0;//no line
            shape.ShapeStyle.FontColor.Color = Color.White;

            //Set the text of the shape
            shape.TextFrame.Text = "This article shows you how to create a simple PowerPoint document using Spire.Presentation for Java.";

            //Save to file
            presentation.SaveToFile("CreatePowerPoint.pptx", FileFormat.Pptx2013);
        }
    }
}

C#/VB.NET: Create a PowerPoint Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 26 January 2022 08:20