情况如下:我有一个包含50个布局的演示文稿。他们可以分为3种类型:一个没有标题,另一个标题幻灯片的底部,第三个标题上面。我准备了PPT插件(c#中的VSTO),在每个幻灯片上添加文本框(具有特定格式):
using System;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Drawing;
namespace TitleAddin
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, EventArgs e)
{
this.Application.PresentationNewSlide +=
new PowerPoint.EApplication_PresentationNewSlideEventHandler(
Application_PresentationNewSlide);
}
private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
}
void Application_PresentationNewSlide(PowerPoint.Slide Sld)
{
PowerPoint.Shape textBox = Sld.Shapes.AddTextbox(
Office.MsoTextOrientation.msoTextOrientationHorizontal, 21, 19, 900, 100);
PowerPoint.TextRange dash = textBox.TextFrame.TextRange.InsertAfter("— n");
PowerPoint.TextRange title = textBox.TextFrame.TextRange.InsertAfter("Title n");
PowerPoint.TextRange subtitle = textBox.TextFrame.TextRange.InsertAfter("Subtitle");
dash.Font.Name = "Calibri";
dash.Font.Size = 24;
dash.Font.Bold = Office.MsoTriState.msoCTrue;
dash.Font.Color.RGB = Color.FromArgb(15,0,255).ToArgb();
title.Font.Name = "Calibri";
title.Font.Size = 24;
title.Font.Bold = Office.MsoTriState.msoCTrue;
title.Font.Color.RGB = Color.FromArgb(0,0,0).ToArgb();
subtitle.Font.Name = "Calibri Light";
subtitle.Font.Size = 24;
subtitle.Font.Bold = Office.MsoTriState.msoFalse;
subtitle.Font.Color.RGB = Color.FromArgb(0, 0, 0).ToArgb();
}
问题是:我不知道如何识别幻灯片使用的布局。我认为我可以在布局中添加一个不可见的形状并使用它的ID,但是添加到布局中的形状并不存在于幻灯片的xml代码中。脚本应该根据所选择的布局(我提到的这3种类型的布局)添加特定的文本。
注意:重要的是,我不想在特定的布局上添加占位符,而是在幻灯片上添加纯文本框,但这取决于它所基于的布局。比如:当我点击"添加新幻灯片"时当我选择layout2时,它会添加textbox1,当我选择layout5时,它会添加textbox2等。
你能帮我找到解决方案或者给我指出我应该使用的PPT对象吗?
我是一个VBA家伙,你必须查找c#等效。这段代码不是专门用来解决问题的,而是展示了如何查找布局名称并根据该名称对幻灯片执行操作:
Dim objSlide As Slide, LayoutIndex as Integer
For Each objSlide In ActivePresentation.Slides
If objSlide.Design.Index = 1 Then 'Ignore slides based on other masters
LayoutIndex(objSlide.SlideIndex) = objSlide.CustomLayout.Index 'Index is not absolute, but relative to slide master
End If
Select Case objSlide.CustomLayout.Name
Case "Text Page", "Text Page No Bullets", "Text Page - Two Columns"
For Each objShape In objSlide.Shapes 'Move subtitles to Title placeholders
If objShape.Type = msoPlaceholder Then
If objShape.PlaceholderFormat.Type = ppPlaceholderBody And objShape.Top > 25 And objShape.Top < 70 _
And objShape.HasTextFrame And objShape.TextFrame2.HasText Then
'Do stuff here
End If
End If
Next objShape
End Select
End Sub
与@John Korchok的回答类似,获得幻灯片布局信息的唯一方法确实只需要知道它的名称。
假设您想要在一个开放的演示文稿中获得第一张幻灯片的名称,c#语法将是:
var layoutName = presentation.Slides[1].CustomLayout.Name;
然后,您可以根据其名称从幻灯片母版获取自定义布局:
var layout = (from CustomLayout cl in slideMaster.CustomLayouts
where cl.MatchingName.Equals(layoutName)
select cl).FirstOrDefault();