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:
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.
Full Code:
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); } } }
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