文档星空最火知名网站>>Spire.Doc系列教程>>Word .NET库组件Spire.Doc系列教程(47):添加多行文字水印到 Word 文档
Word .NET库组件Spire.Doc系列教程(47):添加多行文字水印到 Word 文档
Spire.Doc for .NET是一个专业的Word .NET库,设计用于帮助开发人员高效地开发创建、阅读、编写、转换和打印任何来自.NET( C#, VB.NET, ASP.NET)平台的Word文档文件的功能。
本系列教程将为大家带来Spire.Doc for .NET在使用过程中的各类实际操作,本文将介绍如何使用Spire.Doc for .NET比较两个Word文档的内容差异。
通常情况下,Microsoft word只支持添加一个文本水印到Word文档。该文将介绍如何使用Spire.Doc for .NET 在Word文档中页眉中添加艺术字模拟实现Word文档中多行文字水印效果。
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace WordWatermark
{
class Program
{
static void Main(string[] args)
{
//加载示例文档
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
//添加艺术字并设置大小
ShapeObject shape = new ShapeObject(doc,ShapeType.TextPlainText);
shape.Width = 60;
shape.Height = 20;
//设置艺术字文本内容、位置及样式
shape.VerticalPosition = 30;
shape.HorizontalPosition = 20;
shape.Rotation = 315;
shape.WordArt.Text = "内部使用";
shape.FillColor = System.Drawing.Color.Red;
shape.StrokeColor = System.Drawing.Color.Gray;
Section section;
HeaderFooter header;
for (int n = 0; n < doc.Sections.Count; n++) { section = doc.Sections[n]; //获取section的页眉 header = section.HeadersFooters.Header; Paragraph paragraph1; for (int i = 0; i < 3; i++) { //添加段落到页眉 paragraph1 = header.AddParagraph(); for (int j = 0; j < 4; 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
Class Program
Private Shared Sub Main(ByVal args As String())
Dim doc As Document = New Document()
doc.LoadFromFile("Sample.docx")
Dim shape As ShapeObject = New ShapeObject(doc, ShapeType.TextPlainText)
shape.Width = 60
shape.Height = 20
shape.VerticalPosition = 30
shape.HorizontalPosition = 20
shape.Rotation = 315
shape.WordArt.Text = "内部使用"
shape.FillColor = System.Drawing.Color.Red
shape.StrokeColor = System.Drawing.Color.Gray
Dim section As Section
Dim header As HeaderFooter
For n As Integer = 0 To doc.Sections.Count - 1
section = doc.Sections(n)
header = section.HeadersFooters.Header
Dim paragraph1 As Paragraph
For i As Integer = 0 To 3 - 1
paragraph1 = header.AddParagraph()
For j As Integer = 0 To 4 - 1
shape = CType(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)
End Sub
End Class
多行多列文本水印效果图:
推荐阅读:【想要快速完成文档格式转换吗?Spire系列组件格式转换完整攻略来啦!】
还想要更多吗?您可以点击阅读【2020 · E-iceblue最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时,我们很高兴为您提供查询和咨询。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线