温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用C# / VB.NET 实现在PPT中创建、编辑PPT SmartArt图形

发布时间:2020-10-26 17:31:01 来源:亿速云 阅读:211 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用C# / VB.NET 实现在PPT中创建、编辑PPT SmartArt图形,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Dll文件引用

方式1:下载包。下载后,解压,打开Bin文件夹,根据自己用的.NET Framework选择相应的文件夹,如:此示例中使用的是NET4.0,即打开NET4.0文件,找到Spire.Presentation.dll文件。找到dll文件后,在vs程序中添加引用该dll。

如下引用结果:

使用C# / VB.NET 实现在PPT中创建、编辑PPT SmartArt图形

方式2:通过Nuget搜索下载导入。

注:创建SmartArt图形时,可创建80多种不同类型的图形,编辑图形是,可添加、删除节点、编辑节点内容、给节点内容设置超链接(包括链接到网页、链接到幻灯片)

示例1. 创建PPT SmartArt图形

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 = "董事会\n" + "Board of Directors";      
      node1_1.TextFrame.Text = "总经理\n" + "General Manager";
      node1_1_1.TextFrame.Text = "供应部\n" + "Supply Dept.";
      node1_1_2.TextFrame.Text = "营销部\n" + "Sales Dept.";
      node1_1_3.TextFrame.Text = "生产部\n" + "Productive Dept.";
      node1_1_4.TextFrame.Text = "财务部\n" + "Finance Dept.";
      node1_1_5.TextFrame.Text = "人力资源部\n" + "HR Dept.";
      node1_1_6.TextFrame.Text = "质检中心\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 = "生产车间\n" + "Production Dept.";
      node1_1_3_2.TextFrame.Text = "维修部\n" + "Maintenance Dept.";
      node1_1_6_1.TextFrame.Text = "生产质量管理\n" + "Production Quality Mgt.";
      node1_1_6_2.TextFrame.Text = "生产安全管理\n" + "Production Safety Mgt.";
      node1_1_6_3.TextFrame.Text = "环境管理\n" + "Environmental Mgt.";
      

      //保存文档
      ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);
      System.Diagnostics.Process.Start("result.pptx");
    }
  }
}

图形创建结果:

使用C# / VB.NET 实现在PPT中创建、编辑PPT SmartArt图形

VB.NET

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图形

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

添加超链接后,注意要在幻灯片播放下才可见超链接添加效果:

使用C# / VB.NET 实现在PPT中创建、编辑PPT SmartArt图形

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图形的节点集合
      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

看完上述内容,你们对使用C# / VB.NET 实现在PPT中创建、编辑PPT SmartArt图形有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI