码迷,mamicode.com
首页 > Windows程序 > 详细

C#操作Word文档—— 如何设置Word文档背景

时间:2018-03-12 17:08:40      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:C#   .NET Word API   背景色   免费类库   .NET   

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等常用办公文档的操作技巧,欢迎继续关注博客,希望和大家分享更多的技术知识。

C#操作Word文档—— 如何设置Word文档背景

标签:C#   .NET Word API   背景色   免费类库   .NET   

原文地址:http://blog.51cto.com/eiceblue/2085566

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!