C# 处理PPT水印(三)—— 在PPT中添加多行(平铺)文本水印效果

在PPT幻灯片中,可通过添加形状的方式,来实现类似水印的效果,可添加单一文本水印效果,即幻灯片中只有一个文本水印;也可以添加多行(平铺)文本水印效果,即幻灯片中以一定方式平铺排列多个文本水印效果。本文主要以C#程序代码为例介绍第二种水印添加方法,另附VB.NET代码供参考。

程序环境

  • 需引入以下程序集文件,如图:

 

 

其中,Spire.Presentation.dll程序集,需下载安装至本地(也可以通过Nuget下载),这里使用的免费版

  • .NET Framework 4.8

 

详细代码

【C#】

复制代码
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TextWatermark2
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载PPT文档
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");

            //获取需要添加的水印的幻灯片(第一张幻灯片)
            ISlide slide = ppt.Slides[0];

            //创建水印文本
            Font font = new Font("宋体", 20);
            String watermarkText = "内部资料";
            SizeF size = TextRenderer.MeasureText(watermarkText, font);

            //指定水印添加的起始坐标位置
            float x = 50;
            float y = 80;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    //绘制文本,设置文本格式
                    RectangleF rect = new RectangleF(x, y, size.Width, size.Height);
                    IAutoShape shape = slide.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 = watermarkText;
                    TextRange textRange = shape.TextFrame.TextRange;
                    textRange.Fill.FillType = FillFormatType.Solid;
                    textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink);
                    textRange.EastAsianFont = new TextFont(font.Name);
                    textRange.FontHeight = font.Size;
                    x += (100 + size.Width);
                }
                x = 30;
                y += (100 + size.Height);
            }

            //保存文档
            ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2013);
            System.Diagnostics.Process.Start("TextWatermark.pptx");
        }
    }
}
复制代码

【VB.NET】

复制代码
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing
Imports System.Windows.Forms

Namespace TextWatermark2
    Class Program
        Private Shared Sub Main(args As String())
            '加载PPT文档
            Dim ppt As New Presentation()
            ppt.LoadFromFile("test.pptx")

            '获取需要添加的水印的幻灯片(第一张幻灯片)
            Dim slide As ISlide = ppt.Slides(0)

            '创建水印文本
            Dim font As New Font("宋体", 20)
            Dim watermarkText As [String] = "内部资料"
            Dim size As SizeF = TextRenderer.MeasureText(watermarkText, font)

            '指定水印添加的起始坐标位置
            Dim x As Single = 50
            Dim y As Single = 80
            For i As Integer = 0 To 3
                For j As Integer = 0 To 3
                    '绘制文本,设置文本格式
                    Dim rect As New RectangleF(x, y, size.Width, size.Height)
                    Dim shape As IAutoShape = slide.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 = watermarkText
                    Dim textRange As TextRange = shape.TextFrame.TextRange
                    textRange.Fill.FillType = FillFormatType.Solid
                    textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink)
                    textRange.EastAsianFont = New TextFont(font.Name)
                    textRange.FontHeight = font.Size
                    x += (100 + size.Width)
                Next
                x = 30
                y += (100 + size.Height)
            Next

            '保存文档
            ppt.SaveToFile("TextWatermark.pptx", FileFormat.Pptx2013)
            System.Diagnostics.Process.Start("TextWatermark.pptx")
        End Sub
    End Class
End Namespace
复制代码

完成代码后,执行程序,生成结果文档。在结果文档中可查看水印添加效果,如下图:

 

其他关于C#操作PPT水印的方法可参考以下相关文章:

 

(本文完)

转载请注明出处!

posted @   E-iceblue  阅读(1094)  评论(1编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示