【ArcGIS Pro二次开发】(71):PPT文件操作方法汇总

以下操作都要用到【Microsoft.Office.Interop.PowerPoint】,确保安装并引用。


1、打开PPT文件

cs 复制代码
// 打开PPT
Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application();

Presentation ppt = pptApp.Presentations.Open(pptPath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

2、添加幻灯片

cs 复制代码
// 要添加的幻灯片的页码
int index = 4;
//添加幻灯片
Slide slide2 = ppt.Slides.Add(index, PpSlideLayout.ppLayoutBlank);

3、获取指定的幻灯片

cs 复制代码
// index为页码,从1开始,和PPT上显示的页码一致
Slide initSlide = ppt.Slides[index];

4、获取所有要素Shape

cs 复制代码
// 遍历所有幻灯片
foreach (Slide slide in ppt.Slides)
{
    // 遍历所有元素
    foreach (Shape shape in slide.Shapes)
    {
        // TODO
    }
}

5、获取所有文字

cs 复制代码
// 获取文字
List<string> stringList = new List<string>();
// 遍历所有幻灯片
foreach (Slide slide in ppt.Slides)
{
    // 遍历所有元素
    foreach (Shape shape in slide.Shapes)
    {
        if (shape.HasTextFrame == MsoTriState.msoTrue && shape.TextFrame.HasText == MsoTriState.msoTrue)
        {
            // 获取文字
            stringList.Add(shape.TextFrame.TextRange.Text.ToString());
        }
    }
}

6、获取所有图片,并保存

cs 复制代码
foreach (Shape shape in initSlide.Shapes)
{
    if (shape.Type == MsoShapeType.msoPicture)
    {
        // 复制到剪贴板
        shape.Copy();
        // 获取图片数据
        Image img = (Image)System.Windows.Forms.Clipboard.GetData(System.Windows.Forms.DataFormats.Bitmap);
        // 保存图片
        img.Save(@"C:\Users\Administrator\Desktop\new.png");
    }
}

7、插入一个文本框

cs 复制代码
//添加Shape
float x = 100;
float y = 100;
float width = 500;
float height = 300;
string text = "这是一个新插入的文本!!!!!!!!!";

Shape shape = slide.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, x, y, width, height);

//保存
ppt.Save();

8、Shape属性设置

cs 复制代码
//控制填充色为透明
shape.Fill.Transparency = 1;
//控制边框颜色为黑色
shape.Line.ForeColor.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(0, 0, 0));
//文字加粗
shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue;
//字体为黑色
shape.TextFrame.TextRange.Font.Color.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(0, 0, 0));
//字体
shape.TextFrame.TextRange.Font.NameFarEast = "微软雅黑";
//水平对齐
shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft;
//插入的文本
shape.TextFrame.TextRange.Text = text;
//字体大小
shape.TextFrame.TextRange.Font.Size=40;
//字体居中
shape.TextFrame.TextRange.ParagraphFormat.Alignment = PpParagraphAligrument.ppAlignCenter;
//文本框内容垂直居中
shape.TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;

9、插入一个图片

cs 复制代码
// 获取当前工程中的所有Layouts
IEnumerable<LayoutProjectItem> layouts = Project.Current.GetItems<LayoutProjectItem>();
// 按名称获取
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));

10、保存、另存为

cs 复制代码
// 获取当前工程中的所有Layouts
IEnumerable<LayoutProjectItem> layouts = Project.Current.GetItems<LayoutProjectItem>();
// 按名称获取
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
相关推荐
SpaceAIGlobal14 天前
AI 生成 PPT 工具深度评测与选型指南
人工智能·powerpoint
一头爱吃肉的牛14 天前
2026年10款AI PPT工具横向评测:内容准确性、生成速度、模板丰富度对比
人工智能·powerpoint
中科GIS地理信息培训14 天前
【ArcGIS Pro 3.7新增功能4】增强空间统计中【评估点聚合的图格大小】工具:分析字段和时间间隔
人工智能·算法·arcgis
雪的季节15 天前
GIS 矢量数据格式
arcgis
取个鸣字真的难15 天前
Image2 生成 PPT 的最后分水岭:Prompt
人工智能·prompt·powerpoint
非科班Java出身GISer15 天前
ArcGIS JS 基础教程(11):飞行定位 goTo
arcgis·arcgis js 飞行定位·arcgis js 定位·arcgis js 各种定位·arcgis js 飞行·arcgis js 定位到对象
我是Superman丶15 天前
前端技术手势识别
arcgis
m0_5474866616 天前
《数字图像处理:使用MATLAB分析与实现》全套课件PPT
开发语言·matlab·powerpoint
ShyanZh16 天前
【skill】HTML-PPT:36主题31布局的专业HTML演示文稿工作室
ai·html·powerpoint·html-ppt
Sour17 天前
【无标题】aPPT 翻译后保留版式的流程:文本框、图片、母版和动画检查
pdf·powerpoint