C#實現(xiàn)添加多行文本水印到Word文檔
一般情況下,在Word中添加文字水印僅支持添加一個文本字樣的水印,但在復(fù)雜的辦公環(huán)境中,由于對不同文檔的設(shè)計要求,需要在Word文檔中添加平鋪水印效果,即文檔中的水印文字以多行多列分布的形式存在。本文將介紹如何來實現(xiàn)該水印效果的方法,下面是詳細步驟及方法。
dll引用
通過 NuGet 引入dll(2種方法)的方法
1.可以在Visual Studio中打開 【解決方案資源管理器】,鼠標右鍵點擊 【引用】,【管理NuGet包】,然后搜索 【Free Spire.Doc】,點擊【安裝】。等待程序安裝完成。
2.將以下內(nèi)容復(fù)制到PM控制臺安裝:
Install-Package FreeSpire.Doc -Version 10.2
手動添加dll引用的方法
可通過手動 下載包 到本地,然后解壓,找到BIN文件夾下的Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標右鍵點擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
添加多行多列文字水印
在Word中添加多行文字水印時,實現(xiàn)的方法是通過在頁眉中添加形狀藝術(shù)字,并通過多次復(fù)制形狀來模擬實現(xiàn)多行文字水印效果。以下是實現(xiàn)水印添加的主要代碼步驟:
- 創(chuàng)建Document類的對象,并調(diào)用Document.LoadFromFile(string fileName)方法加載Word文檔。
- 創(chuàng)建ShapeObject類的實例,并通過ShapeObject.Width、ShapeObject.Height、ShapeObject.VerticalPosition、ShapeObject.Rotation、ShapeObject.WordArt.Text、ShapeObject.WordArt.FontFamily、ShapeObject.FillColor等屬性設(shè)置形狀大小、位置、旋轉(zhuǎn)角度、水印文字、字體及顏色等。
- for循環(huán)遍歷所有Section,通過Section.HeadersFooters.Header屬性獲取頁眉,并以HeaderFooter.AddParagraph()方法添加段落到頁眉。
- 通過for循環(huán)以ShapeObject.Clone()方法多次復(fù)制形狀,并通過ShapeObject.VerticalPosition和ShapeObject.HorizontalPosition屬性設(shè)置形狀位置排列。
- 調(diào)用Paragraph.ChildObjects.Add(IDocumentObject entity)方法添加形狀到頁眉段落。
- 最后,通過Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文檔到指定路徑。
實現(xiàn)代碼
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");
//創(chuàng)建形狀,并設(shè)置大小、水印文字、位置及樣式
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++)
{
//復(fù)制形狀并設(shè)置多行多列位置
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")
'創(chuàng)建形狀,并設(shè)置大小、水印文字、位置及樣式
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
'復(fù)制形狀并設(shè)置多行多列位置
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水印效果:

到此這篇關(guān)于C#實現(xiàn)添加多行文本水印到Word文檔的文章就介紹到這了,更多相關(guān)C#添加文本水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# WinForm窗體編程中處理數(shù)字的正確操作方法
這篇文章主要介紹了C# WinForm窗體編程中處理數(shù)字的正確操作方法,本文給出了正確示例,并解釋了為什么要這么做,需要的朋友可以參考下2014-08-08
c#創(chuàng)建windows服務(wù)入門教程實例
windows服務(wù)是windows系統(tǒng)中一類特殊的應(yīng)用程序,一般情況下它們只會在后臺運行,不會影響前臺操作,非常適合做一些不需要用戶參與的而又需要長時間執(zhí)行的任務(wù)2014-04-04
使用C#開源文件實時監(jiān)控工具Tail&TailUI介紹
本篇文章小編為大家介紹,使用C#開源文件實時監(jiān)控工具Tail&TailUI介紹。需要的朋友參考下2013-04-04

