Word中设置水印时,可预设的文字或自定义文字设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法。下面,将以C# 代码为例,对Word每一页设置不同的文字水印效果作详细介绍。


方法思路

在给Word每一页添加水印前,首先需要在Word文档每一页正文的最后一个字符后面插入“连续”分节符,然后在每一节的页眉段落里添加水印图片,并设置图片的坐标位置、对齐方式、衬于文字下方等。最后保存文档。

dll引用

方法1

在程序中引入Spire.Doc.dll文件;将Spire.Doc for .NET下载到本地,解压,找到BIN文件夹下的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。

方法2

通过NuGet安装。可通过以下2种方法安装:

1.可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Spire.Doc”,点击“安装”。等待程序安装完成。

2.将以下内容复制到PM控制台安装。

Install-Package Spire.Doc -Version 10.1.14

代码示例

给每页添加图片水印时,可参考如下步骤:

  • 创建Document类的对象,并通过LoadFromFile(string fileName)方法加载Word文档。
  • 通过Document.Sections[]属性获取指定节。
  • 通过HeadersFooters.Header属性获取页眉,HeaderFooter.AddParagraph()方法添加段落到页眉。
  • 创建ShapeObject类的对象,并传入参数设置形状类型为TextPlainText类型的艺术字。并调用方法设置艺术字样式,如艺术字高度、宽度、旋转、颜色、对齐方式等。
  • 使用DocumentObjectCollection.Add(IDocumentObject)方法将艺术字添加到段落。
  • 最后,通过Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文档。

不同页面中设置不一样的图片水印效果,只需要获取该页面对应的节,然后参考上述用到的方法来添加即可。

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing; namespace TextWatermark2
{
class Program
{
static void Main(string[] args)
{
//加载Word测试文档
Document doc = new Document();
doc.LoadFromFile("test.docx"); //获取文档第一节
Section section1 = doc.Sections[0]; //定义水印文字的纵向坐标位置
float y = section1.PageSetup.PageSize.Height/3; //添加文字水印1
HeaderFooter header1 = section1.HeadersFooters.Header;//获取页眉
header1.Paragraphs.Clear();//删除原有页眉格式的段落
Paragraph para1 = header1.AddParagraph();//重新添加段落 //添加艺术字并设置大小
ShapeObject shape1 = new ShapeObject(doc, ShapeType.TextPlainText);
shape1.Width = 362;
shape1.Height = 118;
//设置艺术字文本内容、位置及样式(即文本水印字样)
shape1.Rotation = 315;
shape1.WordArt.Text = "内部使用";
shape1.FillColor = Color.ForestGreen;
shape1.LineStyle = ShapeLineStyle.Single;
shape1.StrokeColor = Color.ForestGreen;
shape1.StrokeWeight = 0.5;
shape1.VerticalPosition = y;
shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center;
para1.ChildObjects.Add(shape1); //同理设置第二节页眉中的文字水印2
Section section2 = doc.Sections[1];
HeaderFooter header2 = section2.HeadersFooters.Header;
header2.Paragraphs.Clear();
Paragraph para2 = header2.AddParagraph();
ShapeObject shape2 = new ShapeObject(doc, ShapeType.TextPlainText);
shape2.Width = 362;
shape2.Height = 118;
shape2.Rotation = 315;
shape2.WordArt.Text = "绝密资料";
shape2.FillColor = Color.HotPink;
shape2.LineStyle = ShapeLineStyle.Single;
shape2.StrokeColor = Color.HotPink;
shape2.StrokeWeight = 0.5;
shape2.VerticalPosition = y;
shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center;
para2.ChildObjects.Add(shape2); //同理设置第三节中的页眉中的文字水印3
Section section3 = doc.Sections[2];
HeaderFooter header3 = section3.HeadersFooters.Header;
header3.Paragraphs.Clear();
Paragraph para3 = header3.AddParagraph();
ShapeObject shape3 = new ShapeObject(doc, ShapeType.TextPlainText);
shape3.Width = 362;
shape3.Height = 118;
shape3.Rotation = 315;
shape3.WordArt.Text = "禁止传阅";
shape3.FillColor = Color.DarkOrange;
shape3.LineStyle = ShapeLineStyle.Single;
shape3.StrokeColor = Color.DarkOrange;
shape3.StrokeWeight = 0.5;
shape3.VerticalPosition = y;
shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center;
para3.ChildObjects.Add(shape3); //保存文档
doc.SaveToFile("DifferentTextWatermark.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("DifferentTextWatermark.docx");
}
}
}

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing Namespace TextWatermark2
Class Program
Private Shared Sub Main(args As String())
'加载Word测试文档
Dim doc As New Document()
doc.LoadFromFile("test.docx") '获取文档第一节
Dim section1 As Section = doc.Sections(0) '定义水印文字的纵向坐标位置
Dim y As Single = section1.PageSetup.PageSize.Height / 3 '添加文字水印1
Dim header1 As HeaderFooter = section1.HeadersFooters.Header
'获取页眉
header1.Paragraphs.Clear()
'删除原有页眉格式的段落
Dim para1 As Paragraph = header1.AddParagraph()
'重新添加段落
'添加艺术字并设置大小
Dim shape1 As New ShapeObject(doc, ShapeType.TextPlainText)
shape1.Width = 362
shape1.Height = 118
'设置艺术字文本内容、位置及样式(即文本水印字样)
shape1.Rotation = 315
shape1.WordArt.Text = "内部使用"
shape1.FillColor = Color.ForestGreen
shape1.LineStyle = ShapeLineStyle.[Single]
shape1.StrokeColor = Color.ForestGreen
shape1.StrokeWeight = 0.5
shape1.VerticalPosition = y
shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center
para1.ChildObjects.Add(shape1) '同理设置第二节页眉中的文字水印2
Dim section2 As Section = doc.Sections(1)
Dim header2 As HeaderFooter = section2.HeadersFooters.Header
header2.Paragraphs.Clear()
Dim para2 As Paragraph = header2.AddParagraph()
Dim shape2 As New ShapeObject(doc, ShapeType.TextPlainText)
shape2.Width = 362
shape2.Height = 118
shape2.Rotation = 315
shape2.WordArt.Text = "绝密资料"
shape2.FillColor = Color.HotPink
shape2.LineStyle = ShapeLineStyle.[Single]
shape2.StrokeColor = Color.HotPink
shape2.StrokeWeight = 0.5
shape2.VerticalPosition = y
shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center
para2.ChildObjects.Add(shape2) '同理设置第三节中的页眉中的文字水印3
Dim section3 As Section = doc.Sections(2)
Dim header3 As HeaderFooter = section3.HeadersFooters.Header
header3.Paragraphs.Clear()
Dim para3 As Paragraph = header3.AddParagraph()
Dim shape3 As New ShapeObject(doc, ShapeType.TextPlainText)
shape3.Width = 362
shape3.Height = 118
shape3.Rotation = 315
shape3.WordArt.Text = "禁止传阅"
shape3.FillColor = Color.DarkOrange
shape3.LineStyle = ShapeLineStyle.[Single]
shape3.StrokeColor = Color.DarkOrange
shape3.StrokeWeight = 0.5
shape3.VerticalPosition = y
shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center
para3.ChildObjects.Add(shape3) '保存文档
doc.SaveToFile("DifferentTextWatermark.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("DifferentTextWatermark.docx")
End Sub
End Class
End Namespace

如图,每一页均可显示不同的文字水印效果:

★相关推荐阅读:C# 给Word每一页设置不同图片水印

—END—

C# 给Word每一页设置不同文字水印的更多相关文章

  1. Java 给Word每一页设置不同文字水印效果

    Word中设置水印时,可预设的文字或自定义文字设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法.下面,将以Ja ...

  2. Java 给Word每一页设置不同图片水印效果

    Word中设置水印时,可加载图片设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法.下面,将以Java代码为例, ...

  3. C# 给Word每一页设置不同图片水印

    Word中设置水印时,可加载图片设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法.下面,将以C#代码为例,对W ...

  4. Java 如何给Word文档添加多行文字水印

    前言 我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印.关于文本水印,之前那篇教程里主要指的是单行字体的水印,而在操作Word文档时,有时也会碰到需要添加多行文 ...

  5. word从任意页设置页码

    把所有页都设置页码 首先设置分隔符,下一页 在第二节中,找到插入页码,设置起始页码为1即可

  6. Word 最后一页无法删除-解决办法

    Word 最后一页无法删除-解决办法 制服 word 最后一页无法删除 今天在做一个简历的时候,编辑 word 文档的时候,最后一页空白页怎么也删不掉,百度了很多方法之后,只有一个可行,记录一下. 1 ...

  7. 使用 Word 写作论文时设置格式技巧记录

    这里主要记录使用 Word 2013 版本的 Microsoft office Word 软件进行论文书写时的一些常用的格式设置技巧,以供分享与记录. Word文档页脚添加页码 Word设置多级标题格 ...

  8. word页码上加横线&&word删除单页页眉

    word(2010)页码上加横线 插入——>页脚(选择年刊型)——>如图 然后拖住“竖条条”将页码拖到正中间——>点中页脚右击——>选中“表格属性”——>“边框和底纹”— ...

  9. iOS启动页设置

    点击项目->TARGETS->App Icons and Launch Images->Launch Images Source->Use Asset Catalog...-& ...

随机推荐

  1. HamsterBear Linux Low Res ADC按键驱动的适配 + LVGL button移植

    HamsterBear lradc按键驱动的适配 平台 - F1C200s Linux版本 - 5.17.2 ADC按键 - 4 KEY tablet 驱动程序位于主线内核: drivers/inpu ...

  2. 好客租房19-react组件基础目标

    1能够使用函数创建组件 2能够使用class创建组件 3能够给react元素绑定事件 4能够使用state和setstate 5能够处理事件中的this指向问题 6能够使用受控组件处理表单

  3. 数仓选型必列入考虑的OLAP列式数据库ClickHouse(中)

    实战 案例使用 背景 ELK作为老一代日志分析技术栈非常成熟,可以说是最为流行的大数据日志和搜索解决方案:主要设计组件及架构如下: 而新一代日志监控选型如ClickHouse.StarRocks特别是 ...

  4. el-form 中的数组表单验证(数组可动态添加删除)

    除了一些简单的表单验证之外,我们还会有一些稍微复杂点的多层级表单的验证,如下图所示可点击添加,删除对数组进行操作,当点击确定时需要验证每一条form-item不能为空 其tempalte部分主要代码如 ...

  5. AI趋势量化系统(Binance升级版)

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. B圈大跌行情,如何应对? 近期,伴随着美联储持续的加息进程,数字货币市场不论是市场焦点LUNA,还是BT ...

  6. Druid数据库连接池使用体验

    写在前面 在实际工作中我们我们使用较多的则是Spring默认的HikariDataSource数据库连接池,但是它无法提供可视化监控SQL这一能力,而这在很多场景下往往又是我们需要的功能,因此今天来学 ...

  7. Java实现http大文件流读取并批量插入数据库

    1.概述 请求远程大文本,使用流的方式进行返回.需要设置http链接的超时时间 循环插入到List中,使用mybatis-plus批量插入到mysql中 2.需求 两台服务器 大文件放到其中一台服务器 ...

  8. 【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结

    华为运动健康服务(HUAWEI Health Kit)为三方生态应用提供了REST API接口,通过其接口可访问数据库,为用户提供运动健康类数据服务.在实际的集成过程中,开发者们可能会遇到各种问题,这 ...

  9. Linux查看日志文件写入速度的4种方法

    原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 有时,我们需要查看某个文件的增长速度,如日志文件,以此来感受系统的负载情况,因为一般情况下,日志写入越快,说明系统 ...

  10. 中国天气api接口xml,json

    http://m.weather.com.cn/data/101110101.html 大坑有木有??反应慢不说了,还老不更新!! 想贴段代码的,现在又打不 开了(貌似3月4号以后没更新过) ==== ...