C# 处理PPT水印(二)——去除水印效果(文本水印、图片水印)

本文将对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");
        }
    }
}
View Code

 

【示例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");
        }
    }
}
View Code

 

以上是关于C# 去除PPT水印效果的方法介绍。

(本文完)

转载请注明出处!

posted @ 2018-10-12 10:56  E-iceblue  阅读(3193)  评论(0编辑  收藏  举报