Add SmartArt to Slide in C#

PowerPoint SmartArt is a special type of graphic object that gives you the ability to construct fancy graphical lists and diagrams in your slide quickly and easily. With just one click, you’re able to create a flow chart, an organization chart or a timeline in slide. This article gives you an example of how to add an organization chart to slide using Spire.Presentation with C#.


//create an instance of Presentation
Presentation ppt = new Presentation();

//set slide size
ppt.SlideSize.Type = SlideSizeType.Screen16x9;

//append an organization chart and specify position and size
ISmartArt smartArt = ppt.Slides[0].Shapes.AppendSmartArt(50, 50, 450, 250, SmartArtLayoutType.OrganizationChart);

//set the style and color of smartart
smartArt.Style = SmartArtStyleType.IntenceEffect;
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4;

//remove default notes (represent shape in smartart)
foreach (ISmartArtNode node in smartArt.Nodes)
{
smartArt.Nodes.RemoveNode(node);
}

//add nested shapes to smartart
ISmartArtNode node1 = smartArt.Nodes.AddNode();
ISmartArtNode node1_1 = node1.ChildNodes.AddNode();
ISmartArtNode node1_1_1 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_2 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_3 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_4 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_5 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_6 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_1 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_2 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_3 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_3_1 = node1_1_3.ChildNodes.AddNode();
ISmartArtNode node1_1_3_2 = node1_1_3.ChildNodes.AddNode();
ISmartArtNode node1_1_6_1 = node1_1_6.ChildNodes.AddNode();
ISmartArtNode node1_1_6_2 = node1_1_6.ChildNodes.AddNode();
ISmartArtNode node1_1_6_3 = node1_1_6.ChildNodes.AddNode();

//add text to each shape
node1.TextFrame.Text = "Board of Directors";
node1_1.TextFrame.Text = "General Manager";
node1_1_1.TextFrame.Text = "Supply Department";
node1_1_2.TextFrame.Text = "Sales Department";
node1_1_3.TextFrame.Text = "Productive Department";
node1_1_4.TextFrame.Text = "Finance Department";
node1_1_5.TextFrame.Text = "HR Department";
node1_1_6.TextFrame.Text = "Quality Center";
node1_1_1_1.TextFrame.Text = "Purchase Department";
node1_1_1_2.TextFrame.Text = "Warehouse Manager";
node1_1_1_3.TextFrame.Text = "Logistics Department";
node1_1_3_1.TextFrame.Text = "Production Department";
node1_1_3_2.TextFrame.Text = "Maintenance Department";
node1_1_6_1.TextFrame.Text = "Production Quality Management";
node1_1_6_2.TextFrame.Text = "Production Safety Management";
node1_1_6_3.TextFrame.Text = "Environmental Management";

//save to file
ppt.SaveToFile("output.pptx", FileFormat.Pptx2013);

Output

2017-08-02_163332

Advertisements

Convert PPT/PPTX to PNG in C#

It is not uncommon that we may need to convert a PowerPoint file to images or a PDF file in our work. In my previous post, I have introduced how to export PPT/PPTX files as PDFs. This article will introduce how we can convert a PPT/PPTX file into PNGs by using Spire.Presentation in C#.

Here come the code snippets:


using Spire.Presentation;
using System.Drawing;

namespace Conversion
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of Presentation class
            Presentation presentation = new Presentation();

            //Load a PowerPoint document
            presentation.LoadFromFile("Template.pptx");

            //Traverse each slide in PowerPoint and save as png image
            for (int i = 0; i < presentation.Slides.Count; i++)
            {
                Image image = presentation.Slides[i].SaveAsImage();
                String fileName = String.Format("Result-{0}.png", i);
                image.Save(fileName);
            }
        }
    }
}

Output

2017-05-08_152546

Create Pie Chart in PowerPoint using Spire.Presenation in C#

Pie charts are a popular way to show how much individual amounts – such as quarterly sales figures – contribute to a total amount—such as annual sales. In this post, I am going to introduce how to create a pie chart in PowerPoint in C#.

2016-06-16_105733

Using Namespaces

using Spire.Presentation;
using System.Drawing;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;

Step 1: Create a new object of Presentation class.

Presentation ppt = new Presentation();

Step 2: Insert a Pie chart to the first slide.

RectangleF rect1 = new RectangleF(40, 40, 550, 320);
IChart chart = ppt.Slides[0].Shapes.AppendChart(ChartType.Pie3D, rect1, false);
chart.ChartTitle.TextProperties.Text = "Sales by Quarter";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;

Step 3: Define the data.

string[] quarters = new string[] { "1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr" };
int[] sales = new int[] { 5480, 6540, 4780, 8500 };

Step 4: Append data to ChartData, which is the data table associated with chart.

chart.ChartData[0, 0].Text = "Quarters";
chart.ChartData[0, 1].Text = "Sales";
for (int i = 0; i < quarters.Length; ++i)
{
    chart.ChartData[i + 1, 0].Value = quarters[i];
    chart.ChartData[i + 1, 1].Value = sales[i];
}

Step 5: Set category labels, series label and series data.

chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];
chart.Categories.CategoryLabels= chart.ChartData["A2", "A5"];
chart.Series[0].Values = chart.ChartData["B2", "B5"];

Step 6: Add data points to series and format each data point.

for (int i = 0; i < chart.Series[0].Values.Count; i++)
{
    ChartDataPoint cdp = new ChartDataPoint(chart.Series[0]);
    cdp.Index = i;
    chart.Series[0].DataPoints.Add(cdp);

}
chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid;
chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.RosyBrown;

chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid;
chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.LightBlue;

chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid;
chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.LightPink;

chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid;
chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.MediumPurple;

Step 7: Show label value and percentage value in data labels.

chart.Series[0].DataLabels.LabelValueVisible = true;
chart.Series[0].DataLabels.PercentValueVisible = true;

Step 8: Save the file.

ppt.SaveToFile("PieChart.pptx", FileFormat.Pptx2010);

Split PowerPoint Document into Individual Slides in C#

When we deal with a large PowerPoint document, we may need to split the document to small presentations, each of which contains one original slide. In this article, you will learn how to split a PowerPoint document into individual slides using .NET Presentation API, the free Spire.Presentation.

The method in this solution is as below:

  • Create a new PowerPoint document, remove the default blank slide.
  • Clone the specified the slide from source presentation to the new one.

Using Code

using Spire.Presentation;

 

namespace SplitPresentation

{

    class Program

    {

        static void Main(string[] args)

        {

            //load source PowerPoint file

            Presentation ppt = new Presentation();

            ppt.LoadFromFile(“sample.pptx”);

 

            for (int i = 0; i < ppt.Slides.Count; i++)

            {

                //initialize new instance of Presentation class

                Presentation newppt = new Presentation();

                //remove blank slide

                newppt.Slides.RemoveAt(0);

                //clone slide from source file to new document

                newppt.Slides.Append(ppt.Slides[i]);

                //save the file

                newppt.SaveToFile(String.Format(“result-{0}.pptx”, i), FileFormat.Pptx2010);

            }

 

        }

    }

Output

After running the code, the sample file that contains three slides will be split into three presentation files as below.

output

Insert Excel Worksheet as an OEL Object to PowerPoint in C#

Embedding Excel worksheet as an OLE object to a PowerPoint slide is an efficient way to manage Excel data directly from PowerPoint. If you want to update an embedded file, you can double-click the object to open the file and then edit. Updating the source file will not update the embedded file.

In this article, I’ll introduce how to embed Excel sheet as OLE object into PowerPoint slide programmatically using Spire.XLS and Spire.Presentation. To begin with, you need to download Spire.Office and add the related DLLs to your VS project. Then add following namespaces in your program.

Namespaces to be used:

using Spire.Xls;

using Spire.Presentation;

using Spire.Presentation.Drawing;

Main Steps:

Step 1: Initialize new instance of Workbook class and load the Excel file.

Workbook book = new Workbook();

book.LoadFromFile(@”data.xlsx”);

Step 2: Select the cell range from the first worksheet and save it as image.

Image image = book.Worksheets[0].SaveToImage(1, 1, 7, 9);

Step 3: Initialize a new instance of Presentation class, append the image to presentation.

Presentation ppt = new Presentation();

IImageData oleImage = ppt.Images.Append(image);

Step 4: Insert an OLE object to presentation based on the Excel data.

Rectangle rec = new Rectangle(60,60,image.Width,image.Height);

using (MemoryStream ms = new MemoryStream())

{

    book.SaveToStream(ms);

     ms.Position = 0;

    Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject(“excel”,ms.ToArray(), rec);

    oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;

    oleObject.ProgId = “Excel.Sheet.12”;

}

Step 5: Save the file.

ppt.SaveToFile(“Result.ppt”, Spire.Presentation.FileFormat.Pptx2007);

Screenshot of Effect:

result

Full Code:

Workbook book = new Workbook();

book.LoadFromFile(@”data.xlsx”);

Image image = book.Worksheets[0].SaveToImage(1, 1, 7, 9);

 

Presentation ppt = new Presentation();

IImageData oleImage = ppt.Images.Append(image);

Rectangle rec = new Rectangle(60,60,image.Width,image.Height);

using (MemoryStream ms = new MemoryStream())

{

    book.SaveToStream(ms);

    ms.Position = 0;

    Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject(“excel”, ms.ToArray(), rec);


oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage =oleImage;

    oleObject.ProgId = “Excel.Sheet.12”;

}

 

ppt.SaveToFile(“Result.ppt”, Spire.Presentation.FileFormat.Pptx2007);