当前位置: 移动技术网 > IT编程>开发语言>c# > C#设置Word文档背景的三种方法(纯色/渐变/图片背景)

C#设置Word文档背景的三种方法(纯色/渐变/图片背景)

2019年07月18日  | 移动技术网IT编程  | 我要评论

word是我们日常生活、学习和工作中必不可少的文档处理工具。精致美观的文档能给人带来阅读时视觉上的美感。在本篇文章中,将介绍如何使用组件free spire.doc for .net(社区版)给word设置文档背景。下面的示例中,给word添加背景分为三种情况来讲述,即添加纯色背景,渐变色背景和图片背景。

工具使用:下载安装控件free spire.doc后,在项目程序中添加spire.doc.dll即可(该dll可在安装文件下bin文件夹中获取)

一、添加纯色背景

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");
  }
 }
}

调试运行程序后,生成文档

二、添加渐变色背景

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");
  }
 }
}

 三、添加图片背景

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");
  }
 }
}

总结

以上所述是小编给大家介绍的c#设置word文档背景的三种方法(纯色/渐变/图片背景),希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网