看到,因为仍然没有答案,这在这里或其他地方我还不如后我的代码。
例如,如果占位符形状是在幻灯片上移动,
这就是我想出了以处理:各种形状的
- 店的位置
- 重置幻灯片布局
- 匹配滑动的形状和主滑动形状
- 恢复所有形状的位置。
这是一个函数,它返回一个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("KnownPlaceholderName"))
Powerpoint.Shape KnownShape = theSlide[shapeMasters["KnownPlaceholderName"]];
这里是比较函数有两个形状和检查,如果他们是“平等”。可以扩展以使其更加精确。
存储原来的形状位置
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;
}
这是从C#VSTO代码加载,但我想这是不是从VB或其他PPT自动化类型的不同。
谢谢,宅男,但那不行。 PlaceholderFormat.Type不是索引,因为您在此使用它。此外,您的解决方案似乎意味着布局中每种类型的占位符不超过一个。 – OfficeAddinDev
我不确定您的意思是“PlaceholderFormat.Type不是索引” - 它是一个枚举。例如,1是“ppPlaceholderTitle”,2是“ppPlaceholderBody”。这就是你如何找出占位符形状的父项 - 除非你不是在寻找并且需要别的东西。此外,可以有充足的占位符的幻灯片上的 - 我没有提供一个“解决方案”本身,只是一些示例代码,让你进一步完善它,因为你需要,检查其他占位符等 –
因此,假设幻灯片上有4个占位符,类型为ppPlaceholderChart,并填充图表。如何使用此方法在幻灯片的布局中引用与幻灯片上的第二个图表相对应的占位符? mySlide.CustomLayout.Shapes.Placeholders(ppPlaceholderChart)返回的4个占位符ShapeRange,但如何可以告诉它们中的哪对应于第二图表? – OfficeAddinDev