对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一。在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", );
Size size = TextRenderer.MeasureText("内部资料", stringFont);

步骤三:绘制一个shape并指定大小、填充颜色、边框颜色和旋转角度

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

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

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(, Color.Gray);
textRange.FontHeight = ;

步骤六:保存文档

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", );
Size size = TextRenderer.MeasureText("内部资料", stringFont); //绘制一个Shape并指定大小、填充颜色、边框颜色和旋转度
RectangleF rect = new RectangleF((ppt.SlideSize.Size.Width - size.Width) / , (ppt.SlideSize.Size.Height - size.Height) / , size.Width, size.Height);
IAutoShape shape = ppt.Slides[].Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect);
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.Rotation = -; //设定形状属性为保护属性
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(, Color.LightBlue);
textRange.FontHeight = ; //保存文档
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[].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[].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[].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[].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[].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[].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image; //保存文档
ppt.SaveToFile("ImageWatermark1.pptx", FileFormat.Pptx2010);
}
}
}

View full Code

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

C# 处理PPT水印(一)——添加水印效果(文字水印、图片水印)的更多相关文章

  1. ASP.NET(C#)图片加文字、图片水印,神啊,看看吧

    ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...

  2. 开发笔记:PDF生成文字和图片水印

    背景 团队手里在做的一个项目,其中一个小功能是用户需要上传PDF文件到文件服务器上,都是一些合同或者技术评估文档,鉴于知识版权和防伪的目的,需要在上传的PDF文件打上水印, 这时候我们需要提供能力给客 ...

  3. PHP 文字,图片水印,缩略图,裁切成小图(大小变小)

    文字水印基本思路:1.用getimagesize()获取图片的信息(as:大小,属性等):2.根据图片信息用imagecreatefromjpeg ()/imagecreatefromgif/imag ...

  4. PHP加水印代码 支持文字和图片水印

    PHP加图片水印.文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置.水印图片背景透明.自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有 ...

  5. C# 给图片添加透明的文字、图片水印

    #region 添加水印 /// <summary> /// 添加文字水印 /// </summary> /// <param name="image" ...

  6. vue中添加文字或图片水印

    首先引用warterMark.js,内容如下 'use strict' var watermark = (className,str,type) => { let dom = document. ...

  7. CSS3动画实现高亮光弧效果,文字和图片(一闪而过)

    前言 好久没有写博客啦,高亮文字和图片一闪而过的特效,用CSS3来写 先看文字吧, 就上代码了 .shadow { /* 背景颜色线性渐变 */ /* 老式写法 */ /* linear为线性渐变,也 ...

  8. Android给图片加文字和图片水印

    我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行. /** * 图片工具类 * @author 水寒 * 欢迎访问水寒的个人博客:ht ...

  9. php文字、图片水印功能函数封装

    一直在做有关php文字图片上传方面的工作,所以把此功能函数整理了一次,现在分享给大家. <?php class image { var $g_img; var $g_w; var $g_h; v ...

  10. IOS 绘制基本图形(画文字、图片水印)

    - (void)drawRect:(CGRect)rect { // Drawing code // [self test]; // 1.加载图片到内存中 UIImage *image = [UIIm ...

随机推荐

  1. ES6-个人学习大纲

    1,let   const学习补充 1.1,let的知识点: 01-作用域只限制在当前代码块内,代码块形式如下: { var str = '张三'; console.log(str); let str ...

  2. Linux了解知识点

    Linux知识点   1.linux系统内核最早由芬兰大学生linus Torvalds开发. 2.Linux主要用于服务器端和嵌入式两个领域. 3.Linux的特点:开放性.多用户.多任务.良好的用 ...

  3. linux 重新生成网卡配置文件

    nmcli connection add con-name home type ethernet ifname eth1 autoconnect yes ip4 10.1.252.60/24 gw4 ...

  4. C语言常用的编程规范

    1排版 1-1相对独立的程序块之间.变量说明之后必须加空行. 示例:如下例子不符合规范. if (!valid_ni(ni)) { ... // program code } repssn_ind = ...

  5. ubuntu tensorflow install(Ubuntu16.04+CUDA9.0+cuDNN7.5+Python3.6+TensorFlow1.5)

    在网上找了很多案例,踩了许多坑,感觉比较全面的是下面介绍的 http://www.cnblogs.com/xuliangxing/p/7575586.html 先说说我的步骤: 首先安装了Anacod ...

  6. 实战深度学习(下)OpenCV库

    在上一节中,我们讲到了OpenCV库的安装,现在我们来进行实战,看如何利用Python来调用OpenCV库. 一: 如果您的电脑是win10的系统,那么请您按下win键,再按下空格键,输入Python ...

  7. Java线程和进程相关面试题与答案总结

    有几天没有写一写博客了,今天就带给大家一些面试题和参考答案吧! 这些都是上海尚学堂Java培训的学员去面试时遇到的问题,今天总结出来的是Java线程相关类的面试题.把参考答案和解析也发布出来,供大家学 ...

  8. [Swift]LeetCode306. 累加数 | Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

  9. [Swift]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

  10. 【Spark篇】---SparkSQL中自定义UDF和UDAF,开窗函数的应用

    一.前述 SparkSQL中的UDF相当于是1进1出,UDAF相当于是多进一出,类似于聚合函数. 开窗函数一般分组取topn时常用. 二.UDF和UDAF函数 1.UDF函数 java代码: Spar ...