Save PowerPoint file to Stream and Load PowerPoint file from Stream

Spire.Presentation is a powerful and easy-to-use .NET component, especially designed for developers. Using Spire.Presentation you can generate, modify, convert, render, and print documents without installing Microsoft PowerPoint on your machine. In this document, I will introduce you how to create PowerPoint file and save it to stream. I also introduce you how to load PowerPoint file from stream. Check it below.

Create PowerPoint file and save it to Stream

Step 1: Create Presentation instance.

Presentation presentation = new Presentation();

Step 2: Set background image.

string ImageFile = "bg.png";
RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

Step 3: Add text to PowerPoint document.

IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 450, 150));
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White;

//add text to shape
shape.TextFrame.Text = "This demo shows how to Create PowerPoint file and save it to Stream.";
shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;

Step 4: Save document to stream.

FileStream to_stream = new FileStream("To_stream.pptx", FileMode.Create);
presentation.SaveToFile(to_stream, FileFormat.Pptx2010);
to_stream.Close();

Preview the generated PowerPoint file:

Save and Load PowerPoint file to/from Stream

Load PowerPoint file from stream.

Step 1: Create Presentation instance.

Presentation presentationLoad = new Presentation();

Step 2: Load PowerPoint file from stream.

FileStream from_stream = File.OpenRead("sample.pptx");
presentationLoad.LoadFromStream(from_stream, FileFormat.Pptx2010);

Step 3: Save the document.

presentationLoad.SaveToFile("From_stream.pptx",FileFormat.Pptx2010);
from_stream.Dispose();

Preview the PowerPoint file:

Save and Load PowerPoint file to/from Stream

Full code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
using System.IO;
namespace SavePPTtoStream
{
    class Program
    {
        static void Main(string[] args)
        {

            //A: create PowerPoint file and save it to stream
            Presentation presentation = new Presentation();

            //set background Image
            string ImageFile = "bg.png";
            RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
            presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

            //append new shape
            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 450, 150));
            shape.Fill.FillType = FillFormatType.None;
            shape.ShapeStyle.LineColor.Color = Color.White;

            //add text to shape
            shape.TextFrame.Text = "This demo shows how to Create PowerPoint file and save it to Stream.";
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;

            //save to Stream
            FileStream to_stream = new FileStream("To_stream.pptx", FileMode.Create);
            presentation.SaveToFile(to_stream, FileFormat.Pptx2010);
            to_stream.Close();

            System.Diagnostics.Process.Start("To_stream.pptx");


            //B: Load PowerPoint file from Stream
            Presentation presentationLoad = new Presentation();

            //load PowerPoint file from stream
            FileStream from_stream = File.OpenRead("sample.pptx");
            presentationLoad.LoadFromStream(from_stream, FileFormat.Pptx2010);

            //save the document
            presentationLoad.SaveToFile("From_stream.pptx", FileFormat.Pptx2010);
            from_stream.Dispose();

            System.Diagnostics.Process.Start("From_stream.pptx");


        }
    }
}

If you couldn't successfully use Spire.Presentation, please refer Spire.Presentation Quick Start which can guide you quickly use Spire.Presentation.

Additional Info

  • tutorial_title: Save and Load PowerPoint to|from Stream
Last modified on Friday, 24 September 2021 09:12