C# / VB.NET 在PPT中创建、编辑PPT SmartArt图形的方法详解
什么是Office SmartArt?
Office SmartArt 是微软Office套件中的一种图形类型,它可以帮助用户在较短的时间内创建具有高质量的信息图形。它的能力不仅限于流程图和组织结构图,还包括算法图、漏斗图、阶段图、矩阵图、金字塔图等不同种类的图形。
如何在C# / VB.NET中创建和编辑SmartArt图形?
C# / VB.NET 实际上可以通过Office Interop来实现操作PowerPoint对象模型的目的。下面是一个简单的示例来介绍如何在PPT中创建并编辑SmartArt 图形的步骤:
- 导入PowerPoint和Office Interop库
C#代码:
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
VB.NET代码:
Imports Microsoft.Office.Interop.PowerPoint
Imports Microsoft.Office.Core
- 创建一个新的PowerPoint文档和幻灯片
C#代码:
Application pptApplication = new Application();
pptApplication.Visible = MsoTriState.msoTrue;
Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue);
pptPresentation.Slides.Add(1, PpSlideLayout.ppLayoutTitleOnly);
VB.NET代码:
Dim pptApplication As New Application()
pptApplication.Visible = MsoTriState.msoTrue
Dim pptPresentation As Presentation = pptApplication.Presentations.Add(MsoTriState.msoTrue)
pptPresentation.Slides.Add(1, PpSlideLayout.ppLayoutTitleOnly)
- 在幻灯片中添加SmartArt图形
C#代码:
Slide slide = pptPresentation.Slides[1];
Shapes shapes = slide.Shapes;
Shape shape = shapes.AddSmartArt(SmartArtLayoutType.smartArtBasicBlockList);
SmartArt smartArt = shape.SmartArt;
smartArt.Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1";
smartArt.Nodes.Item(1).Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1.1";
smartArt.Nodes.Add(2, SmartArtNodePosition.smartArtNodeBelow, "Node 2");
VB.NET代码:
Dim slide As Slide = pptPresentation.Slides(1)
Dim shapes As Shapes = slide.Shapes
Dim shape As Shape = shapes.AddSmartArt(SmartArtLayoutType.smartArtBasicBlockList)
Dim smartArt As SmartArt = shape.SmartArt
smartArt.Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1"
smartArt.Nodes.Item(1).Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1.1"
smartArt.Nodes.Add(2, SmartArtNodePosition.smartArtNodeBelow, "Node 2")
在上述代码中,我们使用 Shapes
对象和 AddSmartArt
方法,在幻灯片中创建了 SmartArt 图形。我们使用 Nodes
属性获取图形中的节点,然后使用 TextFrame2
对象设置节点中的文本。
- 编辑SmartArt图形
C#代码:
smartArt.Nodes.Item(2).TextFrame2.TextRange.Text = "Node 2 - Updated";
smartArt.Nodes.Add(3, SmartArtNodePosition.smartArtNodeBelow, "Node 3");
smartArt.Nodes.Item(3).Nodes.Add(1, SmartArtNodePosition.smartArtNodeRight, "Node 3.1");
VB.NET代码:
smartArt.Nodes.Item(2).TextFrame2.TextRange.Text = "Node 2 - Updated"
smartArt.Nodes.Add(3, SmartArtNodePosition.smartArtNodeBelow, "Node 3")
smartArt.Nodes.Item(3).Nodes.Add(1, SmartArtNodePosition.smartArtNodeRight, "Node 3.1")
在上述代码中,我们使用返回的 SmartArt 对象,获取指定节点的 Nodes
和 TextFrame2
属性,并且对其进行更新。
示例说明
这是一个C#控制台程序来演示如何创建和编辑SmartArt图形:
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System;
namespace PowerpointSmartArtDemo
{
class Program
{
static void Main(string[] args)
{
Application pptApplication = new Application();
pptApplication.Visible = MsoTriState.msoTrue;
Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue);
pptPresentation.Slides.Add(1, PpSlideLayout.ppLayoutTitleOnly);
Slide slide = pptPresentation.Slides[1];
Shapes shapes = slide.Shapes;
Shape shape = shapes.AddSmartArt(SmartArtLayoutType.smartArtBasicBlockList);
SmartArt smartArt = shape.SmartArt;
smartArt.Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1";
smartArt.Nodes.Item(1).Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1.1";
smartArt.Nodes.Add(2, SmartArtNodePosition.smartArtNodeBelow, "Node 2");
Console.WriteLine("SmartArt created successfully");
Console.WriteLine("Press any key to update the SmartArt");
Console.ReadKey();
smartArt.Nodes.Item(2).TextFrame2.TextRange.Text = "Node 2 - Updated";
smartArt.Nodes.Add(3, SmartArtNodePosition.smartArtNodeBelow, "Node 3");
smartArt.Nodes.Item(3).Nodes.Add(1, SmartArtNodePosition.smartArtNodeRight, "Node 3.1");
Console.WriteLine("SmartArt updated successfully");
Console.ReadKey();
//关闭 PowerPoint 并释放对象
foreach (Presentation ppt in pptApplication.Presentations)
{
ppt.Close();
}
pptApplication.Quit();
}
}
}
这是一个VB.NET控制台程序来演示如何创建和编辑SmartArt图形:
Imports Microsoft.Office.Interop.PowerPoint
Imports Microsoft.Office.Core
Imports System
Namespace PowerpointSmartArtDemo
Class Program
Shared Sub Main(ByVal args As String())
Dim pptApplication As New Application()
pptApplication.Visible = MsoTriState.msoTrue
Dim pptPresentation As Presentation = pptApplication.Presentations.Add(MsoTriState.msoTrue)
pptPresentation.Slides.Add(1, PpSlideLayout.ppLayoutTitleOnly)
Dim slide As Slide = pptPresentation.Slides(1)
Dim shapes As Shapes = slide.Shapes
Dim shape As Shape = shapes.AddSmartArt(SmartArtLayoutType.smartArtBasicBlockList)
Dim smartArt As SmartArt = shape.SmartArt
smartArt.Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1"
smartArt.Nodes.Item(1).Nodes.Item(1).TextFrame2.TextRange.Text = "Node 1.1"
smartArt.Nodes.Add(2, SmartArtNodePosition.smartArtNodeBelow, "Node 2")
Console.WriteLine("SmartArt created successfully")
Console.WriteLine("Press any key to update the SmartArt")
Console.ReadKey()
smartArt.Nodes.Item(2).TextFrame2.TextRange.Text = "Node 2 - Updated"
smartArt.Nodes.Add(3, SmartArtNodePosition.smartArtNodeBelow, "Node 3")
smartArt.Nodes.Item(3).Nodes.Add(1, SmartArtNodePosition.smartArtNodeRight, "Node 3.1")
Console.WriteLine("SmartArt updated successfully")
Console.ReadKey()
'关闭 PowerPoint 并释放对象
For Each ppt As Presentation In pptApplication.Presentations
ppt.Close()
Next
pptApplication.Quit()
End Sub
End Class
End Namespace
这两个程序都会在PPT中创建一个基本块列表SmartArt,并在控制台中输出成功消息,然后更新SmartArt并打印另一条成功消息。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C# / VB.NET 在PPT中创建、编辑PPT SmartArt图形的方法详解 - Python技术站