C# 使用幻灯片中的图表获取所有形状
我正在使用Powerpoint幻灯片,并尝试从幻灯片中提取所有带有图表的形状的名称。我可以找出这张幻灯片是否有图表,但我被困在如何找到对应的幻灯片上 代码:C# 使用幻灯片中的图表获取所有形状,c#,powerpoint,openxml-sdk,C#,Powerpoint,Openxml Sdk,我正在使用Powerpoint幻灯片,并尝试从幻灯片中提取所有带有图表的形状的名称。我可以找出这张幻灯片是否有图表,但我被困在如何找到对应的幻灯片上 代码: //假设已正确加载演示文稿 私有列表从幻灯片获取图表(幻灯片部分幻灯片部分) { var chartList=新列表(); if(slidepart.ChartParts.Any()) { foreach(slidepart.ChartParts中的var图表) { //如何获得图表的ID和相应的幻灯片? } } 返回图表列表; } 我找到
//假设已正确加载演示文稿
私有列表从幻灯片获取图表(幻灯片部分幻灯片部分)
{
var chartList=新列表();
if(slidepart.ChartParts.Any())
{
foreach(slidepart.ChartParts中的var图表)
{
//如何获得图表的ID和相应的幻灯片?
}
}
返回图表列表;
}
我找到了一个解决方案,但这个解决方案相当复杂:
希望它能帮助将来的某个人
/// <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;
}
//
///获取此幻灯片上所有图表的列表
///
///幻灯片部分。
///此幻灯片上所有图表的列表
私有列表从幻灯片获取图表(幻灯片部分幻灯片部分)
{
var chartList=新列表();
if(slidepart.ChartParts.Any())
{
foreach(slidepart.ChartParts中的var图表)
{
////获取图表部件的ID
var id=slidepart.GetIdOfPart(图表);
////获取包含图表的所有形状(图形框架)的列表
var gshapes=来自slidepart.Slide.subjections()中的shapeDesc选择shapeDesc;
var tempgshapes=gshapes.ToList();
////选择所有可能具有图形的形状
var thisShape=从tempgShape中的Gshape中选择,其中this.HasThisChart(id,Gshape)选择Gshape;
var result=thisShape.ToList();
this.logger.Debug(“找到ID为{0}名称为{1}的图表,结果[0]。NonVisualGraphicFrameProperties.NonVisualDrawingProperties.ID,结果[0]。NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name”);
var ppchart=新ppchart(结果[0]。非可视化FrameProperties.NonVisualDrawingProperties.Id);
ppchart.ShapeName=result[0]。非可视化FrameProperties.NonVisualDrawingProperties.Name;
图表列表。添加(ppchart);
}
}
返回图表列表;
}
///
///确定滑块是否具有此图表。
///
///身份证。
///gframe。
///
///如果幻灯片上有图表,则为true;否则,错误。
///
private bool HasThisChart(字符串id,GraphicFrame gframe)
{
var returnValue=false;
if(!(gframe==null)和&this.HasGraphic(gframe))
{
如果(!(gframe.Graphic.GraphicData==null))
{
var graphicData=gframe.Graphic.graphicData;
var drawChartsRef=graphicData.subjects();
如果(!(drawChartsRef==null))
{
foreach(drawChartsRef中的var drawChart)
{
if(drawChart.Id==Id)
{
returnValue=true;
}
}
}
}
}
返回值;
}
///
///确定指定的GraphicFrame是否有图形(图形是图表!)。
///
///gframe。
///
///如果指定的gframe具有图形,则为true;否则,错误。
///
private bool HasGraphic(GraphicFrame gframe)
{
var returnValue=false;
如果(!(gframe==null))
{
var graphicsdegenders=gframe.subgenders();
如果(graphicsublications.Count()>0)
{
returnValue=true;
}
}
返回值;
}
我找到了一个解决方案,但这个解决方案相当复杂:
希望它能帮助将来的某个人
/// <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;
}
//
///获取此幻灯片上所有图表的列表
///
///幻灯片部分。
///此幻灯片上所有图表的列表
私有列表从幻灯片获取图表(幻灯片部分幻灯片部分)
{
var chartList=新列表();
if(slidepart.ChartParts.Any())
{
foreach(slidepart.ChartParts中的var图表)
{
////获取图表部件的ID
var id=slidepart.GetIdOfPart(图表);
////获取包含图表的所有形状(图形框架)的列表
var gshapes=来自slidepart.Slide.subjections()中的shapeDesc选择shapeDesc;
var tempgshapes=gshapes.ToList();
////选择所有可能具有图形的形状
var thisShape=从tempgShape中的Gshape中选择,其中this.HasThisChart(id,Gshape)选择Gshape;
var result=thisShape.ToList();
this.logger.Debug(“找到ID为{0}名称为{1}的图表,结果[0]。NonVisualGraphicFrameProperties.NonVisualDrawingProperties.ID,结果[0]。NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name”);
var ppchart=新ppchart(结果[0]。非可视化FrameProperties.NonVisualDrawingProperties.Id);
ppchart.ShapeName=result[0]。非可视化FrameProperties.NonVisualDrawingProperties.Name;
图表列表。添加(ppchart);
}
}
返回图表列表;
}
///
///确定滑块是否具有此图表。
///
///身份证。
///gframe。
///
///如果幻灯片上有图表,则为true;否则,错误。
///
private bool HasThisChart(字符串id,GraphicFrame gframe)
{
var returnValue=false;
if(!(gframe==null)和&this.HasGraphic(gframe))
{
如果(!(gframe.Graphic.GraphicData==null))
{
var graphicData=gframe.Graphic.graphicData;
var drawChartsRef=graphicData.subjects();
如果(!(drawChartsRef==null))
{
foreach(drawChartsRef中的var drawChart)