C# 设置Word文档背景(纯色/渐变/图片背景)

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");
        }
    }
}
复制代码

以上全部内容为三种添加Word文档背景的方法,如果喜欢本文,欢迎转载(转载请注明出处)。

感谢浏览!

 

posted @   E-iceblue  阅读(1571)  评论(1编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示