C# 如何添加水印到PPT

简介: 对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一。在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:\Users\Administrator\Desktop\sample.pptx", FileFormat.Pptx2010);

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

Font stringFont = new Font("Arial", 90);
Size size = TextRenderer.MeasureText("内部资料", stringFont);

步骤三:绘制一个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(120, Color.Gray);
textRange.FontHeight = 45;

步骤六:保存文档

ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010);

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

 

全部代码:

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

namespace InsertWatermark_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化一个Presentation类实例并加载文档
            Presentation ppt = new Presentation();
            ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx", FileFormat.Pptx2010);

            //初始化一个Font类字体实例并实例化字体格式
            Font stringFont = new Font("Arial", 90);
            Size size = TextRenderer.MeasureText("内部资料", stringFont);

            //绘制一个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(150, Color.LightBlue);
            textRange.FontHeight = 90;

            //保存文档
            ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010);
        }
    }
View full Code

 

2.添加图片水印

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

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx", FileFormat.Pptx2010);

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

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:\Users\Administrator\Desktop\images\1.jpg");
IImageData image = ppt.Images.Append(img);
ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

步骤四:保存文档

 ppt.SaveToFile("ImageWatermark1.pptx", FileFormat.Pptx2010);

全部代码:

using System;
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Drawing;

namespace ImageWatermark_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化一个Presentation类实例并加载文档
            Presentation ppt = new Presentation();
            ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx", FileFormat.Pptx2010);

            //为第一张幻灯片设置背景图片类型和样式
            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:\Users\Administrator\Desktop\images\1.jpg");
            IImageData image = ppt.Images.Append(img);
            ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

            //保存文档
            ppt.SaveToFile("ImageWatermark1.pptx", FileFormat.Pptx2010);
        }
    }
}
View full Code

 

 

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

目录
相关文章
|
4月前
|
PHP 数据安全/隐私保护
ueditor上传图片添加水印
博客在上传图片的时候,我希望能打上我博客链接的水印,掘金,csdn都是这么干的,这事我得学习。 平时的图片上传还好说,在文章编辑的时候,使用ueditor上传图片加水印需要修改ueditor部分PHP的源码,我这里大概记录一下。 首先打开php文件夹下的Uploader.class.php
26 0
|
5月前
|
存储 编解码 Cloud Native
音视频添 加水印
音视频添 加水印
|
8月前
|
数据安全/隐私保护
五、用PhotoShop去图片的水印 | 微课系列教程
图片,是我们PPT、微课必不可少的素材。在之前的课程中,给大家讲过如何找大图、高清图等,但从网站上找到的一些图片,总是多多少少有一些水印之类的杂物,严重影响我们的使用,今天这一课,就跟着我来一起用Ps去掉图片中你不想要的部分吧!
83 0
|
11月前
|
机器学习/深度学习 算法 数据安全/隐私保护
图片/视频去水印代码(毕业设计)
图片/视频去水印代码(毕业设计)
453 0
图片/视频去水印代码(毕业设计)
|
数据安全/隐私保护
图片一键添加水印工具V1.0-免费版
该图片一键添加水印工具V1.0可以批量把常见图片格式('.bmp', '.png', '.jpg', '.jpeg', '.dib', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')一键添加水印。默认在图片右下角添加水印。
265 0
图片一键添加水印工具V1.0-免费版
|
编解码 数据安全/隐私保护 开发者
为视频添加水印 | 学习笔记
快速学习为视频添加水印
484 0
|
数据采集 JavaScript 开发者
批量下载一些图片
学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:
176 0
|
数据安全/隐私保护 计算机视觉
使用opencv给视频添加水印
使用opencv给视频添加水印
269 0
使用opencv给视频添加水印
|
前端开发 数据安全/隐私保护 Android开发
autojs图片加水印
牙叔教程 简单易懂
189 0
|
程序员 数据安全/隐私保护 iOS开发
一日一技:如何批量给PDF添加水印?
一日一技:如何批量给PDF添加水印?
401 0
一日一技:如何批量给PDF添加水印?