VSTO二次開發PPT外掛
阿新 • • 發佈:2022-03-06
c#生成PPT總結(用Microsoft.Office.Interop外掛) 引用自:https://bbs.csdn.net/topics/391937345 1.在專案中新增引用 Microsoft PowerPoint 14.0 Object Library 2. using Microsoft.Office.Interop.PowerPoint; 3.建立一個PPT,新增一個空白頁 Microsoft.Office.Interop.PowerPoint.Application PPT = new Microsoft.Office.Interop.PowerPoint.Application();//建立PPT應用 Microsoft.Office.Interop.PowerPoint.Presentation MyPres = null;//PPT應用的例項 Microsoft.Office.Interop.PowerPoint.Slide MySlide = null;//PPT中的幻燈片 MyPres = PPT.Presentations.Open("檔案路徑", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);//此處將一個PPT例項給了MyPres MySlide = MyPres.Slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank);//像PPT例項中,新增一個空白頁,位置是“第一頁” 4.向PPT的幻燈片中新增元素 4.1文字框 Microsoft.Office.Interop.PowerPoint.TextRange MyTextRng = null; MySlide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 21.5F, 365F, 670F, 270F); MyTextRng = MySlide.Shapes[1].TextFrame.TextRange;//請注意此處Shapes的索引,由於文字框是第一個新增的Shapes,所以此處索引是1。 MyTextRng.Font.NameFarEast = "微軟雅黑";//文字框中,中文的字型 MyTextRng.Font.NameAscii = "Calibri";//文字框中,英文和數字的字型 MyTextRng.Text ="C#生成PPT";//顯示的內容 MyTextRng.Font.Bold = MsoTriState.msoTrue;//是否加粗 MyTextRng.Font.Color.RGB = A+ B * 256 + C * 256 * 256;//字型顏色,其中ABC直接用自定義顏色中的數字代替即可。 MyTextRng.Characters(1, 10).Font.Size = 24;//個性化設計。第1個字元開始,長度為10的字元,字型大小是24. MyTextRng.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignLeft;//文字對齊方式(水平方向) MySlide.Shapes[1].TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle; 文字對齊方式(垂直方向) 心得:最重要的設定在Font屬性中。其他設定,基本可以參考PPT中元素屬性的設定方式來找到。比如我在寫文字水平對齊方式時(左對齊,居中,右對齊),在PPT中,我們直接點選“段落”中的快捷鍵即可。所以我就找“段落”的英文,正好Alignment是對齊的意思,所以就找到了。 4.2 圖形(矩形) MySlide.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 8.5F, 6.5F, 705F, 525F); MySlide.Shapes[1].Line.ForeColor.RGB = A + B * 256 + C * 256 * 256;//改變線條顏色 MySlide.Shapes[1].Fill.Transparency = 1;//控制填充色為透明 MySlide.Shapes[1].Line.Style = MsoLineStyle.msoLineSingle;//改變線型裡的複合型別 MySlide.Shapes[1].Line.Weight = 1F;//改變線粗細 MySlide.Shapes[1].Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制陰影型別 MySlide.Shapes[1].Shadow.ForeColor.RGB = 0;//控制陰影顏色 MySlide.Shapes[1].Shadow.Transparency = 0.6F;//控制透明度 MySlide.Shapes[1].Shadow.Size = 100F;//控制大小 MySlide.Shapes[1].Shadow.Blur = 4F;//控制虛化 MySlide.Shapes[1].Shadow.OffsetX = 2.1F;//控制距離; MySlide.Shapes[1].Shadow.OffsetY = 2.1F;//與offsetX共同決定角度 心得:基本的一些設定,通過英文就可以辨別。不過有些屬性的設定是否與預期一致,需要等圖形生成後再進一步確認。 4.3 圖片 MySlide.Shapes.AddPicture("檔案路徑", MsoTriState.msoFalse, MsoTriState.msoTrue, 27F, 24F, 665F, 333F); 4.4 表格 Microsoft.Office.Interop.PowerPoint.Table MyTable = null; MyTable = MySlide.Shapes.AddTable(19, 5, 40F, 100F, 10F, 10F).Table;//建立時規定的寬和高,不是表格最終的大小。 MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.Size = 10; MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.Color.RGB = A + B * 256 + C * 256 * 256; MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.NameAscii = "Arial"; MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.NameFarEast = "微軟雅黑"; MyTable.Cell(k, j).Shape.TextFrame.TextRange.Font.Bold = MsoTriState.msoTrue; MyTable.Cell(k, j).Shape.TextFrame.TextRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.PowerPoint.PpParagraphAlignment.ppAlignCenter; MyTable.Cell(k, j).Shape.TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle; MyTable.Cell(k, j).Shape.Fill.ForeColor.RGB = 0; MyTable.Cell(k, j).Shape.TextFrame.TextRange.Text = "C#生成PPT"; 這裡的設定,幾乎和文字框的設定一樣。只不過需要先選定Cell。一些個性化的設計,比如合併拆分單元格,邊框顏色,按照一般的英文意思都能找到。 4.5 圖表 Microsoft.Office.Interop.PowerPoint.Chart MyChart = null;//圖表 Microsoft.Office.Interop.PowerPoint.ChartData MyChartData = null;//圖表的資料來源 Microsoft.Office.Interop.PowerPoint.Axis MyYvalaxis = null;//圖表的縱座標 Microsoft.Office.Interop.PowerPoint.Axis MyXvalaxis = null;//圖表的橫座標 Microsoft.Office.Interop.PowerPoint.DataLabels MyDataLabels = null;//圖表的資料標籤 Microsoft.Office.Interop.PowerPoint.Series MySeries = null;//資料系列 Microsoft.Office.Interop.PowerPoint.ChartGroups MyChartGroups = null;//資料系列-系列選項 Microsoft.Office.Interop.PowerPoint.Points MyPoints = null; //資料系列 MyChart = MySlide.Shapes.AddChart(Microsoft.Office.Core.XlChartType.xlColumnClustered, 35F, 205F, 642F, 227F).Chart;//新增柱形圖 MyChartData = MyChart.ChartData;//例項化資料來源 Microsoft.Office.Interop.Excel.Workbook MyDataWorkbook_2 = (Microsoft.Office.Interop.Excel.Workbook)MyChartData.Workbook;//由於PPT的資料來源是EXCEL工作表,所以此處還要呼叫EXCEL。 MyDataWorkbook_2.Application.WindowState = XlWindowState.xlMinimized;//不想看那麼多視窗,所以最小化了。 Microsoft.Office.Interop.Excel.Worksheet MyDataWorksheet_2 = (Microsoft.Office.Interop.Excel.Worksheet)MyDataWorkbook_2.Worksheets[1];//例項化工作表 Microsoft.Office.Interop.Excel.Range tRange_2 = MyDataWorksheet_2.Cells.get_Range("A1", "C10");//選定資料區域 Microsoft.Office.Interop.Excel.ListObject tbl1_2 = MyDataWorksheet_2.ListObjects[1]; tbl1_2.Resize(tRange_2); //賦值 ((Microsoft.Office.Interop.Excel.Range)(MyDataWorksheet_2.Cells.get_Range("A2"))).FormulaR1C1 = "全國得分"; ((Microsoft.Office.Interop.Excel.Range)(MyDataWorksheet_2.Cells.get_Range("A3"))).FormulaR1C1 = null; //圖表標題 MyChart.ChartTitle.Delete(); //縱軸 MyYvalaxis = (Microsoft.Office.Interop.PowerPoint.Axis)MyChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary); MyYvalaxis.MajorGridlines.Delete();//刪除主橫網路線 MyYvalaxis.MajorUnit = 0.5F; MyYvalaxis.MinimumScale = 0.0F; MyYvalaxis.MaximumScale = 1.5F; MyYvalaxis.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256; ;//座標軸顏色 MyYvalaxis.Format.Line.Transparency = 1F;//座標軸是否透明;此句必須先指定顏色,否則無效 MyYvalaxis.TickLabels.Delete();//刪除座標標籤 //橫軸 MyXvalaxis = (Microsoft.Office.Interop.PowerPoint.Axis)MyChart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlCategory, Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary); MyXvalaxis.MajorTickMark = Microsoft.Office.Interop.PowerPoint.XlTickMark.xlTickMarkOutside;//主要刻度線型別 MyXvalaxis.Format.Line.Weight = 0.75F;//線型寬度 MyXvalaxis.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256;//線條顏色 MyXvalaxis.TickLabelPosition = Microsoft.Office.Interop.PowerPoint.XlTickLabelPosition.xlTickLabelPositionNone; //圖例 MyChart.Legend.Delete(); //資料標籤格式和系列 //系列1 MySeries = (Microsoft.Office.Interop.PowerPoint.Series)MyChart.SeriesCollection(1); MySeries.HasDataLabels = true; MySeries.Format.Fill.ForeColor.RGB = A + B * 256 + C * 256 * 256; MySeries.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256; MySeries.Format.Line.Weight = 1.5F; MySeries.Format.Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制陰影型別 MySeries.Format.Shadow.ForeColor.RGB = 0;//控制陰影顏色 MySeries.Format.Shadow.Transparency = 0.6F;//控制透明度 MySeries.Format.Shadow.Size = 100F;//控制大小 MySeries.Format.Shadow.Blur = 4F;//控制虛化 MySeries.Format.Shadow.OffsetX = 2.1F;//控制距離; MySeries.Format.Shadow.OffsetY = 2.1F;//與offsetX共同決定角度 //柱子顏色 MyPoints = (Microsoft.Office.Interop.PowerPoint.Points)MySeries.Points(); MyPoints.Item(1).Format.Fill.ForeColor.RGB = A + B * 256 + B * 256 * 256;//系列1中,第1個柱子的顏色 //柱子距離 MyChartGroups = (Microsoft.Office.Interop.PowerPoint.ChartGroups)MyChart.ChartGroups(); MyChartGroups.Item(1).GapWidth = 50; //資料標籤 MyDataLabels = (Microsoft.Office.Interop.PowerPoint.DataLabels)MySeries.DataLabels(); MyDataLabels.Position = Microsoft.Office.Interop.PowerPoint.XlDataLabelPosition.xlLabelPositionOutsideEnd; MyDataLabels.NumberFormat = "0.0%"; MyDataLabels.Format.TextFrame2.TextRange.Font.Size = 9F; MyDataLabels.Format.TextFrame2.TextRange.Font.NameAscii = "Calibri"; MyDataLabels.Format.TextFrame2.TextRange.Font.Bold = MsoTriState.msoTrue; //系列2 MySeries = (Microsoft.Office.Interop.PowerPoint.Series)MyChart.SeriesCollection(2); MySeries.HasDataLabels = true; MySeries.Format.Fill.ForeColor.RGB = A + B * 256 + C * 256 * 256; MySeries.Format.Line.ForeColor.RGB = A + B * 256 + C * 256 * 256; MySeries.Format.Line.Weight = 1.5F; MySeries.Format.Shadow.Style = MsoShadowStyle.msoShadowStyleOuterShadow;//控制陰影型別 MySeries.Format.Shadow.ForeColor.RGB = 0;//控制陰影顏色 MySeries.Format.Shadow.Transparency = 0.6F;//控制透明度 MySeries.Format.Shadow.Size = 100F;//控制大小 MySeries.Format.Shadow.Blur = 4F;//控制虛化 MySeries.Format.Shadow.OffsetX = 2.1F;//控制距離 MySeries.Format.Shadow.OffsetY = 2.1F;//與offsetX共同決定角度 //柱子距離 MyChartGroups = (Microsoft.Office.Interop.PowerPoint.ChartGroups)MyChart.ChartGroups(); MyChartGroups.Item(1).GapWidth = 50; //資料標籤 MyDataLabels = (Microsoft.Office.Interop.PowerPoint.DataLabels)MySeries.DataLabels(); MyDataLabels.Position = Microsoft.Office.Interop.PowerPoint.XlDataLabelPosition.xlLabelPositionOutsideEnd; MyDataLabels.NumberFormat = "0.0%"; MyDataLabels.Format.TextFrame2.TextRange.Font.Size = 9F; MyDataLabels.Format.TextFrame2.TextRange.Font.NameAscii = "Calibri"; MyDataLabels.Format.TextFrame2.TextRange.Font.Italic = MsoTriState.msoTrue; ==*****************************== C# 建立幻燈片 //C# code using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Office.Core; using PowerPoint=Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Interop.Graph; using System.Runtime.InteropServices; using System.IO; namespace Example { public partial class Form1 : Form { public Form1() { InitializeComponent(); } String MyTemplateFile = Directory.GetCurrentDirectory() + "\\Blends.pot"; //瀏覽影象檔案 private void button1_Click(object sender, EventArgs e) { this.openFileDialog1.Filter="所有影象檔案(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg; *.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 影象檔案 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 影象檔案(*.gif)|*.gif |BMP 影象檔案(*.bmp)|*.bmp|Tiff 影象檔案(*.tif;*.tiff) |*.tif; *.tiff|Png 影象檔案(*.png)| *.png |所有檔案 (*.*)|*.*"; if(this.openFileDialog1.ShowDialog()==DialogResult.OK) { string MyFileName=this.openFileDialog1.FileName; this.pictureBox1.Image=Image.FromFile(MyFileName); } } //建立播放幻燈片 private void button2_Click(object sender, EventArgs e) { string MyFileName; PowerPoint.ApplicationClass MyApp; PowerPoint.Presentations MyPresSet; PowerPoint._Presentation MyPres; PowerPoint.Slides MySlides; PowerPoint._Slide MySlide; PowerPoint.SlideShowTransition MySST; PowerPoint.SlideShowSettings MySSS; PowerPoint.SlideRange MySldRng; PowerPoint.SlideShowWindows MySSWs; bool MyAssistantOn; int i, MyCount; PowerPoint.TextRange MyTextRng; Microsoft.Office.Interop.Graph.Chart MyChart; PowerPoint.Shapes MyShapes; PowerPoint.Shape MyShape; try { MyFileName = this.openFileDialog1.FileName; if (!File.Exists(MyFileName)) { MessageBox.Show("幻燈片模板檔案實際不存在!","資訊提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } MyApp = new PowerPoint.ApplicationClass(); MyApp.Visible = MsoTriState.msoCTrue; MyPresSet = MyApp.Presentations; MyPres = MyPresSet.Open(MyTemplateFile, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue); MySlides = MyPres.Slides; //建立幻燈片第一頁 MySlide=MySlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly); MyTextRng=MySlide.Shapes[1].TextFrame.TextRange; MyTextRng.Text="125555555555555555555552111111"; MyTextRng.Font.Name="宋體"; MyTextRng.Font.Size=48; MySlide.Shapes.AddPicture(MyFileName, MsoTriState.msoFalse,MsoTriState.msoTrue,150, 150, 500, 350); //建立幻燈片第二頁 MySlide=MySlides.Add(2, PowerPoint.PpSlideLayout.ppLayoutTitleOnly); MyTextRng=MySlide.Shapes[1].TextFrame.TextRange; MyTextRng.Text="演示繪製餅圖"; MyTextRng.Font.Name="宋體"; MyTextRng.Font.Size= 48; MyChart = (Microsoft.Office.Interop.Graph.Chart)MySlide.Shapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "", MsoTriState.msoFalse).OLEFormat.Object; MyChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xl3DPie; MyChart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionBottom; MyChart.HasTitle= true; MyChart.ChartTitle.Text="經典系列叢書2006年度圖書銷量"; //建立幻燈片第三頁 MySlide=MySlides.Add(3, PowerPoint.PpSlideLayout.ppLayoutBlank); MySlide.FollowMasterBackground=MsoTriState.msoFalse; MyShapes=MySlide.Shapes; MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"經典程式設計例項集錦", "Impact", 80,MsoTriState.msoFalse, MsoTriState.msoFalse, 50, 200); //建立幻燈片第四頁 MySlide=MySlides.Add(4, PowerPoint.PpSlideLayout.ppLayoutBlank); MySlide.FollowMasterBackground=MsoTriState.msoFalse; MyShapes=MySlide.Shapes; MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect30,"精彩程式設計例項集錦","Impact", 60,MsoTriState.msoFalse, MsoTriState.msoFalse, 150, 10); MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect29,"羅斌編著", "Impact", 80, MsoTriState.msoFalse, MsoTriState.msoFalse, 350, 250); //建立幻燈片第五頁 MySlide=MySlides.Add(5, PowerPoint.PpSlideLayout.ppLayoutBlank); MySlide.FollowMasterBackground=MsoTriState.msoFalse; MyShapes=MySlide.Shapes; MyShape=MyShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27,"The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200); MyCount=(int)MySlides.Count; int[] MySlideIdx=new int[MyCount]; for (i = 0; i < MyCount; i++) { MySlideIdx[i]=i+1; } MySldRng = MySlides.Range(MySlideIdx); MySST=MySldRng.SlideShowTransition; MySST.AdvanceOnTime=MsoTriState.msoTrue; MySST.AdvanceTime=1; MySST.EntryEffect=PowerPoint.PpEntryEffect.ppEffectBoxOut; MyAssistantOn=MyApp.Assistant.On; MyApp.Assistant.On=false; MySSS= MyPres.SlideShowSettings; MySSS.StartingSlide=1; MySSS.EndingSlide=1; MySSS.Run(); MySSWs= MyApp.SlideShowWindows; while (MySSWs.Count >= 1) { System.Threading.Thread.Sleep(2000); } if (MyAssistantOn) { MyApp.Assistant.On = true; MyApp.Assistant.Visible = false; } MyPres.Close(); MyApp.Quit(); } catch (Exception MyEx) { MessageBox.Show(MyEx.Message, "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void Form1_Load(object sender, EventArgs e) { } } } C#操作PowerPoint的基本程式碼 包括開啟ppt檔案、讀取幻燈頁,插入幻燈片,自動播放等 using System; using System.Collections.Generic; using System.Linq; using System.Text; using OFFICECORE = Microsoft.Office.Core; using POWERPOINT = Microsoft.Office.Interop.PowerPoint; using System.Windows; using System.Collections; using System.Windows.Controls; namespace PPTDraw.PPTOperate { /// <summary> /// PPT文件操作實現類. /// </summary> public class OperatePPT { #region=========基本的引數資訊======= POWERPOINT.Application objApp = null; POWERPOINT.Presentation objPresSet = null; POWERPOINT.SlideShowWindows objSSWs; POWERPOINT.SlideShowTransition objSST; POWERPOINT.SlideShowSettings objSSS; POWERPOINT.SlideRange objSldRng; bool bAssistantOn; double pixperPoint = 0; double offsetx = 0; double offsety = 0; #endregion #region===========操作方法============== /// <summary> /// 開啟PPT文件並播放顯示。 /// </summary> /// <param name="filePath">PPT檔案路徑</param> public void PPTOpen(string filePath) { //防止連續開啟多個PPT程式. if (this.objApp != null) { return; } try { objApp = new POWERPOINT.Application(); //以非只讀方式開啟,方便操作結束後儲存. objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse); //Prevent Office Assistant from displaying alert messages: bAssistantOn = objApp.Assistant.On; objApp.Assistant.On = false; objSSS = this.objPresSet.SlideShowSettings; objSSS.Run(); } catch (Exception ex) { this.objApp.Quit(); } } /// <summary> /// 自動播放PPT文件. /// </summary> /// <param name="filePath">PPTy檔案路徑.</param> /// <param name="playTime">翻頁的時間間隔.【以秒為單位】</param> public void PPTAuto(string filePath, int playTime) { //防止連續開啟多個PPT程式. if (this.objApp != null) { return; } objApp = new POWERPOINT.Application(); objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse); // 自動播放的程式碼(開始) int Slides = objPresSet.Slides.Count; int[] SlideIdx = new int[Slides]; for (int i = 0; i < Slides; i++) { SlideIdx[i] = i + 1; }; objSldRng = objPresSet.Slides.Range(SlideIdx); objSST = objSldRng.SlideShowTransition; //設定翻頁的時間. objSST.AdvanceOnTime = OFFICECORE.MsoTriState.msoCTrue; objSST.AdvanceTime = playTime; //翻頁時的特效! objSST.EntryEffect = POWERPOINT.PpEntryEffect.ppEffectCircleOut; //Prevent Office Assistant from displaying alert messages: bAssistantOn = objApp.Assistant.On; objApp.Assistant.On = false; //Run the Slide show from slides 1 thru 3. objSSS = objPresSet.SlideShowSettings; objSSS.StartingSlide = 1; objSSS.EndingSlide = Slides; objSSS.Run(); //Wait for the slide show to end. objSSWs = objApp.SlideShowWindows; while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(playTime * 100); this.objPresSet.Close(); this.objApp.Quit(); } /// <summary> /// PPT下一頁。 /// </summary> public void NextSlide() { if (this.objApp != null) this.objPresSet.SlideShowWindow.View.Next(); } /// <summary> /// PPT上一頁。 /// </summary> public void PreviousSlide() { if (this.objApp != null) this.objPresSet.SlideShowWindow.View.Previous(); } /// <summary> /// 對當前的PPT頁面進行圖片插入操作。 /// </summary> /// <param name="alImage">圖片物件資訊陣列</param> /// <param name="offsetx">插入圖片距離左邊長度</param> /// <param name="pixperPoint">距離比例值</param> /// <returns>是否新增成功!</returns> public bool InsertToSlide(List<PPTOBJ> listObj) { bool InsertSlide = false; if (this.objPresSet != null) { this.SlideParams(); int slipeint = objPresSet.SlideShowWindow.View.CurrentShowPosition; foreach (PPTOBJ myobj in listObj) { objPresSet.Slides[slipeint].Shapes.AddPicture( myobj.Path, //圖片路徑 OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoTrue, (float)((myobj.X - this.offsetx) / this.pixperPoint), //插入圖片距離左邊長度 (float)(myobj.Y / this.pixperPoint), //插入圖片距離頂部高度 (float)(myobj.Width / this.pixperPoint), //插入圖片的寬度 (float)(myobj.Height / this.pixperPoint) //插入圖片的高度 ); } InsertSlide = true; } return InsertSlide; } /// <summary> /// 計算InkCanvas畫板上的偏移引數,與PPT上顯示圖片的引數。 /// 用於PPT載入圖片時使用 /// </summary> private void SlideParams() { double slideWidth = this.objPresSet.PageSetup.SlideWidth; double slideHeight = this.objPresSet.PageSetup.SlideHeight; double inkCanWidth = SystemParameters.PrimaryScreenWidth;//inkCan.ActualWidth; double inkCanHeight = SystemParameters.PrimaryScreenHeight;//inkCan.ActualHeight ; if ((slideWidth / slideHeight) > (inkCanWidth / inkCanHeight)) { this.pixperPoint = inkCanHeight / slideHeight; this.offsetx = 0; this.offsety = (inkCanHeight - slideHeight * this.pixperPoint) / 2; } else { this.pixperPoint = inkCanHeight / slideHeight; this.offsety = 0; this.offsetx = (inkCanWidth - slideWidth * this.pixperPoint) / 2; } } /// <summary> /// 關閉PPT文件。 /// </summary> public void PPTClose() { //裝備PPT程式。 if (this.objPresSet != null) { //判斷是否退出程式,可以不使用。 //objSSWs = objApp.SlideShowWindows; //if (objSSWs.Count >= 1) //{ if (MessageBox.Show("是否儲存修改的筆跡!", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK) this.objPresSet.Save(); //} //this.objPresSet.Close(); } if (this.objApp != null) this.objApp.Quit(); GC.Collect(); } #endregion } } ==************************=========