在 powerpoint 演示文稿中插入文本水印是保护人生就是博尊龙凯时的版权并确保文档内容真实性的有效方式。spire.presentation for .net 可帮助开发者通过简单的代码添加水印到 powerpoint 幻灯片中,并且支持批量处理,允许精确控制水印的位置和外观,便于集成到更大的 .net 应用程序中。本文将演示如何使用 spire.presentation for .net 库通过 c# 代码插入文字水印到 powerpoint 演示文稿。
安装 spire.presentation for .net
首先,您需要添加 spire.presentation for .net 包中包含的 dll 文件作为.net项目中的引用。dll 文件可以从此链接下载或通过 安装。
pm> install-package spire.presentation
在 powerpoint 幻灯片中插入单一文字水印
借助 spire.presentation for .net,开发者可以通过在每一个幻灯片上创建一个锁定的透明文本框,并以自定义样式插入水印文字到其中,从而实现在 powerpoint 演示文稿中添加单一文本水印。详细步骤如下:
- 创建 presentation 类的实例,并使用 presentation.loadfromfile() 方法加载 powerpoint 文件。
- 定义水印文本,创建 font 对象,并计算文本的大小。
- 通过 presentation.slidesize.size 属性获取演示文稿中幻灯片的大小。
- 基于文本大小和幻灯片大小创建 rectanglef 对象。
- 遍历演示文稿中的幻灯片,在每张幻灯片上执行以下操作:
- 使用 islide.shapes.appendshape() 方法在指定位置创建 iautoshape 对象。
- 使用 iautoshape 类的属性设置形状的样式。
- 通过 iautoshape.textframe.text 属性将水印文本添加到形状中。
- 使用 iautoshape.textframe.textrange 属性将水印文本获取为 textrange 对象。
- 使用 textrange 类的属性设置水印文本的格式。
- 使用 presentation.savetofile() 方法保存演示文稿。
- c#
using spire.presentation;
using spire.presentation.drawing;
using system.drawing;
namespace singletextwatermarkpowerpoint
{
class program
{
static void main(string[] args)
{
// 创建一个 presentation 实例
presentation presentation = new presentation();
// 加载一个 powerpoint 文件
presentation.loadfromfile("sample.pptx");
// 定义水印文本并为其创建字体
string text = "水印文字示例";
// 创建字体对象
font font = new font("harmonyos sans sc", 50);
// 测量水印文本的尺寸
graphics graphics = graphics.fromimage(new bitmap(1, 1));
sizef size = graphics.measurestring(text, font);
// 获取幻灯片尺寸
sizef slidesize = presentation.slidesize.size;
// 基于文本尺寸和页面尺寸创建矩形
rectanglef rect = new rectanglef((slidesize.width - size.width) / 2, (slidesize.height - size.height) / 2, size.width, size.height);
// 遍历演示文稿中的幻灯片
foreach (islide slide in presentation.slides)
{
// 在每张幻灯片上创建一个水印形状
iautoshape watermark = slide.shapes.appendshape(shapetype.rectangle, rect);
// 设置水印的样式
watermark.fill.filltype = fillformattype.none;
watermark.shapestyle.linecolor.color = color.empty;
watermark.rotation = -45;
watermark.locking.selectionprotection = true;
watermark.line.filltype = fillformattype.none;
// 将水印文本添加到形状中
watermark.textframe.text = text;
// 设置水印文本的样式
textrange textrange = watermark.textframe.textrange;
textrange.fill.filltype = fillformattype.solid;
textrange.fill.solidcolor.color = color.fromargb(120, color.hotpink);
textrange.fontheight = 50;
}
// 保存演示文稿
presentation.savetofile("output/powerpoint单一文字水印.pptx", fileformat.auto);
presentation.dispose();
}
}
}
在 powerpoint 幻灯片中插入重复文字水印
开发者还可以在幻灯片上以指定的间隔插入多个相同的文本水印,实现重复水印的效果。详细步骤如下:
- 创建 presentation 类的实例,并使用 presentation.loadfromfile() 方法加载 powerpoint 文件。
- 定义水印文本,创建 font 对象,并计算文本的大小。
- 通过 presentation.slidesize.size 属性获取演示文稿中幻灯片的大小。
- 遍历演示文稿中的幻灯片,执行以下操作:
- 定义起始位置和间隔。
- 添加水印形状和文本,并设置其格式。
- 完成一行水印添加后,将水平位置移动到下一个间隔。
- 完成每行水印添加后,将垂直位置移动到下一行。
- 使用 presentation.savetofile() 方法保存演示文稿。
- c#
using spire.presentation.drawing;
using spire.presentation;
using system.drawing;
namespace repeatedtextwatermarkpowerpoint
{
class program
{
static void main(string[] args)
{
// 创建一个 presentation 实例
presentation presentation = new presentation();
// 加载一个 powerpoint 文件
presentation.loadfromfile("sample.pptx");
// 定义水印文本并为其创建字体
string text = "水印文字示例";
// 创建字体对象
font font = new font("harmonyos sans sc", 20);
// 测量水印文本的尺寸
graphics graphics = graphics.fromimage(new bitmap(1, 1));
sizef size = graphics.measurestring(text, font);
// 获取幻灯片尺寸
sizef slidesize = presentation.slidesize.size;
// 遍历演示文稿中的幻灯片
foreach (islide slide in presentation.slides)
{
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 watermark = slide.shapes.appendshape(shapetype.rectangle, rect);
// 设置水印的样式
watermark.fill.filltype = fillformattype.none;
watermark.shapestyle.linecolor.color = color.empty;
watermark.rotation = -45;
watermark.locking.selectionprotection = true;
watermark.line.filltype = fillformattype.none;
// 将水印文本添加到形状中
watermark.textframe.text = text;
// 设置水印文本的样式
textrange textrange = watermark.textframe.textrange;
textrange.fill.filltype = fillformattype.solid;
textrange.fill.solidcolor.color = color.fromargb(120, color.hotpink);
textrange.fontheight = 20;
x = ((slidesize.width - 60) / 3 size.width / 2);
}
x = 30;
y = ((slidesize.height - 160) / 3 size.height / 2);
}
}
// 保存演示文稿
presentation.savetofile("output/powerpoint重复文字水印.pptx", fileformat.auto);
presentation.dispose();
}
}
}
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。