Create a PowerPoint Document and Convert it to PDF using Free Library
As a graduate student, I was assigned to create a PowerPoint silde programmatically using C#. After several days' searching online, I found a free library to create a PowerPoint document: Free Spire.Presentation. Considering it's free and easy to use, I decided to go with it. Below you can see that how I created and manipulated a PowerPoint document.
Introduction:
Recently, I found a free library to create a PowerPoint document: Free Spire.Presentation. Considering it's free and easy to use, I decided to go with it. Below you can see that how I created and manipulated a PowerPoint document.
Purpose:
•Create a PowerPoint document.
•Insert text and image.
•Insert table.
•Add hyperlink.
•Set footer.
•Set transition of slide.
•Save PowerPoint document.
•Save as PDF document.
Code Snippet:
First, I create a PowerPoint document and set background image of the slide.
Presentation ppt = new Presentation();
ISlide slide = ppt.Slides[0];
SizeF pptSize=ppt.SlideSize.Size;
string bgFile = "bg.jpg";
RectangleF bgRect = new RectangleF(new PointF(0, 0), pptSize);
slide.Shapes.AppendEmbedImage(ShapeType.Rectangle, bgFile, bgRect);
Next, I add a title and an image in the middle position.
//Add title
RectangleF titleRect = new RectangleF(pptSize.Width / 2 - 200, 10, 400, 50);
IAutoShape titleShape = slide.Shapes.AppendShape(ShapeType.Rectangle, titleRect);
titleShape.Fill.FillType = FillFormatType.None;
titleShape.ShapeStyle.LineColor.Color = Color.Empty;
TextParagraph titlePara = titleShape.TextFrame.Paragraphs[0];
titlePara.Text = "Microsoft PowerPoint";
titlePara.FirstTextRange.FontHeight = 36;
titlePara.FirstTextRange.Fill.FillType = FillFormatType.Solid;
titlePara.FirstTextRange.Fill.SolidColor.Color = Color.Black;
titlePara.Alignment = TextAlignmentType.Center;
//Insert Image
string logoFile = "logo.png";
RectangleF logoRect = new RectangleF(pptSize.Width / 2 - 40, 60, 80, 80);
IEmbedImage image = slide.Shapes.AppendEmbedImage(ShapeType.Rectangle, logoFile, logoRect);
image.Line.FillType = FillFormatType.None;
Next, I add a built-in style table and the hyperlink to "WebSite".
//Insert Table
double[] widths={160, 320};
double[] heights={15, 15, 15, 15, 15, 15, 15};
ITable table = slide.Shapes.AppendTable(pptSize.Width / 2 - 240, 150, widths, heights);
string[,] data = new string[,]
{
{"Developer(s)", "Microsoft"},
{"Stable Release", "2013/10,2,2012; 2 years ago"},
{"Written In", "C++"},
{"Operation System", "Microsoft Windows"},
{"Type", "Presentation Program"},
{"License", "Trialware"},
{"Website", "http://office.microsoft.com/powerpoint"}
};
for(int i = 0; i < 7; i++)
{
for (int j = 0; j < 2; j++)
{
table[j, i].TextFrame.Text = data[i, j];
}
}
table.StylePreset = TableStylePreset.LightStyle3Accent5;
//Add hyperlink
ClickHyperlink hyperlink = new ClickHyperlink("http://office.microsoft.com/powerpoint");
table[1, 6].TextFrame.TextRange.ClickAction = hyperlink;
Next, I add a new shape which contains a brief description of Microsoft PowerPoint.
//Add content
RectangleF textRect = new RectangleF(pptSize.Width / 2 - 240, 365, 480, 130);
IAutoShape textShape = slide.Shapes.AppendShape(ShapeType.Rectangle, textRect);
//Content format
string text = File.ReadAllText("description.txt");
textShape.AppendTextFrame(text);
TextParagraph contentPara = textShape.TextFrame.Paragraphs[0];
contentPara.FirstTextRange.Fill.FillType = FillFormatType.Solid;
contentPara.FirstTextRange.Fill.SolidColor.Color = Color.Black;
contentPara.Alignment = TextAlignmentType.Left;
//Shape format
textShape.ShapeStyle.LineColor.Color = Color.Empty;
textShape.ShapeStyle.FillColor.Color = Color.FromArgb(217, 237, 238);
Finally, I add footer to slide and set slide transition effect, and then save the document.
//Set footer
ppt.SetFooterText("Demo For Free Spire.Presentation");
ppt.SetFooterVisible(true);
ppt.SetDateTimeVisible(true);
ppt.SetSlideNoVisible(true);
//Set transition of slide
slide.SlideShowTransition.Type = TransitionType.Fade;
//Save document
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
Screenshot of PowerPoint:
You can also the document as a PDF document.
ppt.SaveToFile("result.pdf", FileFormat.PDF);
Screenshot of PDF: