word是我們日常生活、學習和工作中必不可少的文檔處理工具。精致美觀的文檔能給人帶來閱讀時視覺上的美感。在本篇文章中,將介紹如何使用組件free spire.doc for .net(社區版)給word設置文檔背景。下面的示例中,給word添加背景分為三種情況來講述,即添加純色背景,漸變色背景和圖片背景。
工具使用:下載安裝控件free spire.doc后,在項目程序中添加spire.doc.dll即可(該dll可在安裝文件下bin文件夾中獲取)
一、添加純色背景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using spire.doc; using system.drawing; namespace addbackground { class program { static void main( string [] args) { //創建一個document類對象,并加載word文檔 document document = new document(); document.loadfromfile( @"c:\users\administrator\desktop\test.docx" ); //設置文檔的背景填充模式為顏色填充 document.background.type = spire.doc.documents.backgroundtype.color; //設置背景顏色 document.background.color = color.mistyrose; //保存并打開文檔 document.savetofile( "purebackground.docx" , fileformat.docx2013); system.diagnostics.process.start( "purebackground.docx" ); } } } |
調試運行程序后,生成文檔
二、添加漸變色背景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using spire.doc; using system.drawing; using spire.doc.documents; namespace addgradientbackground { class program { static void main( string [] args) { //創建document類實例,并加載word文檔 document document = new document(); document.loadfromfile( @"c:\users\administrator\desktop\test.docx" ); //設置文檔的背景填充模式為漸變填充 document.background.type = spire.doc.documents.backgroundtype.gradient; //設置漸變背景顏色 backgroundgradient gradient = document.background.gradient; gradient.color1 = color.lightskyblue; gradient.color2 = color.palegreen; //設置漸變模式 gradient.shadingvariant = gradientshadingvariant.shadingmiddle; gradient.shadingstyle = gradientshadingstyle.fromcenter; //保存并打開文檔 document.savetofile( "gradientcolor.docx" , fileformat.docx2013); system.diagnostics.process.start( "gradientcolor.docx" ); } } } |
三、添加圖片背景
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using system.drawing; using spire.doc; namespace imagebackground { class program { static void main( string [] args) { //創建一個document類實例,并加載word文檔 document document = new document(); document.loadfromfile( @"c:\users\administrator\desktop\test.docx" ); //設置文檔的背景填充模式為圖片填充 document.background.type = spire.doc.documents.backgroundtype.picture; //設置背景圖片 document.background.picture = image.fromfile( @"c:\users\administrator\desktop\1.jpg" ); //保存并打開文檔 document.savetofile( "imagebackground.docx" , fileformat.docx2013); system.diagnostics.process.start( "imagebackground.docx" ); } } } |
總結
以上所述是小編給大家介紹的c#設置word文檔背景的三種方法(純色/漸變/圖片背景),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/Yesi/archive/2018/03/12/8549916.html