在文档中添加水印可以有效地声明和保护文档,是保护重要文件的方法之一。PPT文档中也可以设置水印,包括文本水印和图片水印。本文将介绍如何通过spire给PPT添加水印。介绍for.net。下载安装免费Spire后。演示文稿。NET,添加引用dll文件,参考以下步骤完成水印添加。
1。添加文本水印
步骤1:初始化演示类实例并加载文档
演示PPT = new Presentation(); ppt。LoadFromFile(@ & # 34;c:usersadministratordesktopsample . pptx & # 34;,文件格式。pptx 2010);步骤2:初始化一个字体类实例,实例化字体格式
Font stringFont =新字体(& # 34;Arial & # 34, 90); Size size = TextRenderer。measure text(& # 34;内部信息& # 34;,string font);第三步:绘制一个形状并指定大小、填充颜色、边框颜色和旋转角度
RectangleF rect = new RectangleF((PPT。SlideSize.Size.Width - size。宽度)/ 2,(ppt。SlideSize.Size.Height - size。身高)/ 2,大小。宽度,大小。身高); IAutoShape shape = ppt。slides[0]. shapes . append shape(Spire。presentation . shape type . rectangle,rect); 形状。Fill.FillType = FillFormatType。无; 形状。shape style . line Color . Color = Color。白色; 形状。旋转=-45°;第四步:将形状属性设置为保护属性
形状。locking . selection protection = true; 形状。Line.FillType = FillFormatType。无;第五步:设置文本大小和颜色
形状。TextFrame . Text = & # 34内部信息& # 34;; TextRange textRange = shape。TextFrame . TextRange textRange。Fill.FillType = Spire。presentation . drawing . fillformattype . solid; textRange。Fill.SolidColor.Color = Color。FromArgb(120,彩色。灰色); textRange。FontHeight = 45第六步:保存文档
ppt。SaveToFile(& # 34;TextWatermark.pptx & # 34,文件格式。pptx 2010);完成以上代码步骤后,调试运行项目程序生成文件(可在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:UsersAdministratorDesktopsample.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); } }
使用系统;
使用系统。文本;
使用Spire。演示文稿;
使用系统。绘图;
使用Spire。演示文稿。绘图;
使用系统。Windows . Forms
namespace insert watermark _ PPT
{
class program
{
static void main(string[]args)
{
//初始化一个存在。
ppt。LoadFromFile(@ & # 34;c:usersadministratordesktopsample . pptx & # 34;,文件格式。pptx 2010);
//初始化一个字体实例,实例化字体格式
Font string Font = new Font(& # 34;Arial & # 34, 90);
Size size = TextRenderer。measure text(& # 34;内部信息& # 34;,string font);
//绘制一个形状并指定大小、填充颜色、边框颜色和旋转
rectangle f rect = new rectangle f((PPT . slidesize . width-size . width)/2,(PPT . slidesize . size . height-)
IAutoShape Shape Shape = PPT。slides[0]. shapes . append shape(Spire。presentation . shape type . rectangle,rect);
形状。Fill.FillType = FillFormatType。无;
形状。shape style . line Color . Color = Color。白色;
形状。旋转=-45°;
//将shape属性设置为保护属性
shape。锁定中。selectionprotection = true
形状。Line.FillType = FillFormatType。无;
//设置文本大小和颜色
形状。TextFrame . Text = & # 34内部信息& # 34;;
TextRange textRange = shape。TextFrame . TextRange
textRange。Fill.FillType = Spire。presentation . drawing . fillformattype . solid;
textRange。Fill.SolidColor.Color = Color。FromArgb(150,彩色。浅蓝色);
textRange。FontHeight = 90
//保存文档
ppt。SaveToFile(& # 34;TextWatermark.pptx & # 34,文件格式。pptx 2010);
}
}
2。添加图片水印
步骤1:初始化一个演示类实例并加载文档
演示PPT = new Presentation(); ppt。LoadFromFile(@ & # 34;c:usersadministratordesktopsample . pptx & # 34;,文件格式。pptx 2010);第二步:设置第一张幻灯片的背景图片类型和样式
ppt。slides[0]. slide background . type = Spire。presentation . drawing . background type . custom; ppt。slides[0]. slide background . fill . fill type = FillFormatType。图片; ppt。slides[0]. slide background . fill . picture fill . fill type = PictureFillType。拉伸;第三步:加载图片,为第一张幻灯片设置水印
Image img = Image.FromFile(@"C:UsersAdministratorDesktopimages1.jpg"); IImageData image = ppt.Images.Append(img); ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
步骤四:保存文档图像img =图像。from file(@ & # 34;c:usersadministratordesktopimages 1 . jpg & # 34;); IImageData image = ppt。images . Append(img); ppt。slides[0]. slide background . fill . picture fill . picture . embed image = image;第四步:保存文档
ppt.SaveToFile("ImageWatermark1.pptx", FileFormat.Pptx2010);
ppt。SaveToFile(& # 34;image watermark 1 . pptx & # 34;,文件格式。pptx 2010);
所有代码:
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:UsersAdministratorDesktopsample.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:UsersAdministratorDesktopimages1.jpg"); IImageData image = ppt.Images.Append(img); ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image; //保存文档 ppt.SaveToFile("ImageWatermark1.pptx", FileFormat.Pptx2010); } } }
使用系统;
使用系统。绘图;
使用Spire。演示文稿;
使用Spire。演示文稿。绘图;
namespace image watermark _ PPT
{
class program
{
static void main(string[]args)
{
//初始化一个存在。
ppt。LoadFromFile(@ & # 34;c:usersadministratordesktopsample . pptx & # 34;,文件格式。pptx 2010);
//设置第一张幻灯片的背景图片类型和样式
PPT.slides [0]。幻灯片background . type = spire . presentation . drawing . background type . custom;
ppt。slides[0]. slide background . fill . fill type = FillFormatType。图片;
ppt。slides[0]. slide background . fill . picture fill . fill type = PictureFillType。拉伸;
//加载图片并设置第一张幻灯片的水印效果
Imageimg = image . from file(@ & # 34;c:usersadministratordesktopimages 1 . jpg & # 34;);
IImageData image = ppt。images . Append(img);
ppt。slides[0]. slide background . fill . picture fill . picture . embed image = image;
//保存文档
ppt。SaveToFile(& # 34;image watermark 1 . pptx & # 34;,文件格式。pptx 2010);
}
}
}
以上是给PPT添加水印的代码操作。希望这个方法能有所帮助。感谢您的阅读!