C# 添加、删除PPT水印

【前言】

水印是一种有效的文档防伪手段,在工作中非常实用。在接下来的示例中,将介绍如何通过C#编程语言来实现Power Point幻灯片添加水印。我们知道,水印可以分为文本水印、图片水印,在此也将分别介绍实现两种水印效果的具体方法。另外,水印幻灯片中已经存在的水印,如果我们想要去除水印效果,也可以参考下面的关于删除水印的方法。

【工具】

* Free Spire.Presentation for .NET 3.3 (社区版)
编辑代码时,注意添加引用Spire.Presentation.dll(dll文件可在安装路径下的Bin文件夹中获取)

C#  添加、删除PPT水印

【示例1】添加文本水印

using System;
using System.Text;
using Spire.Presentation;
using System.Drawing;
using Spire.Presentation.Drawing;
using System.Windows.Forms;

namespace InsertWatermark_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化一个Presentation类实例并加载文档
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.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(150, Color.LightBlue);
            textRange.FontHeight = 90;

            //保存并打开文档
            ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("TextWatermark.pptx");
        }
    }
}

文本水印添加效果:
C#  添加、删除PPT水印

【示例2】添加图片水印

using System;
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Drawing;

namespace ImageWatermark_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化一个Presentation类实例并加载文档
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.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("1.jpg");
            IImageData image = ppt.Images.Append(img);
            ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

            //保存并打开文档
            ppt.SaveToFile("ImageWatermark.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("ImageWatermark.pptx");
        }
    }
}

图片水印添加效果:
C#  添加、删除PPT水印

【示例3】删除文本水印效果

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

删除效果:
C#  添加、删除PPT水印

【示例4】删除图片水印效果

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水印

(本文完)

转载于:https://blog.51cto.com/eiceblue/2299135

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用[1]: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; 问题: C#批量添加图片水印 回答: 在C#中,可以使用System.Drawing命名空间中的类来实现批量添加图片水印的功能。首先,你需要引用System.Drawing命名空间,并使用System.IO命名空间来处理文件操作。然后,你可以使用Bitmap类来加载图片,使用Graphics类来绘制水印,并使用Image类来保存修改后的图片。以下是一个简单的示例代码: ```csharp using System; using System.Drawing; using System.IO; namespace WatermarkExample { class Program { static void Main(string[] args) { string watermarkText = "Watermark"; // 水印文本 string imagePath = "image.jpg"; // 图片路径 string outputDirectory = "output"; // 输出目录 // 创建输出目录 Directory.CreateDirectory(outputDirectory); // 加载图片 using (Bitmap image = new Bitmap(imagePath)) { // 创建Graphics对象 using (Graphics graphics = Graphics.FromImage(image)) { // 设置水印字体和颜色 Font font = new Font("Arial", 12); Brush brush = new SolidBrush(Color.Red); // 计算水印位置 SizeF textSize = graphics.MeasureString(watermarkText, font); PointF position = new PointF(image.Width - textSize.Width - 10, image.Height - textSize.Height - 10); // 绘制水印 graphics.DrawString(watermarkText, font, brush, position); } // 保存修改后的图片 string outputImagePath = Path.Combine(outputDirectory, "watermarked_image.jpg"); image.Save(outputImagePath); } Console.WriteLine("水印添加完成!"); } } } ``` 在上述示例代码中,你可以修改`watermarkText`变量来设置水印文本,`imagePath`变量来设置要添加水印的图片路径,`outputDirectory`变量来设置输出目录。运行代码后,修改后的图片将保存在输出目录中。请确保你已经正确引用了System.Drawing和System.IO命名空间,并且已经将图片文件放置在正确的路径下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值