C#对PPT编辑操作,目前比较方法便捷高效的就属他了:Spire.Presentation,具体使用方法,如果想查看所有实例,可直接按照官网进行下载,里面有队Vb,C#,java的语言是如何使用,如果你想直接应用DLL文件,也可以,但在这里提醒下,引用DLL文件时,需要区分目标框架是.NET Framework 那个版本以及是否为.NET Framework Client Profile,我就是在这坑死了,未注意到自己使用框架是Client Profile,不管怎么引用都是找不到,如果你不想寻找,可点击下载,下面为我自己实际如何使用的,有误还请指点!
一,创建PPT文档
//创建ppt文档
Presentation ppt = new Presentation();
ppt.SlideSize.Type = SlideSizeType.Screen16x9;//定义PPT文档大小以及方向
ppt.SlideSize.Orientation = SlideOrienation.Landscape;
IMasterSlide masterSlide = ppt.Masters[0];//获取第一张母版
string backgroundPic = "F:\\PPTFile\\background.png";
RectangleF rect = new RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height);//设置母版背景
masterSlide.SlideBackground.Fill.FillType = FillFormatType.Picture;
IEmbedImage image = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, backgroundPic, rect);
masterSlide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image as IImageData;
二,将文字插入PPT文档指定页数中
//添加首页图片至PPT以及填入实时时间数据
string ImageFile = "F:\\PPTFile\\header.png";
string PPTTime = DateTime.Now.ToString("yyyy-MM-dd");
ISlide slide = ppt.Slides[0];//获取第一张幻灯片
RectangleF rects = new RectangleF(0, 0, 980, 540);
ppt.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rects);
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(790, 350, 180, 30));
shape.Fill.FillType = FillFormatType.None;
shape.Line.Width = 0;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.TextFrame.Paragraphs[0].TextRanges.Append(new TextRange(PPTTime));
备注:
1,RectangleF(0,0,980,540),四个参数:距离幻灯片左上角X距离,距离幻灯片左上角Y距离,插入对象的Width,插入对象的Height
2,如果每次重新对PPT文件编辑,均需要新建Presentation对象
三,将图片插入指定PPT文档幻灯片位置中
RectangleF location = new RectangleF(144, 238, 680, 300);
ppt.Slides[j - 1].Shapes.AppendEmbedImage(ShapeType.Rectangle, SavePath, location);
IAutoShape TextLocation = slideindex.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(144, 40, 680, 200));
TextLocation.Fill.FillType = FillFormatType.None;
TitlePage.Line.Width = 0;
TextLocation.ShapeStyle.LineColor.Color = Color.White;
TextLocation.TextFrame.AutofitType = TextAutofitType.Normal;
TextLocation.AppendTextFrame(str);
//设置字体样式
foreach (TextParagraph para in TextLocation.TextFrame.Paragraphs)
{
foreach (TextRange range in para.TextRanges)
{
range.LatinFont = new TextFont("宋体");
range.Fill.FillType = FillFormatType.Solid;
range.Fill.SolidColor.Color = Color.Black;
}
//设置段落对齐方式、段首缩进及行距
para.Alignment = TextAlignmentType.Center;
//para.Indent = 50;
//para.LineSpacing = 150;
}
备注: 1,TextLocation.TextFrame.AutofitType = TextAutofitType.Normal →对文本自动调节作用
四,对PPT文档中文字区域修改样式
IAutoShape TitlePage = slideindex.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(20, 4, 920, 30));
TitlePage.AppendTextFrame(N[j]);
TitlePage.Line.FillType = FillFormatType.None;
TitlePage.Line.Width = 1;
TitlePage.Line.SolidFillColor.Color = Color.LightYellow;
TitlePage.TextFrame.AutofitType = TextAutofitType.Normal;
//设置shape填充颜色为渐变色
TitlePage.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient;
TitlePage.Fill.Gradient.GradientShape = Spire.Presentation.Drawing.GradientShapeType.Linear;
TitlePage.Fill.Gradient.GradientStops.Append(1f, KnownColors.SkyBlue);
TitlePage.Fill.Gradient.GradientStops.Append(0, KnownColors.LightPink);
//设置shape阴影
Spire.Presentation.Drawing.OuterShadowEffect shadow = new Spire.Presentation.Drawing.OuterShadowEffect();
shadow.BlurRadius = 10;
shadow.Direction = 20;
shadow.Distance = 4;
shadow.ColorFormat.Color = Color.LightGray;
TitlePage.EffectDag.OuterShadowEffect = shadow;