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

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


1、打开PPT文件

// 打开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、添加幻灯片

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

3、获取指定的幻灯片

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

4、获取所有要素Shape

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

5、获取所有文字

// 获取文字
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、获取所有图片,并保存

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、插入一个文本框

//添加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属性设置

//控制填充色为透明
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、插入一个图片

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

10、保存、另存为

// 获取当前工程中的所有Layouts
IEnumerable<LayoutProjectItem> layouts = Project.Current.GetItems<LayoutProjectItem>();
// 按名称获取
LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals("MyLayout"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

规划GIS会

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值