DCodes
DCodes

Reputation: 839

How to retrieve all properties of charts and graphics in a ppt slide using openxm c#?

I used a function that would identify whether the shape is a chart or not, but I want to retrieve all properties of the chart (plot, width, height, chart content,..)

what would help to generate these properties knowing that they are many?

This is the function I used to get the charts:

private static List<PPChart> GetChartsfromSlide(SlidePart slidepart)
    {
        var chartList = new List<PPChart>();

        if (slidepart.ChartParts.Any())
        {
            foreach (var chart in slidepart.ChartParts)
            {
                //// get the ID of the Chart-Part
                var id = slidepart.GetIdOfPart(chart);

                //// Get a list of all Shapes(Graphicframes) which contain Charts
                var gshapes = from shapeDesc in slidepart.Slide.Descendants<GraphicFrame>() select shapeDesc;

                var tempgshapes = gshapes.ToList();

                //// Select all possible Shapes which have Graphics
                var thisShape = from Gshape in tempgshapes where HasThisChart(id, Gshape) select Gshape;

                var result = thisShape.ToList();

                Console.WriteLine("Found Chart with ID:{0} Name:{1}", result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Id, result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name);
               var childlists= result[0].ChildElements.ToList();
               

                var ppchart = new PPChart(result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Id);
                ppchart.Name = result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;
                chartList.Add(ppchart);
            }
        }

        return chartList;
    }

Upvotes: 1

Views: 225

Answers (1)

Andrey Potapov
Andrey Potapov

Reputation: 67

If you used Aspose. Slides for .NET, you could read these properties very easily as shown below:

using (var presentation = new Presentation("example.pptx"))
{
    var chart = presentation.Slides[0].Shapes.OfType<IChart>().First();

    // chart properties
    var plot = chart.PlotArea;
    var width = chart.Width;
    var height = chart.Height;
    var data = chart.ChartData;
    var legend = chart.Legend;
    var axes = chart.Axes;
    // etc.
}

Alternatively, you can use Aspose.Slides Cloud API and make 150 free API calls per month for API learning and presentation processing.

var slidesApi = new SlidesApi(config);

const int slideIndex = 1;
const int shapeIndex = 1;

var shape = slidesApi.GetShape("example.pptx", slideIndex, shapeIndex);
if (shape is Chart chart)
{
    var width = chart.Width;
    // etc.
}

I work at Aspose.

Upvotes: 2

Related Questions