本文介紹透過C#和VB.NET程式碼來建立和編輯PPT檔案中的SmartArt圖形。文中將分兩個操作範例來演示建立和編輯結果。
使用工具:Spire.Presentation for .NET hotfix 5.9.5
Dll檔案引用:
方式1:下載包。下載後,解壓,開啟Bin資料夾,根據自己用的.NET Framework選擇相應的資料夾,如:此範例中使用的是NET4.0,即開啟NET4.0檔案,找到Spire.Presentation.dll檔案。找到c#教學dll檔案後,在vs程式中新增引用該dll。
如下引用結果:
方式2:透過Nuget搜尋下載匯入。
註:建立SmartArt圖形時,可建立80多種不同型態的圖形,編輯圖形是,可新增、刪除節點、編輯節點內容、給節點內容設定超連結(包括連結到網頁、連結到幻燈片)
範例1. 建立PPT SmartArt圖形
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | using Spire.Presentation; using Spire.Presentation.Diagrams; namespace AddSmartArt {<!-- --> class Program {<!-- --> static void Main(string[] args) {<!-- --> //例項化Presentation物件 Presentation ppt = new Presentation(); //設定幻燈片大小 ppt.SlideSize.Type = SlideSizeType.Screen16x9; //新增組織結構圖型別的SmartArt圖形,並指定位置、大小 ISmartArt smartArt = ppt.Slides[0].Shapes.AppendSmartArt(100, 50, 450, 250, SmartArtLayoutType.OrganizationChart); //設定SmartArt的樣式和顏色 smartArt.Style = SmartArtStyleType.IntenceEffect; smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4; //移除預設的形狀(Node即代表SmartArt中的形狀) foreach (ISmartArtNode node in smartArt.Nodes) {<!-- --> smartArt.Nodes.RemoveNode(node); } //新增形狀並在其下面新增巢狀子形狀 ISmartArtNode node1 = smartArt.Nodes.AddNode(); ISmartArtNode node1_1 = node1.ChildNodes.AddNode(); ISmartArtNode node1_1_1 = node1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_2 = node1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_3 = node1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_4 = node1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_5 = node1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_6 = node1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_1_1 = node1_1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_1_2 = node1_1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_1_3 = node1_1_1.ChildNodes.AddNode(); ISmartArtNode node1_1_3_1 = node1_1_3.ChildNodes.AddNode(); ISmartArtNode node1_1_3_2 = node1_1_3.ChildNodes.AddNode(); ISmartArtNode node1_1_6_1 = node1_1_6.ChildNodes.AddNode(); ISmartArtNode node1_1_6_2 = node1_1_6.ChildNodes.AddNode(); ISmartArtNode node1_1_6_3 = node1_1_6.ChildNodes.AddNode(); //在每一個形狀上新增文字 node1.TextFrame.Text = "董事會 " + "Board of Directors"; node1_1.TextFrame.Text = "總經理 " + "General Manager"; node1_1_1.TextFrame.Text = "供應部 " + "Supply Dept."; node1_1_2.TextFrame.Text = "行銷部 " + "Sales Dept."; node1_1_3.TextFrame.Text = "生產部 " + "Productive Dept."; node1_1_4.TextFrame.Text = "財務部 " + "Finance Dept."; node1_1_5.TextFrame.Text = "人力資源部 " + "HR Dept."; node1_1_6.TextFrame.Text = "質檢中心 " + "Quality Center"; node1_1_1_1.TextFrame.Text = "採購部 " + "Purchase Dept."; node1_1_1_2.TextFrame.Text = "倉庫管理 " + "Warehouse Manager"; node1_1_1_3.TextFrame.Text = "物流部 " + "Logistics Dept."; node1_1_3_1.TextFrame.Text = "生產車間 " + "Production Dept."; node1_1_3_2.TextFrame.Text = "維修部 " + "Maintenance Dept."; node1_1_6_1.TextFrame.Text = "生產品質管理 " + "Production Quality Mgt."; node1_1_6_2.TextFrame.Text = "生產安全管理 " + "Production Safety Mgt."; node1_1_6_3.TextFrame.Text = "環境管理 " + "Environmental Mgt."; //儲存檔案 ppt.SaveToFile("result.pptx", FileFormat.Pptx2013); System.Diagnostics.Process.Start("result.pptx"); } } } |
圖形建立結果:
VB.NET
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | Imports Spire.Presentation Imports Spire.Presentation.Diagrams Namespace AddSmartArt Class Program Private Shared Sub Main(args As String()) '例項化Presentation物件 Dim ppt As New Presentation() '設定幻燈片大小 ppt.SlideSize.Type = SlideSizeType.Screen16x9 '新增組織結構圖型別的SmartArt圖形,並指定位置、大小 Dim smartArt As ISmartArt = ppt.Slides(0).Shapes.AppendSmartArt(100, 50, 750, 450, SmartArtLayoutType.OrganizationChart) '設定SmartArt的樣式和顏色 smartArt.Style = SmartArtStyleType.IntenceEffect smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4 '移除預設的形狀(Node即代表SmartArt中的形狀) For Each node As ISmartArtNode In smartArt.Nodes smartArt.Nodes.RemoveNode(node) Next '新增形狀並在其下面新增巢狀子形狀 Dim node1 As ISmartArtNode = smartArt.Nodes.AddNode() Dim node1_1 As ISmartArtNode = node1.ChildNodes.AddNode() Dim node1_1_1 As ISmartArtNode = node1_1.ChildNodes.AddNode() Dim node1_1_2 As ISmartArtNode = node1_1.ChildNodes.AddNode() Dim node1_1_3 As ISmartArtNode = node1_1.ChildNodes.AddNode() Dim node1_1_4 As ISmartArtNode = node1_1.ChildNodes.AddNode() Dim node1_1_5 As ISmartArtNode = node1_1.ChildNodes.AddNode() Dim node1_1_6 As ISmartArtNode = node1_1.ChildNodes.AddNode() Dim node1_1_1_1 As ISmartArtNode = node1_1_1.ChildNodes.AddNode() Dim node1_1_1_2 As ISmartArtNode = node1_1_1.ChildNodes.AddNode() Dim node1_1_1_3 As ISmartArtNode = node1_1_1.ChildNodes.AddNode() Dim node1_1_3_1 As ISmartArtNode = node1_1_3.ChildNodes.AddNode() Dim node1_1_3_2 As ISmartArtNode = node1_1_3.ChildNodes.AddNode() Dim node1_1_6_1 As ISmartArtNode = node1_1_6.ChildNodes.AddNode() Dim node1_1_6_2 As ISmartArtNode = node1_1_6.ChildNodes.AddNode() Dim node1_1_6_3 As ISmartArtNode = node1_1_6.ChildNodes.AddNode() '在每一個形狀上新增文字 node1.TextFrame.Text = "董事會" & vbLf + "Board of Directors" node1_1.TextFrame.Text = "總經理" & vbLf + "General Manager" node1_1_1.TextFrame.Text = "供應部" & vbLf + "Supply Dept." node1_1_2.TextFrame.Text = "行銷部" & vbLf + "Sales Dept." node1_1_3.TextFrame.Text = "生產部" & vbLf + "Productive Dept." node1_1_4.TextFrame.Text = "財務部" & vbLf + "Finance Dept." node1_1_5.TextFrame.Text = "人力資源部" & vbLf + "HR Dept." node1_1_6.TextFrame.Text = "質檢中心" & vbLf + "Quality Center" node1_1_1_1.TextFrame.Text = "採購部" & vbLf + "Purchase Dept." node1_1_1_2.TextFrame.Text = "倉庫管理" & vbLf + "Warehouse Manager" node1_1_1_3.TextFrame.Text = "物流部" & vbLf + "Logistics Dept." node1_1_3_1.TextFrame.Text = "生產車間" & vbLf + "Production Dept." node1_1_3_2.TextFrame.Text = "維修部" & vbLf + "Maintenance Dept." node1_1_6_1.TextFrame.Text = "生產品質管理" & vbLf + "Production Quality Mgt." node1_1_6_2.TextFrame.Text = "生產安全管理" & vbLf + "Production Safety Mgt." node1_1_6_3.TextFrame.Text = "環境管理" & vbLf + "Environmental Mgt." '儲存檔案 ppt.SaveToFile("result.pptx", FileFormat.Pptx2013) System.Diagnostics.Process.Start("result.pptx") End Sub End Class End Namespace |
範例2. 編輯PPT SmartArt圖形
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 | using Spire.Presentation; using Spire.Presentation.Diagrams; namespace ModifySmartArt {<!-- --> class Program {<!-- --> static void Main(string[] args) {<!-- --> //載入PPT幻燈片檔案 Presentation ppt = new Presentation(); ppt.LoadFromFile("test.pptx"); //取得SmartArt圖形的節點集合 ISmartArt smartart = ppt.Slides[0].Shapes[0] as ISmartArt; ISmartArtNodeCollection nodes = smartart.Nodes; //更改節點內容 nodes[1].TextFrame.Text = "新修改的節點內容"; //新增超連結到節點 nodes[2].Click = new ClickHyperlink("https://baike.baidu.com/");//新增指向網頁的超連結 nodes[3].Click = new ClickHyperlink(ppt.Slides[1]);//新增指向指定幻燈片的超連結 //新增節點 ISmartArtNode newnode = nodes[5].ChildNodes.AddNode(); newnode.TextFrame.Text = "新新增的節點內容"; //刪除節點 //nodes[0].ChildNodes[3].ChildNodes.RemoveNodeByPosition(0); //儲存到本地並開啟 ppt.SaveToFile("output.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("output.pptx"); } } } |
新增超連結後,注意要在幻燈片播放下才可見超連結新增效果:
VB.NET
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 | Imports Spire.Presentation Imports Spire.Presentation.Diagrams Namespace ModifySmartArt Class Program Private Shared Sub Main(args As String()) '載入PPT幻燈片檔案 Dim ppt As New Presentation() ppt.LoadFromFile("test.pptx") '取得SmartArt圖形的節點集合 Dim smartart As ISmartArt = TryCast(ppt.Slides(0).Shapes(0), ISmartArt) Dim nodes As ISmartArtNodeCollection = smartart.Nodes '更改節點內容 nodes(1).TextFrame.Text = "新修改的節點內容" '新增超連結到節點 nodes(2).Click = New ClickHyperlink("https://baike.baidu.com/") '新增指向網頁的超連結 nodes(3).Click = New ClickHyperlink(ppt.Slides(1)) '新增指向指定幻燈片的超連結 '新增節點 Dim newnode As ISmartArtNode = nodes(5).ChildNodes.AddNode() newnode.TextFrame.Text = "新新增的節點內容" '刪除節點 'nodes[0].ChildNodes[3].ChildNodes.RemoveNodeByPosition(0); '儲存到本地並開啟 ppt.SaveToFile("output.pptx", FileFormat.Pptx2010) System.Diagnostics.Process.Start("output.pptx") End Sub End Class End Namespace |