C# 操作PPT图形
1、下载并安装Spire.Presentation for .NET, 并将Spire.Presentation.dll文件引用到项目中。
2、 将代码放入Visual Studio中:
旋转图形
【C#】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Presentation;
namespace Rotate_Shapes
{
class Program
{
static void Main(string[] args)
{
//创建一个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);
}
}
}
3、前后效果对不图如下:

1、重置图形的大小
【C#】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Presentation;
using System.Drawing;
namespace Reset_Shape_Size
{
class Program
{
static void Main(string[] args)
{
//创建一个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);
}
}
}
}
2、前后效果对比图如下:

3、重新排列重叠图形的顺序
【C#】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Presentation;
namespace Reorder_Shapes
{
class Program
{
static void Main(string[] args)
{
//创建一个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); }
}
}
4、前后效果对比图如下:
