-
C#/VB.NET 添加多行文本水印到Word文档
一般情况下,在Word中添加文字水印仅支持添加一个文本字样的水印,但在复杂的办公环境中,由于对不同文档的设计要求,需要在Word文档中添加平铺水印效果,即文档中的水印文字以多行多列分布的形式存在。本文将介绍如何来实现该水印效果的方法,下面是详细步骤及方法。
dll引用
通过 NuGet 引入dll(2种方法)的方法
1.可以在Visual Studio中打开 【解决方案资源管理器】,鼠标右键点击 【引用】,【管理NuGet包】,然后搜索 【Free Spire.Doc】,点击【安装】。等待程序安装完成。
2.将以下内容复制到PM控制台安装:
Install-Package FreeSpire.Doc -Version 10.2
手动添加dll引用的方法
可通过手动 下载包 到本地,然后解压,找到BIN文件夹下的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。
添加多行多列文字水印
在Word中添加多行文字水印时,实现的方法是通过在页眉中添加形状艺术字,并通过多次复制形状来模拟实现多行文字水印效果。以下是实现水印添加的主要代码步骤:
- 创建Document类的对象,并调用Document.LoadFromFile(string fileName)方法加载Word文档。
- 创建ShapeObject类的实例,并通过ShapeObject.Width、ShapeObject.Height、ShapeObject.VerticalPosition、ShapeObject.Rotation、ShapeObject.WordArt.Text、ShapeObject.WordArt.FontFamily、ShapeObject.FillColor等属性设置形状大小、位置、旋转角度、水印文字、字体及颜色等。
- for循环遍历所有Section,通过Section.HeadersFooters.Header属性获取页眉,并以HeaderFooter.AddParagraph()方法添加段落到页眉。
- 通过for循环以ShapeObject.Clone()方法多次复制形状,并通过ShapeObject.VerticalPosition和ShapeObject.HorizontalPosition属性设置形状位置排列。
- 调用Paragraph.ChildObjects.Add(IDocumentObject entity)方法添加形状到页眉段落。
- 最后,通过Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文档到指定路径。
C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace MultiLineTextWatermark { class Program { static void Main(string[] args) { //加载Word文档 Document doc = new Document(); doc.LoadFromFile("test.docx"); //创建形状,并设置大小、水印文字、位置及样式 ShapeObject shape = new ShapeObject(doc, ShapeType.TextPlainText); shape.Width = 60; shape.Height =15; shape.VerticalPosition = 25; shape.HorizontalPosition = 20; shape.Rotation = 320; shape.WordArt.Text = "草稿副本"; shape.WordArt.FontFamily = "宋体"; shape.FillColor = System.Drawing.Color.Red; shape.StrokeColor = System.Drawing.Color.Red; //遍历所有section for (int n = 0; n < doc.Sections.Count; n++) { Section section = doc.Sections[n]; //获取页眉 HeaderFooter header = section.HeadersFooters.Header; //添加段落到页眉 Paragraph paragraph1 = header.AddParagraph(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { //复制形状并设置多行多列位置 shape = (ShapeObject)shape.Clone(); shape.VerticalPosition = 50 + 150 * i; shape.HorizontalPosition = 20 + 160 * j; //添加形状到段落 paragraph1.ChildObjects.Add(shape); } } } //保存文档 doc.SaveToFile("result.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("result.docx"); } } }
VB.NET
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace MultiLineTextWatermark Class Program Private Shared Sub Main(args As String()) '加载Word文档 Dim doc As New Document() doc.LoadFromFile("test.docx") '创建形状,并设置大小、水印文字、位置及样式 Dim shape As New ShapeObject(doc, ShapeType.TextPlainText) shape.Width = 60 shape.Height = 15 shape.VerticalPosition = 25 shape.HorizontalPosition = 20 shape.Rotation = 320 shape.WordArt.Text = "草稿副本" shape.WordArt.FontFamily = "宋体" shape.FillColor = System.Drawing.Color.Red shape.StrokeColor = System.Drawing.Color.Red '遍历所有section For n As Integer = 0 To doc.Sections.Count - 1 Dim section As Section = doc.Sections(n) '获取页眉 Dim header As HeaderFooter = section.HeadersFooters.Header '添加段落到页眉 Dim paragraph1 As Paragraph = header.AddParagraph() For i As Integer = 0 To 4 For j As Integer = 0 To 5 '复制形状并设置多行多列位置 shape = DirectCast(shape.Clone(), ShapeObject) shape.VerticalPosition = 50 + 150 * i shape.HorizontalPosition = 20 + 160 * j '添加形状到段落 paragraph1.ChildObjects.Add(shape) Next Next Next '保存文档 doc.SaveToFile("result.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("result.docx") End Sub End Class End Namespace
水印效果:
—END—
出处:https://www.cnblogs.com/Yesi/p/16493239.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数