C# 處理PPT水印(三)—— 在PPT中新增多行(平鋪)文字水印效果

iceblue發表於2021-03-01

在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水印的方法可參考以下相關文章:

 

(本文完)

轉載請註明出處!

相關文章