c# - 如何根据使用的布局在幻灯片上添加文本框
问题描述
情况如下: 我有一个包含 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
推荐阅读
- shell - 如何通过计算前缀的使用次数来删除文件?
- c++ - 如何创建自定义 shared_ptr?
- php - 连接到我的 SQL 数据库以在我的表中添加值时遇到问题
- c# - 如何将以下 JSON 字符串转换为字典 C#
- c# - sql选择某门课程的先决条件
- python-3.x - 每次满足特定条件时,如何找到行之间的最大值?
- javascript - 需要两次点击才能提交表单 - JQuery(如 stackoverflow 审查问题)
- javascript - 对象不继承属性和值
- javascript - 我如何使用 vanilla javascript 找到具有特定类的子元素的数量
- c# - 为什么我的 C# 代码没有打印所需的输出