好吧..这是史蒂夫的“丑陋!” 解决方案。
对于我的项目,我既没有也不想控制形状的创建,所以我不能“标记”它们。这就是为什么我有一个自定义占位符名称来识别它们,所以形状的名称确实是 [PlaceholderType] ##。
步骤:
- 存储所有形状的位置
- 重置幻灯片布局
- 匹配幻灯片形状和母版幻灯片形状
- 恢复所有形状的位置。
注意:我不使用形状组。如果您这样做并且您需要检查组内部,此技术将变得更加复杂。
这是执行此操作并返回 mastershapename - shapename 映射的函数。
private Dictionary<string, string> GetShapeMasters(Powerpoint.Slide s)
{
Dictionary<string, string> shapeMasters = new Dictionary<string, string>();
List<ShapeLocation> shapeLocations = new List<ShapeLocation>();
//store locations
foreach (Powerpoint.Shape sh in s.Shapes)
{
shapeLocations.Add(new ShapeLocation()
{
Name = sh.Name,
Location = new System.Drawing.RectangleF(sh.Left, sh.Top, sh.Width, sh.Height)
});
}
//have powerpoint reset the slide
//ISSUE: this changes the names of placeholders without content.
s.CustomLayout = s.CustomLayout;
//compare slide and master
foreach (Powerpoint.Shape sh in s.Shapes)
{
foreach (Powerpoint.Shape msh in s.CustomLayout.Shapes)
{
if (IsShapeMaster(sh, msh))
{
shapeMasters[msh.Name] = sh.Name;
}
}
}
//restore locations
//TODO: might be replaced by undo
foreach (var shm in shapeLocations)
{
Powerpoint.Shape sh = null;
try
{
sh = s.Shapes[shm.Name];
}
catch
{
//Fails for renamed placeholder shapes.
//Have yet to find a decent way to check if a shape name exists.
}
//placeholders do not need to be restored anyway.
if (sh != null)
{
sh.Left = shm.Location.Left;
sh.Top = shm.Location.Top;
sh.Width = shm.Location.Width;
sh.Height = shm.Location.Height;
}
}
return shapeMasters;
}
有了这个你可以做
Dictionary<string, string> shapeMasters = GetShapeMasters(theSlide);
if(shapeMasters.ContainsKey(stringToSearch))
Powerpoint.Shape KnownShape = theSlide[shapeMasters[stringToSearch]];
这是比较函数,它采用两种形状并检查它们是否“相等”。可以扩展以使其更精确。
private bool IsShapeMaster(Powerpoint.Shape sh, Powerpoint.Shape msh)
{
return
sh.Left == msh.Left
&& sh.Top == msh.Top
&& sh.Width == msh.Width
&& sh.Height == msh.Height
&& sh.Type == msh.Type
&& sh.PlaceholderFormat.Type == msh.PlaceholderFormat.Type;
}
存储原始形状位置的小类
class ShapeLocation
{
public string Name;
public System.Drawing.RectangleF Location;
}
我愿意接受建议,因为我也不喜欢这样。只有似乎没有其他方法可以将形状和占位符链接在一起。真的没有shape.MasterShape
我们缺少的东西,是吗?