C# / VB.NET 在PPT中創(chuàng)建、編輯PPT SmartArt圖形的方法詳解
本文介紹通過C#和VB.NET程序代碼來創(chuàng)建和編輯PPT文檔中的SmartArt圖形。文中將分兩個(gè)操作示例來演示創(chuàng)建和編輯結(jié)果。
使用工具:Spire.Presentation for .NET hotfix 5.9.5
Dll文件引用:
方式1:下載包。下載后,解壓,打開Bin文件夾,根據(jù)自己用的.NET Framework選擇相應(yīng)的文件夾,如:此示例中使用的是NET4.0,即打開NET4.0文件,找到Spire.Presentation.dll文件。找到dll文件后,在vs程序中添加引用該dll。
如下引用結(jié)果:

方式2:通過Nuget搜索下載導(dǎo)入。
注:創(chuàng)建SmartArt圖形時(shí),可創(chuàng)建80多種不同類型的圖形,編輯圖形是,可添加、刪除節(jié)點(diǎn)、編輯節(jié)點(diǎn)內(nèi)容、給節(jié)點(diǎn)內(nèi)容設(shè)置超鏈接(包括鏈接到網(wǎng)頁、鏈接到幻燈片)
示例1. 創(chuàng)建PPT SmartArt圖形
using Spire.Presentation;
using Spire.Presentation.Diagrams;
namespace AddSmartArt
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化Presentation對(duì)象
Presentation ppt = new Presentation();
//設(shè)置幻燈片大小
ppt.SlideSize.Type = SlideSizeType.Screen16x9;
//添加組織結(jié)構(gòu)圖類型的SmartArt圖形,并指定位置、大小
ISmartArt smartArt = ppt.Slides[0].Shapes.AppendSmartArt(100, 50, 450, 250, SmartArtLayoutType.OrganizationChart);
//設(shè)置SmartArt的樣式和顏色
smartArt.Style = SmartArtStyleType.IntenceEffect;
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4;
//移除默認(rèn)的形狀(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();
//在每一個(gè)形狀上添加文字
node1.TextFrame.Text = "董事會(huì)\n" + "Board of Directors";
node1_1.TextFrame.Text = "總經(jīng)理\n" + "General Manager";
node1_1_1.TextFrame.Text = "供應(yīng)部\n" + "Supply Dept.";
node1_1_2.TextFrame.Text = "營銷部\n" + "Sales Dept.";
node1_1_3.TextFrame.Text = "生產(chǎn)部\n" + "Productive Dept.";
node1_1_4.TextFrame.Text = "財(cái)務(wù)部\n" + "Finance Dept.";
node1_1_5.TextFrame.Text = "人力資源部\n" + "HR Dept.";
node1_1_6.TextFrame.Text = "質(zhì)檢中心\n" + "Quality Center";
node1_1_1_1.TextFrame.Text = "采購部\n" + "Purchase Dept.";
node1_1_1_2.TextFrame.Text = "倉庫管理\n" + "Warehouse Manager";
node1_1_1_3.TextFrame.Text = "物流部\n" + "Logistics Dept.";
node1_1_3_1.TextFrame.Text = "生產(chǎn)車間\n" + "Production Dept.";
node1_1_3_2.TextFrame.Text = "維修部\n" + "Maintenance Dept.";
node1_1_6_1.TextFrame.Text = "生產(chǎn)質(zhì)量管理\n" + "Production Quality Mgt.";
node1_1_6_2.TextFrame.Text = "生產(chǎn)安全管理\n" + "Production Safety Mgt.";
node1_1_6_3.TextFrame.Text = "環(huán)境管理\n" + "Environmental Mgt.";
//保存文檔
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);
System.Diagnostics.Process.Start("result.pptx");
}
}
}
圖形創(chuàng)建結(jié)果:

VB.NET
Imports Spire.Presentation
Imports Spire.Presentation.Diagrams
Namespace AddSmartArt
Class Program
Private Shared Sub Main(args As String())
'實(shí)例化Presentation對(duì)象
Dim ppt As New Presentation()
'設(shè)置幻燈片大小
ppt.SlideSize.Type = SlideSizeType.Screen16x9
'添加組織結(jié)構(gòu)圖類型的SmartArt圖形,并指定位置、大小
Dim smartArt As ISmartArt = ppt.Slides(0).Shapes.AppendSmartArt(100, 50, 750, 450, SmartArtLayoutType.OrganizationChart)
'設(shè)置SmartArt的樣式和顏色
smartArt.Style = SmartArtStyleType.IntenceEffect
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4
'移除默認(rèn)的形狀(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()
'在每一個(gè)形狀上添加文字
node1.TextFrame.Text = "董事會(huì)" & vbLf + "Board of Directors"
node1_1.TextFrame.Text = "總經(jīng)理" & vbLf + "General Manager"
node1_1_1.TextFrame.Text = "供應(yīng)部" & vbLf + "Supply Dept."
node1_1_2.TextFrame.Text = "營銷部" & vbLf + "Sales Dept."
node1_1_3.TextFrame.Text = "生產(chǎn)部" & vbLf + "Productive Dept."
node1_1_4.TextFrame.Text = "財(cái)務(wù)部" & vbLf + "Finance Dept."
node1_1_5.TextFrame.Text = "人力資源部" & vbLf + "HR Dept."
node1_1_6.TextFrame.Text = "質(zhì)檢中心" & 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 = "生產(chǎn)車間" & vbLf + "Production Dept."
node1_1_3_2.TextFrame.Text = "維修部" & vbLf + "Maintenance Dept."
node1_1_6_1.TextFrame.Text = "生產(chǎn)質(zhì)量管理" & vbLf + "Production Quality Mgt."
node1_1_6_2.TextFrame.Text = "生產(chǎn)安全管理" & vbLf + "Production Safety Mgt."
node1_1_6_3.TextFrame.Text = "環(huán)境管理" & vbLf + "Environmental Mgt."
'保存文檔
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)
System.Diagnostics.Process.Start("result.pptx")
End Sub
End Class
End Namespace
示例2. 編輯PPT SmartArt圖形
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圖形的節(jié)點(diǎn)集合
ISmartArt smartart = ppt.Slides[0].Shapes[0] as ISmartArt;
ISmartArtNodeCollection nodes = smartart.Nodes;
//更改節(jié)點(diǎn)內(nèi)容
nodes[1].TextFrame.Text = "新修改的節(jié)點(diǎn)內(nèi)容";
//添加超鏈接到節(jié)點(diǎn)
nodes[2].Click = new ClickHyperlink("https://baike.baidu.com/");//添加指向網(wǎng)頁的超鏈接
nodes[3].Click = new ClickHyperlink(ppt.Slides[1]);//添加指向指定幻燈片的超鏈接
//添加節(jié)點(diǎn)
ISmartArtNode newnode = nodes[5].ChildNodes.AddNode();
newnode.TextFrame.Text = "新添加的節(jié)點(diǎn)內(nèi)容";
//刪除節(jié)點(diǎn)
//nodes[0].ChildNodes[3].ChildNodes.RemoveNodeByPosition(0);
//保存到本地并打開
ppt.SaveToFile("output.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("output.pptx");
}
}
}
添加超鏈接后,注意要在幻燈片播放下才可見超鏈接添加效果:

VB.NET
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圖形的節(jié)點(diǎn)集合
Dim smartart As ISmartArt = TryCast(ppt.Slides(0).Shapes(0), ISmartArt)
Dim nodes As ISmartArtNodeCollection = smartart.Nodes
'更改節(jié)點(diǎn)內(nèi)容
nodes(1).TextFrame.Text = "新修改的節(jié)點(diǎn)內(nèi)容"
'添加超鏈接到節(jié)點(diǎn)
nodes(2).Click = New ClickHyperlink("https://baike.baidu.com/")
'添加指向網(wǎng)頁的超鏈接
nodes(3).Click = New ClickHyperlink(ppt.Slides(1))
'添加指向指定幻燈片的超鏈接
'添加節(jié)點(diǎn)
Dim newnode As ISmartArtNode = nodes(5).ChildNodes.AddNode()
newnode.TextFrame.Text = "新添加的節(jié)點(diǎn)內(nèi)容"
'刪除節(jié)點(diǎn)
'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
到此這篇關(guān)于C# / VB.NET 在PPT中創(chuàng)建、編輯PPT SmartArt圖形的方法詳解的文章就介紹到這了,更多相關(guān)C# PPT SmartArt圖形內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C# 多進(jìn)程打開PPT的示例教程
- C# 實(shí)現(xiàn)PPT 每一頁轉(zhuǎn)成圖片過程解析
- C#將PPT文件轉(zhuǎn)換成PDF文件
- C#如何添加PPT背景
- C# 實(shí)現(xiàn)對(duì)PPT文檔加密、解密及重置密碼的操作方法
- C#提取PPT文本和圖片的實(shí)現(xiàn)方法
- C# 使用Free Spire.Presentation 實(shí)現(xiàn)對(duì)PPT插入、編輯、刪除表格
- 在C#里面給PPT文檔添加注釋的實(shí)現(xiàn)代碼
- C#向PPT文檔插入圖片以及導(dǎo)出圖片的實(shí)例
- C#實(shí)現(xiàn)將PPT轉(zhuǎn)換成HTML的方法
- word ppt excel文檔轉(zhuǎn)換成pdf的C#實(shí)現(xiàn)代碼
- C#/VB.NET 自定義PPT動(dòng)畫路徑的步驟
相關(guān)文章
C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable
這篇文章介紹了C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
解析如何正確使用SqlConnection的實(shí)現(xiàn)方法
本篇文章對(duì)如何正確使用SqlConnection的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C# 創(chuàng)建Excel氣泡圖的實(shí)例代碼
這篇文章主要介紹了C# 創(chuàng)建Excel氣泡圖的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
使用 BenchmarkDotNet 對(duì) C# 代碼進(jìn)行基準(zhǔn)測(cè)試
這篇文章主要介紹了使用 BenchmarkDotNet 對(duì) C# 代碼進(jìn)行基準(zhǔn)測(cè)試,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03

