Word是我們日常生活、學習和工作中必不可少的文檔處理工具。精緻美觀的文檔能在閱讀時帶來視覺上的美感。在本篇文章中,將介紹如何使用組件Free Spire.Doc for .NET(社區版)給Word設置文檔背景。下面的示例中,給Word添加背景分爲三種情況來講述,即添加純色背景,漸變色背景和圖片背景。
工具使用:下載安裝控件Free Spire.Doc後,在項目程序中添加Spire.Doc.dll即可(該dll可在安裝文件下Bin文件夾中獲取)
一、添加純色背景
C#
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");
}
}
}
調試運行程序後,生成文檔
二、添加漸變色背景
C#
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");
}
}
}
三、添加圖片背景
C#
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");
}
}
}
以上全部內容爲三種添加Word文檔背景的方法,如果喜歡本文,歡迎轉載(轉載請註明出處)
PS:更多關於Word,PDF,PPT,EXCEL等常用辦公文檔的操作技巧,歡迎繼續關注博客,希望和大家分享更多的技術知識。