標籤:type back 閱讀 dll nis .sh 參考 download 操作
對文檔添加浮水印可以有效聲明和保護文檔,是保護重要檔案的方式之一。在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