Free Spire.Doc for .NET(社区版)
Visual Studio
【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'); } }}
下载安装控件Free Spire.Doc后,在项目程序中添加Spire.Doc.dll即可(该dll可在安装文件下Bin文件夹中获取)