前面我们介绍了Spire.Presentation 添加和删除常规图形 , 下文将详细介绍如何使用Spire.Presentation 操作幻灯片中的图形,包括旋转图形,重置图形的大小和位置,以及重新排列重叠图形的顺序。
旋转图形
C#
//创建一个PowerPoint文档并加载示例文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");
//获取第一个幻灯片里的第一个图形
IShape shape = ppt.Slides[0].Shapes[0];
//旋转180度
shape.Rotation = 180;
//保存文档
ppt.SaveToFile("Rotate.pptx", FileFormat.Pptx2010);
VB.NET
'创建一个PowerPoint文档并加载示例文档
Dim ppt As New Presentation()
ppt.LoadFromFile("Sample.pptx")
'获取第一个幻灯片里的第一个图形
Dim shape As IShape = ppt.Slides(0).Shapes(0)
'旋转180度
shape.Rotation = 180
'保存文档
ppt.SaveToFile("Rotate.pptx", FileFormat.Pptx2010)
重置图形的大小和位置
C#
//创建一个PowerPoint文档并加载示例文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");
//获取原始幻灯片高度和宽度
float currentHeight = ppt.SlideSize.Size.Height;
float currentWidth = ppt.SlideSize.Size.Width;
//将幻灯片大小设置为A3.
ppt.SlideSize.Type = SlideSizeType.A3;
//获取新幻灯片的高度和宽度
float newHeight = ppt.SlideSize.Size.Height;
float newWidth = ppt.SlideSize.Size.Width;
//指定高度和宽度的比例
float ratioHeight = newHeight / currentHeight;
float ratioWidth = newWidth / currentWidth;
//重新设置图形大小和位置
foreach (ISlide slide in ppt.Slides)
{
foreach (IShape shape in slide.Shapes)
{
//重置图形大小
shape.Height = shape.Height * ratioHeight;
shape.Width = shape.Width * ratioWidth;
//重置图形位置
shape.Left = shape.Left * ratioHeight;
shape.Top = shape.Top * ratioWidth;
}
//保存文档
ppt.SaveToFile("ResetSizePosition.pptx", FileFormat.Pptx2010);
}
VB.NET
'创建一个PowerPoint文档并加载示例文档
Dim ppt As New Presentation()
ppt.LoadFromFile("Sample.pptx")
'获取原始幻灯片高度和宽度
Dim currentHeight As Single = ppt.SlideSize.Size.Height
Dim currentWidth As Single = ppt.SlideSize.Size.Width
'将幻灯片大小设置为A3.
ppt.SlideSize.Type = SlideSizeType.A3
'获取新幻灯片的高度和宽度
Dim newHeight As Single = ppt.SlideSize.Size.Height
Dim newWidth As Single = ppt.SlideSize.Size.Width
'指定高度和宽度的比例
Dim ratioHeight As Single = newHeight / currentHeight
Dim ratioWidth As Single = newWidth / currentWidth
'重新设置图形大小和位置
For Each slide As ISlide In ppt.Slides
For Each shape As IShape In slide.Shapes
'重置图形大小
shape.Height = shape.Height * ratioHeight
shape.Width = shape.Width * ratioWidth
'重置图形位置
shape.Left = shape.Left * ratioHeight
shape.Top = shape.Top * ratioWidth
Next
'保存文档
ppt.SaveToFile("ResetSizePosition.pptx", FileFormat.Pptx2010)
Next
重新排列重叠图形的顺序
C#
//创建一个PowerPoint文档并加载示例文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");
//获取第二个幻灯片里的第一个图形
IShape shape = ppt.Slides[1].Shapes[0];
//重新设置图形的顺序.
ppt.Slides[1].Shapes.ZOrder(1, shape);
//保存文档
ppt.SaveToFile("Reorder.pptx", FileFormat.Pptx2010);
VB.NET
'创建一个PowerPoint文档并加载示例文档
Dim ppt As New Presentation()
ppt.LoadFromFile("Sample.pptx")
'获取第二个幻灯片里的第一个图形
Dim shape As IShape = ppt.Slides(1).Shapes(0)
'重新设置图形的顺序.
ppt.Slides(1).Shapes.ZOrder(1, shape)
'保存文档
ppt.SaveToFile("Reorder.pptx", FileFormat.Pptx2010)