Search code examples
c#powerpointoffice-interopcom-interopopenxml-sdk

How to programmatically define `data labels` state of a PowerPoint chart?


Is it possible to programmatically, for example by using Interop.PowerPoint, or Open XML SDK, define, enable/disable state Data Labels checkbox of a chart that is placed on a PowerPoint slide?

enter image description here

I have given it a shot using Interop.PowerPoint, as my code below shows, but my code is incomplete. Does anyone have ideas in how to complete the code to achieve the tasks I am after, as explained above?

internal static void DataLabels()
{
    var app = new PP.Application();
    var pre = app.Presentations.Open2007(@"c:\input.pptx", WithWindow: MsoTriState.msoFalse, Untitled: MsoTriState.msoTrue);

    var chartShape = pre.Slides[1].Shapes[1];
    var chart = chartShape.Chart;

    //...
    // if chart has Data Labels set true write 'Yes' in console.
    Console.WriteLine("Yes.");

}

Solution

  • Every chart can have one or more series. In the case of pie chart you need to access first series and check for HasDataLabels property:

    bool has_labels = chart.SeriesCollection(1).HasDataLabels;