1. 程式人生 > 其它 >VSTO開發向PPT中程式碼插入文字和圖片

VSTO開發向PPT中程式碼插入文字和圖片

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();
}
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中新增物件:

/// <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中新增物件呼叫:

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);