C# 处理PPT水印(一)——添加水印效果(文字水印、图片水印)
对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一。在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水印(一)——添加水印效果(文字水印、图片水印)的更多相关文章
- ASP.NET(C#)图片加文字、图片水印,神啊,看看吧
ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...
- 开发笔记:PDF生成文字和图片水印
背景 团队手里在做的一个项目,其中一个小功能是用户需要上传PDF文件到文件服务器上,都是一些合同或者技术评估文档,鉴于知识版权和防伪的目的,需要在上传的PDF文件打上水印, 这时候我们需要提供能力给客 ...
- PHP 文字,图片水印,缩略图,裁切成小图(大小变小)
文字水印基本思路:1.用getimagesize()获取图片的信息(as:大小,属性等):2.根据图片信息用imagecreatefromjpeg ()/imagecreatefromgif/imag ...
- PHP加水印代码 支持文字和图片水印
PHP加图片水印.文字水印类代码,PHP加水印类,支持文字图片水印的透明度设置.水印图片背景透明.自己写的一个类,因为自己开发的一套CMS中要用到,网上的总感觉用着不顺手,希望大家也喜欢这个类,后附有 ...
- C# 给图片添加透明的文字、图片水印
#region 添加水印 /// <summary> /// 添加文字水印 /// </summary> /// <param name="image" ...
- vue中添加文字或图片水印
首先引用warterMark.js,内容如下 'use strict' var watermark = (className,str,type) => { let dom = document. ...
- CSS3动画实现高亮光弧效果,文字和图片(一闪而过)
前言 好久没有写博客啦,高亮文字和图片一闪而过的特效,用CSS3来写 先看文字吧, 就上代码了 .shadow { /* 背景颜色线性渐变 */ /* 老式写法 */ /* linear为线性渐变,也 ...
- Android给图片加文字和图片水印
我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行. /** * 图片工具类 * @author 水寒 * 欢迎访问水寒的个人博客:ht ...
- php文字、图片水印功能函数封装
一直在做有关php文字图片上传方面的工作,所以把此功能函数整理了一次,现在分享给大家. <?php class image { var $g_img; var $g_w; var $g_h; v ...
- IOS 绘制基本图形(画文字、图片水印)
- (void)drawRect:(CGRect)rect { // Drawing code // [self test]; // 1.加载图片到内存中 UIImage *image = [UIIm ...
随机推荐
- Ubuntu上安装使用WeChat、TIM
WeChat可以直接到软件商店安装,不过是网页版...(其实个人感觉还行,就是什么都不能设置就挺蛋疼的,字体大小.背景什么的) 以下是网上找到的教程,在此总结一下: 下载地址:https://gith ...
- gc笔记2
空间分配担保:在发生MinorGC之前,虚拟机会检查老年代最大连续可用是否大于新生代所有对象的空间,如果这个条件成立,则minorgc时安全的
- [IOT] 自制蓝牙工牌办公室定位系统 (一)—— 阿里物联网平台概览及打通端到云(硬核·干货)
目录:老少皆宜.超长干货文警告 1.快速入门创建产品 -- 小白,打包带走去吹牛 2.代码分析 -- 老炮,快速了解能用上 2.1 从start.sh分析开发环境如何自动构建 2.2 从sample. ...
- Spring AOP实现 Bean字段合法性校验
使用SpringAop 验证方法参数是否合法 先定义两个注解类ValidateGroup 和 ValidateFiled ValidateGroup .java package com.zf.an ...
- Python 爬虫入门(二)——爬取妹子图
Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...
- 漫画:什么是HTTPS?
什么是HTTP协议? HTTP协议全称Hyper Text Transfer Protocol,翻译过来就是超文本传输协议,位于TCP/IP四层模型当中的应用层. HTTP协议通过请求/响应的方式,在 ...
- [Swift]LeetCode77. 组合 | Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- mysql与PHP建立连接实现增删查改
mysql与PHP连接的查询写法: <?php //1.建立与数据库的连接 //类似于宽字符集问题,mysqli是额外的扩展 //需要找到配置文件去开启扩展 //如果需要在调用函数 之前忽略错误 ...
- 微信小程序中样式问题
1.去除button按钮的默认样式 这是button按钮自带的默认样式 button { position:relative; display:block; margin-left:auto; mar ...
- 了解django部署(Django + Uwsgi + Nginx)
首先了解下基本概念: 1 WSGI WSGI:全称是Web Server Gateway Interface,是python应用程序或者框架和web服务器之间的一种接口,被广泛接受.WSGI不是服务器 ...