gpt4 book ai didi

c# - PowerPoint 互操作 : Get Placeholder by Name defined in Master

转载 作者:行者123 更新时间:2023-11-30 18:31:31 25 4
gpt4 key购买 nike

在我的 PowerPoint 插件中,我想访问幻灯片上的形状。这些形状是在 slidemaster 的自定义布局中定义的占位符。

当我根据自定义布局添加幻灯片时,形状只是命名为“占位符 1”、“占位符 2”...

有没有办法通过master给定的名字来获取占位符?

目前我正在使用这段代码搜索形状:

public static Shape GetShape(string stringToSearch, Shapes shapes) {

foreach (Shape shape in shapes) {

if (shape.Name == stringToSearch) {
return shape;
}

// Search Groups
if (shape.Type == MsoShapeType.msoGroup) {
foreach (Shape childshape in shape.GroupItems) {
if (childshape.Name == stringToSearch) {
return childshape;
}
}
}
}

throw new KeyNotFoundException("No Shape found");
}

更新:也许为了更清楚,这是 PowerPoint-Presentation 的结构。

为占位符定义名称的母版: Scrennshot of the master

在 master 中定义的名称丢失的演示: Screenshot of the presentation

问题:如何通过在 master 中定义的名称获取演示文稿中的元素?

最佳答案

嗯..这是史蒂夫的“丑陋!”解决方案。

对于我的项目,我既没有也不想控制形状的创建,所以我不能“标记”它们。这就是为什么我有一个自定义的占位符名称来标识它们,所以形状的名称确实是 [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,是吗?

关于c# - PowerPoint 互操作 : Get Placeholder by Name defined in Master,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20002401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com