C# 添加、刪除PPT水印
阿新 • • 發佈:2018-10-12
href poi sin n-n 通過 using ++ etc insert 【前言】
水印是一種有效的文檔防偽手段,在工作中非常實用。在接下來的示例中,將介紹如何通過C#編程語言來實現Power Point幻燈片添加水印。我們知道,水印可以分為文本水印、圖片水印,在此也將分別介紹實現兩種水印效果的具體方法。另外,水印幻燈片中已經存在的水印,如果我們想要去除水印效果,也可以參考下面的關於刪除水印的方法。
【工具】
* Free Spire.Presentation for .NET 3.3 (社區版)
編輯代碼時,註意添加引用Spire.Presentation.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)
【示例1】添加文本水印
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"); } } }
文本水印添加效果:
【示例2】添加圖片水印
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"); } } }
圖片水印添加效果:
【示例3】刪除文本水印效果
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"); } } }
刪除效果:
【示例4】刪除圖片水印效果
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");
}
}
}
刪除效果:
(本文完)
C# 添加、刪除PPT水印