Free Spire.Presentation for .NET 3.3 (社区版)
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('test.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); System.Diagnostics.Process.Start('TextWatermark.pptx'); } }}
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('test.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('1.jpg'); IImageData image = ppt.Images.Append(img); ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image; //保存并打开文档 ppt.SaveToFile('ImageWatermark.pptx', FileFormat.Pptx2010); System.Diagnostics.Process.Start('ImageWatermark.pptx'); } }}
using Spire.Presentation; namespace DeleteTextWatermark_PPT{ class Program { static void Main(string[] args) { //实例化Presentation类,加载有水印的PowerPoint文档 Presentation ppt = new Presentation(); ppt.LoadFromFile('TextWatermark.pptx'); //遍历每一张幻灯片, 查找水印文字内容所在的形状并删除 for (int i = 0; i < ppt.Slides.Count; i++) { for (int j = 0; j < ppt.Slides[i].Shapes.Count; j++) { if (ppt.Slides[i].Shapes[j] is IAutoShape) { IAutoShape shape = ppt.Slides[i].Shapes[j] as IAutoShape; if (shape.TextFrame.Text.Contains('内部资料')) { ppt.Slides[i].Shapes.Remove(shape); } } } } //保存并打开文档 ppt.SaveToFile('RemoveTextWatermak.pptx', FileFormat.Pptx2010); System.Diagnostics.Process.Start('RemoveTextWatermak.pptx'); } }}
using Spire.Presentation;using Spire.Presentation.Drawing; namespace DeleteImageWatermark_PPT{ class Program { static void Main(string[] args) { //实例化Presentation类,加载有图片水印的PowerPoint文档 Presentation ppt = new Presentation(); ppt.LoadFromFile('ImageWatermark.pptx'); //遍历每一张幻灯片, 设置背景填充类型为None for (int i = 0; i < ppt.Slides.Count; i++) { ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.None; } //保存结果文档到本地并打开 ppt.SaveToFile('RemovePicWatermak.pptx', FileFormat.Pptx2010); System.Diagnostics.Process.Start('RemovePicWatermak.pptx'); } }}