C#設(shè)置Word文檔背景的三種方法(純色/漸變/圖片背景)
Word是我們?nèi)粘I?、學(xué)習(xí)和工作中必不可少的文檔處理工具。精致美觀(guān)的文檔能給人帶來(lái)閱讀時(shí)視覺(jué)上的美感。在本篇文章中,將介紹如何使用組件Free Spire.Doc for .NET(社區(qū)版)給Word設(shè)置文檔背景。下面的示例中,給Word添加背景分為三種情況來(lái)講述,即添加純色背景,漸變色背景和圖片背景。
工具使用:下載安裝控件Free Spire.Doc后,在項(xiàng)目程序中添加Spire.Doc.dll即可(該dll可在安裝文件下Bin文件夾中獲取)
一、添加純色背景
using Spire.Doc;
using System.Drawing;
namespace AddBackground
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè)Document類(lèi)對(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;
//保存并打開(kāi)文檔
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類(lèi)實(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;
//保存并打開(kāi)文檔
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類(lèi)實(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");
//保存并打開(kāi)文檔
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#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法
這篇文章主要介紹了C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法,以實(shí)例形式較為詳細(xì)的分析了C#窗體間傳值的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
總結(jié)C#網(wǎng)絡(luò)編程中對(duì)于Cookie的設(shè)定要點(diǎn)
這篇文章主要介紹了總結(jié)C#網(wǎng)絡(luò)編程中對(duì)于Cookie的設(shè)定要點(diǎn),文中還給出了一個(gè)cookie操作實(shí)例僅供參照,需要的朋友可以參考下2016-04-04
C#對(duì)多個(gè)集合和數(shù)組的操作方法(合并,去重,判斷)
下面小編就為大家?guī)?lái)一篇C#對(duì)多個(gè)集合和數(shù)組的操作方法(合并,去重,判斷)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
C# 動(dòng)畫(huà)窗體(AnimateWindow)的小例子
C# 動(dòng)畫(huà)窗體(AnimateWindow)的小例子,需要的朋友可以參考一下2013-03-03
unity實(shí)現(xiàn)多點(diǎn)觸控代碼
這篇文章主要介紹了unity實(shí)現(xiàn)多點(diǎn)觸控代碼,我最近在學(xué)習(xí)Unity游戲引擎。先從Unity平面開(kāi)始,本章介紹Unity 平面上的多點(diǎn)觸摸。有需要的小伙伴參考下。2015-03-03

