加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 处理PPT水印(一)——添加水印效果(文字水印、图片水印)

发布时间:2020-12-16 01:07:34 所属栏目:百科 来源:网络整理
导读:对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一。在PPT文档中同样也可以设置水印,包括文本水印和图片水印,本文将讲述如何通过Spire.Presentation for .NET来对PPT添加水印,下载安装Free Spire.Presentationfor .NET后,添加引用dll文件

对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一。在PPT文档中同样也可以设置水印,包括文本水印和图片水印,本文将讲述如何通过Spire.Presentation for .NET来对PPT添加水印,下载安装Free Spire.Presentationfor .NET后,添加引用dll文件,参考下面的操作步骤,完成水印添加。

1.添加文本水印

步骤一:初始化Presentation类实例,并加载文档

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"C:UsersAdministratorDesktopsample.pptx",FileFormat.Pptx2010);

步骤二:初始化一个Font类实例,并实例化字体格式

Font stringFont = new Font("Arial90);
Size size = TextRenderer.MeasureText(内部资料步骤三:绘制一个shape并指定大小、填充颜色、边框颜色和旋转角度

RectangleF rect = new RectangleF((ppt.SlideSize.Size.Width - size.Width) / 2,(ppt.SlideSize.Size.Height - size.Height) / 2,size.Width,size.Height);
IAutoShape shape = ppt.Slides[0].Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle,rect);
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.Rotation = -45;

步骤四:设定形状属性为保护属性

shape.Locking.SelectionProtection = true;
shape.Line.FillType = FillFormatType.None;

步骤五:设置文本大小、颜色

shape.TextFrame.Text = ";
TextRange textRange = shape.TextFrame.TextRange;
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.FromArgb(12045;

步骤六:保存文档

ppt.SaveToFile(TextWatermark.pptx完成以上代码步骤后,调试运行项目程序,生成文件(可在该项目文件中bin>Debug中查看),如下图所示:

?

?

全部代码:

using System;
 System.Text;
 Spire.Presentation;
 System.Drawing;
 Spire.Presentation.Drawing;
 System.Windows.Forms;

namespace InsertWatermark_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化一个Presentation类实例并加载文档
            Presentation ppt =  Presentation();
            ppt.LoadFromFile(初始化一个Font类字体实例并实例化字体格式
            Font stringFont = );
            Size size = TextRenderer.MeasureText(绘制一个Shape并指定大小、填充颜色、边框颜色和旋转度
            RectangleF rect = = FillFormatType.None;
            shape.ShapeStyle.LineColor.Color = Color.White;
            shape.Rotation = -45;

            设定形状属性为保护属性
            shape.Locking.SelectionProtection = ;
            shape.Line.FillType = FillFormatType.None;

            设置文本大小、颜色
            shape.TextFrame.Text = ;
            TextRange textRange = shape.TextFrame.TextRange;
            textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
            textRange.Fill.SolidColor.Color = Color.FromArgb(150保存文档
            ppt.SaveToFile(View full Code

?

2.添加图片水印

步骤一:初始化一个Presentation类实例并加载文档

Presentation ppt = 步骤二: 为第一张幻灯片设置背景图片类型和样式

ppt.Slides[0].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[0].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;

步骤三:加载图片并为第一张幻灯片设置水印

Image img = Image.FromFile(C:UsersAdministratorDesktopimages1.jpg);
IImageData image = ppt.Images.Append(img);
ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

步骤四:保存文档

 ppt.SaveToFile(ImageWatermark1.pptx

?

全部代码:

 Spire.Presentation.Drawing;

 ImageWatermark_PPT
{
    为第一张幻灯片设置背景图片类型和样式
            ppt.Slides[ Spire.Presentation.Drawing.BackgroundType.Custom;
            ppt.Slides[ FillFormatType.Picture;
            ppt.Slides[0].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;

            加载图片并为第一张幻灯片设置水印效果
            Image img = Image.FromFile();
            IImageData image = ppt.Images.Append(img);
            ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

            View full Code

?

?

以上是对PPT添加水印的代码操作,希望该方法能提供帮助,感谢阅读!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读