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)文章
總結(jié)C#網(wǎng)絡(luò)編程中對于Cookie的設(shè)定要點
這篇文章主要介紹了總結(jié)C#網(wǎng)絡(luò)編程中對于Cookie的設(shè)定要點,文中還給出了一個cookie操作實例僅供參照,需要的朋友可以參考下2016-04-04
C#對多個集合和數(shù)組的操作方法(合并,去重,判斷)
下面小編就為大家?guī)硪黄狢#對多個集合和數(shù)組的操作方法(合并,去重,判斷)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12

