1. 程式人生 > >C# 如何添加文本、圖片水印到PPT

C# 如何添加文本、圖片水印到PPT

mini append tran tex present strong 背景圖片 ict type

對文檔添加水印可以有效聲明和保護文檔,是保護重要文件的方式之一。在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", 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(120, Color.Gray);
textRange.FontHeight = 45;

步驟六:保存文檔

ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010);

完成以上代碼步驟後,調試運行項目程序,生成文件(可在該項目文件中bin>Debug中查看),如下圖所示:

技術分享

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);

技術分享

以上是對PPT添加水印的代碼操作,希望該方法能提供幫助,感謝閱讀!

C# 如何添加文本、圖片水印到PPT