VSTO开发向PPT中代码插入文本和图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private void AddTextBox(PowerPoint.Slide slide, string txtContent)
{
PowerPoint.Shape textbox;
textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50);//向当前PPT添加文本框
textbox.TextFrame.TextRange.Text = txtContent;//设置文本框的内容
textbox.TextFrame.TextRange.Font.Size = 48;//设置文本字体大小
textbox.TextFrame.TextRange.Font.Color.RGB = Color.DarkViolet.ToArgb();//设置文本颜色
}
private void AddPicture(PowerPoint.Slide slide, PowerPoint.Shape shape, string filePath)
{
PowerPoint.Shape pic;
pic = slide.Shapes.AddPicture(filePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, shape.Left, shape.Top, shape.Width, shape.Height);
pic.Name = "广告图片";
pic.Height = shape.Height;
pic.Width = shape.Width;
}
private void AddTextMessage(PowerPoint.Slide slide, PowerPoint.Shape shape, string txtContent)
{
PowerPoint.Shape textbox;
textbox = slide.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, shape.Left, shape.Top, shape.Width, shape.Height);//向当前PPT添加文本框
textbox.Height = shape.Height;
textbox.Width = shape.Width;
textbox.TextFrame.TextRange.Text = txtContent;
textbox.TextFrame.TextRange.Font.Color.RGB = Color.Orange.ToArgb();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Microsoft.Office.Interop.PowerPoint.Presentation MyPres = null;//PPT应用的实例
Microsoft.Office.Interop.PowerPoint.Slide MySlide = null;//PPT中的幻灯片         
MyPres = Globals.ThisAddIn.Application.ActivePresentation; // 当前ppt应用实例         
MySlide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide;  //获取当前选中的幻灯片
 
PowerPoint.Shape bg; //声明一个shape
bg = slide.Shapes.AddShape(Office.MsoAutoShapeType.msoShapeRectangle, 0, 0, MyPres.PageSetup.SlideWidth, 70);  //添加一个shape,形状为矩形(msoShapeRectangle)
bg.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.LightGray); //设置它的背景填充色为灰色
bg.Line.Visible = Office.MsoTriState.msoFalse;       //去掉边框
bg.TextFrame.TextRange.Text=“hello”  //设置它的插入文本内容
bg.TextFrame.HorizontalAnchor = Office.MsoHorizontalAnchor.msoAnchorCenter;  //文本格式居中
bg .TextFrame.TextRange.Font.Size = 20;  //文本字号
bg .Visible = Office.MsoTriState.msoFalse //设置自选图形为不可见状态
 
PowerPoint.Shape pic;        
string picParh = "http://ww3.sinaimg.cn/mw690/be159dedgw1evgxdt9h3fj218g0xctod.jpg";         
pic = MySlide.Shapes.AddPicture(picParh, Office.MsoTriState.msoTrue, Office.MsoTriState.msoTrue, 50, 50, 100, 100); //下载一个网络图片,插入至当前幻灯片,并设置相关参数

  PPT中添加对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="filePath"></param>
        private void AddADPicture(PowerPoint.Slide slide, string filePath)
        {
            PowerPoint.Shape pic;
            pic = slide.Shapes.AddPicture(filePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, 100, 200, 100, 100);
            pic.Name = "GlodonAD";
        }
 
        /// <summary>
        /// 添加多媒体文件
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="filePath"></param>
        private void AddMedia(PowerPoint.Slide slide, string filePath)
        {
            PowerPoint.Shape media;
            media = slide.Shapes.AddMediaObject(filePath, 10, 10, 200, 200);
            media.Name = "Media";
        }
 
        //对于OLED对象标识:http://msdn.microsoft.com/zh-cn/library/office/ff746158.aspx
         
        /// <summary>
        /// 通过ClassName创建对象
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="className"></param>
        private void AddOLEDClass(PowerPoint.Slide slide, string className)
        {
            PowerPoint.Shape oledClass;
            oledClass = slide.Shapes.AddOLEObject(50, 50, 800, 500, ClassName: className, DisplayAsIcon: Office.MsoTriState.msoTrue);
            oledClass.Name = "OLEDClassName";
        }
 
        /// <summary>
        /// 通过文件路径,添加文件
        /// </summary>
        /// <param name="slide"></param>
        /// <param name="filePath"></param>
        private void AddOLEDPath(PowerPoint.Slide slide, string filePath)
        {
            PowerPoint.Shape oledFile;
            oledFile = slide.Shapes.AddOLEObject(50, 50, 800, 500, FileName: filePath, DisplayAsIcon: Office.MsoTriState.msoTrue);//, DisplayAsIcon: Office.MsoTriState.msoTrue
            oledFile.Name = "OLEDFilePath";
        }

  PPT中添加对象调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string filePath = "E:\\FF.doc";
                filePath = "E:\\FF.ppt";
                filePath = "E:\\FF.xls";
 
                filePath = @"C:\Users\Administrator\Videos\PB043926.AVI";
                filePath = @"C:\Users\Administrator\Videos\轻轻的问候.swf";
 
                //AddMedia(slide, filePath);
 
                AddOLEDPath(slide, filePath);
 
                string className = "Excel.Sheet";
                className = "Word.Document";
                className = "PowerPoint.Slide";
                // AddOLEDClass(slide, className);

  

posted @   多见多闻  阅读(507)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示