對文檔添加水印能夠有效聲明和保護文檔,是保護重要文件的方式之一。在PPT文檔中一樣也能夠設置水印,包括文本水印和圖片水印,本文將講述如何經過Spire.Presentation for .NET來對PPT添加水印,下載安裝Free Spire.Presentationfor .NET後,添加引用dll文件,參考下面的操做步驟,完成水印添加。html
1.添加文本水印ide
步驟一:初始化Presentation類實例,並加載文檔字體
Presentation ppt = new Presentation(); ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx", FileFormat.Pptx2010);
步驟二:初始化一個Font類實例,並實例化字體格式spa
Font stringFont = new Font("Arial", 90); Size size = TextRenderer.MeasureText("內部資料", stringFont);
步驟三:繪製一個shape並指定大小、填充顏色、邊框顏色和旋轉角度3d
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;
步驟五:設置文本大小、顏色code
shape.TextFrame.Text = "內部資料"; TextRange textRange = shape.TextFrame.TextRange; textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid; textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.Gray); textRange.FontHeight = 45;
步驟六:保存文檔orm
ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010);
完成以上代碼步驟後,調試運行項目程序,生成文件(可在該項目文件中bin>Debug中查看),以下圖所示:htm
所有代碼:blog
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", 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); } }
2.添加圖片水印
步驟一:初始化一個Presentation類實例並加載文檔
Presentation ppt = new Presentation(); ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.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:\Users\Administrator\Desktop\images\1.jpg"); IImageData image = ppt.Images.Append(img); ppt.Slides[0].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[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:\Users\Administrator\Desktop\images\1.jpg"); IImageData image = ppt.Images.Append(img); ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image; //保存文檔 ppt.SaveToFile("ImageWatermark1.pptx", FileFormat.Pptx2010); } } }
以上是對PPT添加水印的代碼操做,但願該方法能提供幫助,感謝閱讀!