本文將對C#處理PPT幻燈片中的水印進一步說明和介紹。在C# 處理PPT水印(一)一文中,分享了如何插入水印效果的方法,包括插入文字水印效果、插入圖片作為水印效果兩種情況,那對於不需要水印效果的情況,要如何來去除PPT中已有的水印效果呢,具體實現步驟,可參考下面將要講述的方法。
工具
PS:安裝后,注意在編輯代碼時,添加引用Spire.Presentation.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)

代碼示例(供參考)
【示例1】去除文字水印效果
測試文件中的文字水印效果如下:

步驟1 :實例化Presentation類,加載含有水印效果的PPT文檔
Presentation ppt = new Presentation(); ppt.LoadFromFile("TextWatermark.pptx");
步驟2 :遍歷所有幻燈片,查找包含水印字樣的shape,並刪除
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); } } } }
步驟3:保存文檔並打開
ppt.SaveToFile("RemoveTextWatermak.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("RemoveTextWatermak.pptx");
文字水印去除效果:

全部代碼:
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"); } } }
【示例2】去除圖片水印效果
測試文件中的圖片水印效果如下:

步驟1 :實例化Presentation類,加載測試文檔
Presentation ppt = new Presentation(); ppt.LoadFromFile("ImageWatermark.pptx");
步驟2 :遍歷每一張幻燈片, 設置背景填充類型為None
for (int i = 0; i < ppt.Slides.Count; i++) { ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.None; }
步驟3 :保存文檔並打開
ppt.SaveToFile("RemovePicWatermak.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("RemovePicWatermak.pptx");
圖片水印去除效果:

全部代碼:
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水印效果的方法介紹。
(本文完)
轉載請注明出處!
