Home > Articles > .Net Framework Articles > Easy Solution to Play with PowerPoint document

Easy Solution to Play with PowerPoint document

by kim johnson   on Jul 15, 2014   Category: .Net Framework  | Level: Beginner  |  Views: 1815    |  Points: 100   
Like this article? Bookmark and Share:
In this article, I introduce a FREE .NET library that can create and edit PowerPoint document very easily. And there is also a simple demo showing how to use the library.



Background

PowerPoint is a very useful and powerful file format. It enables users to show their stuff in a beautiful and impressive way. I am a programmer myself. And I deal with PowerPoint document now and then.  During my work, I found a very useful free .NET library (Spire.Presentation) that can create and edit PowerPoint document without much effort. And I want to share it with you here.

It is an independent PowerPoint .NET component; Spire.Presentation for .NET doesn't need Microsoft PowerPoint installed on the machine. And it is compatible with other .NET applications.

Introduction

Using Spire.Presentation, you can create and edit PowerPoint document very easily. I will give you detailed introduction here about how to use this library and create a simple PowerPoint document.

First download the library, and add reference to Spire.Presentation from project. 

Add this line of code.

using Spire.Presentation;

 

Then you can use the library now.

 

Presentation presentation = new Presentation();

This line of code will create a blank PowerPoint document. See? It is very easy.

IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle,

new RectangleF(70, 100, 200, 50));

 

Add new IAutoShape to document. IAutoShape is representation of textbox. So you can add text to the IAutoShape.

 

shape.AppendTextFrame("Hello World!");

 

You can also insert image into document.

string ImageFile = "pic.png";

presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect_Image);

 

At last, just save the document.

presentation.SaveToFile("demo.pptx", FileFormat.Pptx2010);

 

You may need to load existing PowerPoint documents, check the methods.

void LoadFromFile(string file);

void LoadFromFile(string file, FileFormat fileFormat);

void LoadFromFile(string file, string password);

void LoadFromFile(string file, FileFormat fileFormat, string password);

void LoadFromStream(Stream stream, FileFormat fileFormat);

 

These methods enable you to load from file or stream. You can specify file format or the password to load encrypted file.

There are other features that this library supports:

l  Insert all kinds of shapes (such as triangle, rectangle, BackOrPreviousButton );

l  Insert audio, video;

l  Add table, chart;

l  Convert to other file format;

l  Encrypt and decrypt PowerPoint document;

l  Animation.

In a word, this free library can do you lot of help when you dealing with PowerPoint document.

Demo Code

Here is full code of demo. If you find it difficult to understand, read the above part which can guide you through the demo.

//create Presentation instance

Presentation presentation = new Presentation();

 

//add new shape to PPT document

IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle,

    new RectangleF(70, 100, 200, 50));

shape.ShapeStyle.LineColor.Color = Color.White;

shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;

 

//add text to shape

shape.AppendTextFrame("Hello World!");

TextRange textRange = shape.TextFrame.TextRange;

textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;

textRange.Fill.SolidColor.Color = Color.Black;

textRange.LatinFont = new TextFont("Arial Black");

 

//insert image to PPT

RectangleF rect_Image = new RectangleF(80, 170, 390, 260);

string ImageFile = "pic.png";

presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect_Image);

presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

 

//save the document

presentation.SaveToFile("demo.pptx", FileFormat.Pptx2010);

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

 

Check the result file:


Conclusion

It is a useful and easy to use library. You are welcome to test the demo code. And I hope this article can do you some help.

Add this line of code.

using Spire.Presentation;

 

Then you can use the library now.

 

Presentation presentation = new Presentation();

This line of code will create a blank PowerPoint document. See? It is very easy.

IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle,

new RectangleF(70, 100, 200, 50));

 

Add new IAutoShape to document. IAutoShape is representation of textbox. So you can add text to the IAutoShape.

 

shape.AppendTextFrame("Hello World!");

 

You can also insert image into document.

string ImageFile = "pic.png";

presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect_Image);

 

At last, just save the document.

presentation.SaveToFile("demo.pptx", FileFormat.Pptx2010);

 

You may need to load existing PowerPoint documents, check the methods.

void LoadFromFile(string file);

void LoadFromFile(string file, FileFormat fileFormat);

void LoadFromFile(string file, string password);

void LoadFromFile(string file, FileFormat fileFormat, string password);

void LoadFromStream(Stream stream, FileFormat fileFormat);

 

These methods enable you to load from file or stream. You can specify file format or the password to load encrypted file.

There are other features that this library supports:

l  Insert all kinds of shapes (such as triangle, rectangle, BackOrPreviousButton );

l  Insert audio, video;

l  Add table, chart;

l  Convert to other file format;

l  Encrypt and decrypt PowerPoint document;

l  Animation.

In a word, this free library can do you lot of help when you dealing with PowerPoint document.

Demo Code

Here is full code of demo. If you find it difficult to understand, read the above part which can guide you through the demo.

//create Presentation instance

Presentation presentation = new Presentation();

 

//add new shape to PPT document

IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle,

    new RectangleF(70, 100, 200, 50));

shape.ShapeStyle.LineColor.Color = Color.White;

shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;

 

//add text to shape

shape.AppendTextFrame("Hello World!");

TextRange textRange = shape.TextFrame.TextRange;

textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;

textRange.Fill.SolidColor.Color = Color.Black;

textRange.LatinFont = new TextFont("Arial Black");

 

//insert image to PPT

RectangleF rect_Image = new RectangleF(80, 170, 390, 260);

string ImageFile = "pic.png";

presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect_Image);

presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

 

//save the document

presentation.SaveToFile("demo.pptx", FileFormat.Pptx2010);

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

 

Check the result file:


 

Conclusion

It is a useful and easy to use library. You are welcome to test the demo code. And I hope this article can do you some help.



Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts