本文将详细介绍如何使用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
效果图: