page 207

Videos are common elements in PowerPoint, sometimes developers need to extract videos from PowerPoint documents and save them to disk programmatically. Spire.Presentation provides developers many flexible functions to manipulate PowerPoint documents including extract Videos. This article will explain how to achieve this task in C# and VB.NET using Spire.Presentation.

Note: Before start, please download and install Spire.Presentation correctly, after that add a reference to Spire.Presentation.dll in your project.

Detail steps:

Step 1: Instantiate an object of Presentation class and load the sample document.

Presentation presentation = new Presentation();
presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");

Step 2: Loop through all shapes on the slides, find out the videos and then call VideoData.SaveToFile(string fileName) method to save them to disk.

int i = 0;
foreach (ISlide slide in presentation.Slides)
{
    foreach (IShape shape in slide.Shapes)
    {
        if (shape is IVideo)
        {
            (shape as IVideo).EmbeddedVideoData.SaveToFile(string.Format(@"Video\Video-{0}.mp4",i));
            i++;
        }
    }
}

Below are the extracted videos from the PowerPoint document:

How to Extract Videos from PowerPoint Documents in C#, VB.NET

Full code:

[C#]
using Spire.Presentation;

namespace Extract_Video
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");
            int i = 0;
            foreach (ISlide slide in presentation.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape is IVideo)
                    {
                        (shape as IVideo).EmbeddedVideoData.SaveToFile(string.Format(@"Video\Video-{0}.mp4",i));
                        i++;
                    }
                }
            }

        }
    }
}
[VB.NET]
Imports Spire.Presentation

Namespace Extract_Video
	Class Program
		Private Shared Sub Main(args As String())
			Dim presentation As New Presentation()
			presentation.LoadFromFile("C:\Users\Administrator\Desktop\Sample.pptx")
			Dim i As Integer = 0
			For Each slide As ISlide In presentation.Slides
				For Each shape As IShape In slide.Shapes
					If TypeOf shape Is IVideo Then
						TryCast(shape, IVideo).EmbeddedVideoData.SaveToFile(String.Format("Video\Video-{0}.mp4", i))
						i += 1
					End If
				Next
			Next

		End Sub
	End Class
End Namespace

Required fields force the user to fill in the selected form field. If the user attempts to submit the form while a required field is blank, an error message appears and the empty required form field is highlighted.

Using Spire.PDF, we're able to enforce a form field as "Required" or determine which fields are required in an existing PDF document. This article mainly introduce how to detect the required form fields in C# and VB.NET.

Code Snippet:

Step 1: Initialize an instance of PdfDocument class and load a sample PDF file that contains some form fields.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C: \Users\Administrator\Desktop\PdfFormExample.pdf");

Step 2: Get all form widgets from the PDF document.

PdfFormWidget formWidget = doc.Form as PdfFormWidget;

Step 3: Traverse the form widgets to find the form field that is required and print all results on console.

for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
{
    PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
    string fieldName = field.Name;
    bool isRequired = field.Required;
    if (isRequired)
    {
        Console.WriteLine(fieldName + " is required.");
    }
}

Output:

How to Detect the Required Form Field in PDF in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;
using System;

namespace Detect
{
    class Program
    {
        static void Main(string []args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C: \Users\Administrator\Desktop\PdfFormExample.pdf");

            PdfFormWidget formWidget = doc.Form as PdfFormWidget;
            for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
            {
                PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
                string fieldName = field.Name;
                bool isRequired = field.Required;
                if (isRequired)
                {
                    Console.WriteLine(fieldName + " is required.");
                }
            }
            Console.ReadKey();
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Fields
Imports Spire.Pdf.Widget

Namespace Detect
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C: \Users\Administrator\Desktop\PdfFormExample.pdf")

			Dim formWidget As PdfFormWidget = TryCast(doc.Form, PdfFormWidget)
			For i As Integer = 0 To formWidget.FieldsWidget.List.Count - 1
				Dim field As PdfField = TryCast(formWidget.FieldsWidget.List(i), PdfField)
				Dim fieldName As String = field.Name
				Dim isRequired As Boolean = field.Required
				If isRequired Then
					Console.WriteLine(fieldName & Convert.ToString(" is required."))
				End If
			Next
			Console.ReadKey()
		End Sub
	End Class
End Namespace

A pie chart helps show proportions and percentages between categories, by dividing a circle into proportional segments. This article will introduce how to create a pie chart that looks like the below screenshot in PowerPoint document using Spire.Presentation in C#.

How to Create Pie Chart in PowerPoint in C#

Code Snippets:

Step 1: Initialize an instance of Presentation class.

Presentation ppt = new Presentation();

Step 2: Insert a Pie chart to the first slide by calling the AppendChart() method and set the chart title.

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

Step 3: Define some data.

string[] quarters = new string[] { "1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr" };
int[] sales = new int[] { 210, 320, 180, 500 };

Step 4: Append data to ChartData, which represents a data table where the chart data is stored.

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 fill each data point with different color.

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: Set the data labels to display label value and percentage value.

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

Step 8: Save the file.

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

Full Code:

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


namespace CreatePieChartInPowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {

            Presentation ppt = new Presentation();

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

            string[] quarters = new string[] { "1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr" };
            int[] sales = new int[] { 210, 320, 180, 500 };
            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];
            }
            
            chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];         
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];
            chart.Series[0].Values = chart.ChartData["B2", "B5"];

            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;

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

            ppt.SaveToFile("PieChart.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("PieChart.pptx");
        }
    }
}
page 207