从幻灯片中获取所有带有图表的形状

时间:2013-05-08 08:18:11

标签: c# powerpoint openxml-sdk

我正在使用Powerpoint幻灯片并尝试使用幻灯片中的图表提取所有形状的名称。我可以看出幻灯片是否有图表,但我仍然不知道如何找到相应的幻灯片。

代码:

// suppose that a presentation is correctly loaded
private List<PPChart> GetChartsfromSlide(SlidePart slidepart)
{
    var chartList = new List<PPChart>();
    if (slidepart.ChartParts.Any())
    {
        foreach (var chart in slidepart.ChartParts)
        {
            // how to get the ID of the Chart and get the corespondending slide?
        }
    }

    return chartList;
}

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,但解决方案涉及到: 希望它能在将来帮助某人......

   /// <summary>
    /// Gets a List of all Charts on this Slide
    /// </summary>
    /// <param name="slidepart">The SlidePart.</param>
    /// <returns>A List of all Charts on this Slide</returns>
    private 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 this.HasThisChart(id, Gshape) select Gshape;

                var result = thisShape.ToList();

                this.logger.Debug("Found Chart with ID:{0} Name:{1}", result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Id, result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name);

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

        return chartList;
    }

    /// <summary>
    /// Determines whether the Slider has this Chart or not.
    /// </summary>
    /// <param name="id">The id.</param>
    /// <param name="gframe">The gframe.</param>
    /// <returns>
    ///   <c>true</c> if the Slide has the chart; otherwise, <c>false</c>.
    /// </returns>
    private bool HasThisChart(string id, GraphicFrame gframe)
    {
        var returnValue = false;

        if (!(gframe == null) && this.HasGraphic(gframe))
        {
            if (!(gframe.Graphic.GraphicData == null))
            {
                var graphicData = gframe.Graphic.GraphicData;
                var drawChartsRef = graphicData.Descendants<DrawCharts.ChartReference>();

                if (!(drawChartsRef == null))
                {
                    foreach (var drawChart in drawChartsRef)
                    {
                        if (drawChart.Id == id)
                        {
                            returnValue = true;
                        }
                    }
                }
            }
        }

        return returnValue;
    }

    /// <summary>
    /// Determines whether the specified GraphicFrame has a graphic (A graphic is a chart!).
    /// </summary>
    /// <param name="gframe">The gframe.</param>
    /// <returns>
    ///   <c>true</c> if the specified gframe has a graphic; otherwise, <c>false</c>.
    /// </returns>
    private bool HasGraphic(GraphicFrame gframe)
    {
        var returnValue = false;

        if (!(gframe == null))
        {
            var graphicDescendants = gframe.Descendants<Draw.Graphic>();
            if (graphicDescendants.Count() > 0)
            {
                returnValue = true;
            }
        }

        return returnValue;
    }