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

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.