C#实现给Word每一页设置不同文字水印的方法详解
作者:E-iceblue 发布时间:2023-07-01 18:49:40
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
如图,每一页均可显示不同的文字水印效果:
来源:https://www.cnblogs.com/Yesi/p/16520912.html


猜你喜欢
- 关于java图片验证码的文章最近更新了不少,帮助大家掌握java验证码的生成技术,下文为大家分享了java生成图片验证码最简单的方法,供大家
- 现在的大部分框架都是 MVC 模式,但 MVC 三个部分怎么配合,这里做了一点总结:基本原则:业务逻辑代码应该写在 M 里面,而应用程序逻辑
- Spring官网上有一篇Getting Start,介绍了如何使用Docker发布Spring Boot应用,算是比较详细了,不过有些细节没
- 常用配置ObjectMapper objectMapper = new ObjectMapper();objectMapper.config
- Java.io 包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。Java.io 包中的流支持很多种格式,比如:基
- 一、简介线程安全概念:线程安全是指在当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用。不会出
- Android Studio打包.so库到apk中实例详解由于在原来的ADT的Eclipse环境中,用ndk_build工具生成了相应的各个
- 部署在tomcat容器中首先需要添加一些新的包和启动程序1.在pom.xml文件中packaging便签下 jar 改为 war<pa
- 引言 在一些项目中或是一些特殊的业务场景中,需要用到显示系统的当前时间,以及一些
- 最近项目上需要实现蓝牙传输apk的一个功能,能够搜索周围的蓝牙手机并分享文件。从需求上讲android手机自带的蓝牙传输模块就可以满足需要了
- static void Main(string[] args) &nb
- 本文实例为大家分享了Android实现层叠卡片式banner的具体代码,供大家参考,具体内容如下效果图如下:背景由于公司VIP模块项目需要,
- 为什么使用Kotlin项目一期在收尾了终于有时间折腾了,一个多月以来Kotlin从入门到现在,坚持用来开发的切身感受。因为语法与Java的区
- 一、原文翻译WorkManager API 可以很容易的指定可延迟的异步任务。允许你创建任务,并把它交给WorkManager来立即运行或在
- 本文实例为大家分享了C语言实现银行系统的具体代码,供大家参考,具体内容如下1.实现要求生成一个1000-1000000之间的随机数来代表账户
- 前言最近公司在重构广告系统,其中核心的打包功由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统。因此,为
- 这一篇写一下springboot整合solr,代码已经上传到github,传送门。1、新建core并配置schemasolr create
- 简单介绍一下Java中的Excel文件导出功能(基于HttpServletResponse实现下载)首先,引入需要依赖的jar包:<d
- 在学习和回顾该知识前,已经掌握了if语句的结构和用法。if (条件) 语句;当条件满足的情况下,if结构
- 1、一个示例回顾Future一些业务场景我们需要使用多线程异步执行任务,加快任务执行速度。JDK5新增了Future接口,用于描述一个异步计