对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一。在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. (BUG记录)使用迭代器安全的删除处于循环下集合中的元素

    今日在写一个功能时,需要从MQ拿取数据集合调用对端系统进行批量处理,为了幂等支持,在循环内部如果不满足调用条件就直接从集合中移除. 以上是一个典型的循环集合内删除的场景任务,工作一年第一次遇到这个场景 ...

  2. 用简单的代码让一组静态图片变成gif动画

    比如这组图片:     变成这样的gif动画:   是不是很神奇....   先看html .样式.很简单,一个div,然后引入图片.   <!DOCTYPE html> <html ...

  3. Idea集成maven插件

    学习目标 1.正确在idea上安装maven 2.安装后使用的基本操作 3.回顾安装步骤 安装过程 设置安装后自动下载功能 maven一键构建概念 我们的项目,往往都要经历编译. 测试. 运行. 打包 ...

  4. Golang Go Go Go part3:数据类型及操作

    五.Go 基本类型 1.基本类型种类 布尔值: bool 长度 1字节 取值范围 true, false注意事项:不可用数字代表 true 或 false 整型: int/uint 根据运行平台可能为 ...

  5. Mesos源码分析(15): Test Executor的运行

    Test Executor的代码在src/examples/test_executor.cpp中   int main(int argc, char** argv) {   TestExecutor ...

  6. Mesos源码分析(2): Mesos Master的启动之一

    Mesos Master的启动参数如下: /usr/sbin/mesos-master --zk=zk://127.0.0.1:2181/mesos --port=5050 --log_dir=/va ...

  7. C++ : cin.get()函数和cin函数的使用

    笔者由于自己忘记了cin函数怎么用,所以这里趁自己复习C++的空子正好可以做做记录. 1.cin>>          用法1:最基本,也是最常用的用法,输入一个数字: #include ...

  8. [Swift]LeetCode463. 岛屿的周长 | Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  9. 【Impala篇】---Hue从初始到安装应用

    一.前述 Cloudera公司推出,提供对HDFS.Hbase数据的高性能.低延迟的交互式SQL查询功能.基于Hive使用内存计算,兼顾数据仓库.具有实时.批处理.多并发等优点 是CDH平台首选的PB ...

  10. Python实现 Typora数学公式 转 有道云笔记Markdown数学公式

    话不多说上代码,可以按照自己的需求把匿名函数改成普通函数,改不来的可以加我微信我帮你改. 块状数学公式转换 import re test_str = r''' $D={\{\vec{x_1},\vec ...