How to Protect Chart on PowerPoint Slide in C#, VB.NET

2015-04-03 03:04:42 Written by  support iceblue
Rate this item
(0 votes)

When we create a PowerPoint slide that contains charts on it, we may not want others to change the chart data, especially when we create a presentation of financial report, it is very important for legal reasons that no changes get made when the slides are presented. In this article, I'll introduce how to protect chart on PowerPoint slide via Spire.Presentation in C# and VB.NET.

Test File:

How to Protect Chart on PowerPoint Slide in C#, VB.NET

Code Snippet:

Step 1: Create a new instance of Presentation class. Load the sample file to PPT document by calling LoadFromFile() method.

Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx",FileFormat.Pptx2010);

Step 2: Get the second shape from slide and convert it as IChart. The first shape in the sample file is a textbox.

IChart chart = ppt.Slides[0].Shapes[1] as IChart;

Step 3: Set the Boolean value of IChart.IsDataProtect as true.

chart.IsDataProtect = true;

Step 4: Save the file.

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

Output:

Run this program and open the result file, you’ll get following warning message if you try to modify the chart data in Excel.

How to Protect Chart on PowerPoint Slide in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Charts;

namespace ProtectChart
{

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

            Presentation ppt = new Presentation();
            ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010);

            IChart chart = ppt.Slides[0].Shapes[1] as IChart;
            chart.IsDataProtect = true;
            ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);


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

Namespace ProtectChart

	Class Program
		Private Shared Sub Main(args As String())

			Dim ppt As New Presentation()
			ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010)

			Dim chart As IChart = TryCast(ppt.Slides(0).Shapes(1), IChart)
			chart.IsDataProtect = True
			ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)


		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Protect Chart on PowerPoint Slide
Last modified on Friday, 24 September 2021 09:25