C#将图片或文字等插入PPT文档中

本文详细介绍使用Spire.Presentation库在C#中创建、编辑PPT文档的方法,包括创建文档、插入文字与图片、样式调整等核心操作,提供实用代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bibabu135766

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值