C#設(shè)置Word文檔背景的三種方法(純色/漸變/圖片背景)
Word是我們?nèi)粘I?、學(xué)習(xí)和工作中必不可少的文檔處理工具。精致美觀的文檔能給人帶來閱讀時視覺上的美感。在本篇文章中,將介紹如何使用組件Free Spire.Doc for .NET(社區(qū)版)給Word設(shè)置文檔背景。下面的示例中,給Word添加背景分為三種情況來講述,即添加純色背景,漸變色背景和圖片背景。
工具使用:下載安裝控件Free Spire.Doc后,在項目程序中添加Spire.Doc.dll即可(該dll可在安裝文件下Bin文件夾中獲?。?/p>
一、添加純色背景
using Spire.Doc; using System.Drawing; namespace AddBackground { class Program { static void Main(string[] args) { //創(chuàng)建一個Document類對象,并加載Word文檔 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx"); //設(shè)置文檔的背景填充模式為顏色填充 document.Background.Type = Spire.Doc.Documents.BackgroundType.Color; //設(shè)置背景顏色 document.Background.Color = Color.MistyRose; //保存并打開文檔 document.SaveToFile("PureBackground.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("PureBackground.docx"); } } }
調(diào)試運行程序后,生成文檔
二、添加漸變色背景
using Spire.Doc; using System.Drawing; using Spire.Doc.Documents; namespace AddGradientBackground { class Program { static void Main(string[] args) { //創(chuàng)建Document類實例,并加載Word文檔 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx"); //設(shè)置文檔的背景填充模式為漸變填充 document.Background.Type = Spire.Doc.Documents.BackgroundType.Gradient; //設(shè)置漸變背景顏色 BackgroundGradient gradient = document.Background.Gradient; gradient.Color1 = Color.LightSkyBlue; gradient.Color2 = Color.PaleGreen; //設(shè)置漸變模式 gradient.ShadingVariant = GradientShadingVariant.ShadingMiddle; gradient.ShadingStyle = GradientShadingStyle.FromCenter; //保存并打開文檔 document.SaveToFile("GradientColor.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("GradientColor.docx"); } } }
三、添加圖片背景
using System.Drawing; using Spire.Doc; namespace ImageBackground { class Program { static void Main(string[] args) { //創(chuàng)建一個Document類實例,并加載Word文檔 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx"); //設(shè)置文檔的背景填充模式為圖片填充 document.Background.Type = Spire.Doc.Documents.BackgroundType.Picture; //設(shè)置背景圖片 document.Background.Picture = Image.FromFile(@"C:\Users\Administrator\Desktop\1.jpg"); //保存并打開文檔 document.SaveToFile("ImageBackground.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("ImageBackground.docx"); } } }
總結(jié)
以上所述是小編給大家介紹的C#設(shè)置Word文檔背景的三種方法(純色/漸變/圖片背景),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
C#程序中session值的保存方法以及轉(zhuǎn)為字符串的方法總結(jié)
這篇文章主要介紹了C#程序中session值的保存方法以及轉(zhuǎn)為字符串的方法總結(jié),經(jīng)常被用于ASP.NET網(wǎng)絡(luò)編程項目中,需要的朋友可以參考下2016-04-04C#實現(xiàn)從多列的DataTable里取需要的幾列
這篇文章主要介紹了C#實現(xiàn)從多列的DataTable里取需要的幾列,涉及C#針對DataTable操作的相關(guān)技巧,需要的朋友可以參考下2016-03-03C#實現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法
這篇文章主要介紹了C#實現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法,涉及C#網(wǎng)頁瀏覽器及圖片操作的相關(guān)技巧,需要的朋友可以參考下2016-01-01