C#設(shè)置Word文檔背景的三種方法(純色/漸變/圖片背景)
Word是我們?nèi)粘I?、學(xué)習(xí)和工作中必不可少的文檔處理工具。精致美觀的文檔能給人帶來(lái)閱讀時(shí)視覺上的美感。在本篇文章中,將介紹如何使用組件Free Spire.Doc for .NET(社區(qū)版)給Word設(shè)置文檔背景。下面的示例中,給Word添加背景分為三種情況來(lái)講述,即添加純色背景,漸變色背景和圖片背景。
工具使用:下載安裝控件Free Spire.Doc后,在項(xiàng)目程序中添加Spire.Doc.dll即可(該dll可在安裝文件下Bin文件夾中獲?。?/p>
一、添加純色背景
using Spire.Doc;
using System.Drawing;
namespace AddBackground
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè)Document類對(duì)象,并加載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)試運(yùn)行程序后,生成文檔

二、添加漸變色背景
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;
namespace AddGradientBackground
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建Document類實(shí)例,并加載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)建一個(gè)Document類實(shí)例,并加載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文檔背景的三種方法(純色/漸變/圖片背景),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#程序中session值的保存方法以及轉(zhuǎn)為字符串的方法總結(jié)
這篇文章主要介紹了C#程序中session值的保存方法以及轉(zhuǎn)為字符串的方法總結(jié),經(jīng)常被用于ASP.NET網(wǎng)絡(luò)編程項(xiàng)目中,需要的朋友可以參考下2016-04-04
C#備忘錄模式(Memento Pattern)實(shí)例教程
這篇文章主要介紹了C#備忘錄模式(Memento Pattern),以一個(gè)支持回退操作的例子講述了C#備忘模式的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-09-09
C#實(shí)現(xiàn)從多列的DataTable里取需要的幾列
這篇文章主要介紹了C#實(shí)現(xiàn)從多列的DataTable里取需要的幾列,涉及C#針對(duì)DataTable操作的相關(guān)技巧,需要的朋友可以參考下2016-03-03
C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在網(wǎng)頁(yè)中根據(jù)url截圖并輸出到網(wǎng)頁(yè)的方法,涉及C#網(wǎng)頁(yè)瀏覽器及圖片操作的相關(guān)技巧,需要的朋友可以參考下2016-01-01
基于WPF實(shí)現(xiàn)3D畫廊動(dòng)畫效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)簡(jiǎn)單的3D畫廊動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02

