本文将详细介绍如何使用C# 、VB.NET给PowerPoint文档添加多行多列文本水印。
C#
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WatermarkDemo
{
class Program
{
static void Main(string[] args)
{
//加载示例文档
Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");
//获取文本的大小
Font font = new Font("宋体", 20);
String watermarkText = "冰蓝科技";
SizeF size = TextRenderer.MeasureText("E-iceblue", font);
float x = 30;
float y = 80;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
//绘制文本,设置文本格式并将其添加到第一张幻灯片
RectangleF rect = new RectangleF(x, y, size.Width, size.Height);
IAutoShape shape = presentation.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 = 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);
}
//保存文档
presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010);
}
}
}
VB.NET
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Namespace WatermarkDemo
Class Program
Private Shared Sub Main(ByVal args() As String)
'加载示例文档
Dim presentation As Presentation = New Presentation
presentation.LoadFromFile("Sample.pptx")
'获取文本大小
Dim font As Font = New Font("宋体", 20)
Dim watermarkText As String = "冰蓝科技"
Dim size As SizeF = TextRenderer.MeasureText("E-iceblue", font)
Dim x As Single = 30
Dim y As Single = 80
Dim i As Integer = 0
Do While (i < 3)
Dim j As Integer = 0
Do While (j < 3)
'绘制文本,设置文本格式并将其添加到第一张幻灯片
Dim rect As RectangleF = New RectangleF(x, y, size.Width, size.Height)
Dim shape As IAutoShape = presentation.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 = 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 = (x + (100 + size.Width))
j = (j + 1)
Loop
x = 30
y = (y + (100 + size.Height))
i = (i + 1)
Loop
'保存文档
presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010)
End Sub
End Class
End Namespace
效果图: