本文将详细介绍如何使用c# 给ppt文档添加图片和文本水印的方法。
添加图片水印
spire.presentation类提供了一个属性slidebackground,我们通过该属性来设置当前页面的背景图以用作图片水印。
c#
//加载ppt文档
presentation ppt = new presentation();
ppt.loadfromfile("sample.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("logo.png");
iimagedata image = ppt.images.append(img);
ppt.slides[0].slidebackground.fill.picturefill.picture.embedimage = image;
//保存文档
ppt.savetofile("imagewatermark.pptx", fileformat.pptx2010);
vb.net
'加载ppt文档
dim ppt as new presentation()
ppt.loadfromfile("sample.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
'获取图片并将其设置为页面的背景图
dim img as image = image.fromfile("logo.png")
dim image__1 as iimagedata = ppt.images.append(img)
ppt.slides(0).slidebackground.fill.picturefill.picture.embedimage = image__1
'保存文档
ppt.savetofile("imagewatermark.pptx", fileformat.pptx2010)
添加文本水印
添加文本水印时,需要先绘制文本并设置文本格式如字体、颜色及排列方式等,然后将其添加到页面作为水印。
c#
//加载ppt文档
presentation ppt = new presentation();
ppt.loadfromfile("sample.pptx", fileformat.pptx2010);
//获取文本的大小
font stringfont = new font("arial", 45);
size size = textrenderer.measuretext("e-iceblue", stringfont);
//绘制文本,设置文本格式并将其添加到第二张幻灯片
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[1].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 = "e-iceblue";
textrange textrange = shape.textframe.textrange;
textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid;
textrange.fill.solidcolor.color = color.fromargb(120, color.black);
textrange.fontheight = 45;
//保存文档
ppt.savetofile("textwatermark.pptx", fileformat.pptx2010);
vb.net
'加载ppt文档
dim ppt as new presentation()
ppt.loadfromfile("sample.pptx", fileformat.pptx2010)
'获取文本的大小
dim stringfont as new font("arial", 45)
dim size as size = textrenderer.measuretext("e-iceblue", stringfont)
'绘制文本,设置文本格式并将其添加到第二张幻灯片
dim rect as new rectanglef((ppt.slidesize.size.width - size.width) / 2, (ppt.slidesize.size.height - size.height) / 2, size.width, size.height)
dim shape as iautoshape = ppt.slides(1).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 = "e-iceblue"
dim textrange as textrange = shape.textframe.textrange
textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid
textrange.fill.solidcolor.color = color.fromargb(120, color.black)
textrange.fontheight = 45
'保存文档
ppt.savetofile("textwatermark.pptx", fileformat.pptx2010)