温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#怎么设置、删除、读取Word文档背景

发布时间:2021-09-01 14:38:12 来源:亿速云 阅读:161 作者:chen 栏目:编程语言

这篇文章主要讲解了“C#怎么设置、删除、读取Word文档背景”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#怎么设置、删除、读取Word文档背景”吧!

Spire.Cloud.Word.Sdk提供了接口SetBackgroudColor()、SetBackgroudImage()、DeleteBackground()、GetBackgroudColor()用于设置、删除及读取Word文档背景。本文将以C#程序为例演示如何来调用API接口实现以上内容操作。

必要步骤:

步骤一:dll文件获取及导入。通过官网 下载SDK文件包。

C#怎么设置、删除、读取Word文档背景

下载后,解压文件,将Spire.Cloud.Word.Sdk.dll文件及其他三个dll添加引用至VS程序(如下图);或者在程序中通过Nuget搜索安装,直接导入。

C#怎么设置、删除、读取Word文档背景

步骤二:App ID及Key获取。在 云端创建账号,并在“我的应用”板块中创建应用以获得App ID及App Key。

C#怎么设置、删除、读取Word文档背景

步骤三:源文档上传。在“文档管理”板块,上传源文档。这里如果想方便文档管理,可以新建文件夹,将源文档及结果文档分别保存至相应的文件夹下。不建文件夹时,源文档及结果文档直接保存在根目录。本文示例中,建了两个文件夹,分别用于存放源文档及结果文档。

C#怎么设置、删除、读取Word文档背景

【示例1】设置背景颜色

using Spire.Cloud.Word;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Model;
using System;
 
namespace BackgroundColor
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";
        static void Main(string[] args)
        {
            //配置账号信息
            Configuration wordConfiguration = new Configuration(appId, appKey);
 
            //创建BackgroundApi实例
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
            
            //源文档
            var fileName = "testfile.docx";            
            string name = fileName;
 
            //源文档所在文件夹,若没有文件夹则设置为null
            string folder = "input";
 
            //设置背景颜色RGB值
            Color color = new Color(255, 255, 205);
 
            //设置文档密码,如果没有密码,则设置为null
            string password = null;
 
            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;
 
            //设置生成文档的路径及文档名称
            string destFilePath = "output/BackgroundColor.docx";
 
            //调用方法设置背景颜色
            backgroundApi.SetBackgroudColor(name,color, folder, storage, password, destFilePath);            
        }
    }
}

背景颜色设置结果:

C#怎么设置、删除、读取Word文档背景

【示例2】设置背景图片

using Spire.Cloud.Word.Sdk;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System;
 
 
namespace BackgroundImg
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";
        static void Main(string[] args)
        {
            //配置账号信息
            Configuration wordConfiguration = new Configuration(appId, appKey);
 
            //创建BackgroundApi实例
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
 
            //源文档及图片
            var fileName = "testfile.docx";
            var imageName = "ss.png";
            string name = fileName;
 
            //源文档所在文件夹,若没有文件夹则设置为null
            string folder = "input";
            string imagePath = "input" + "/"+ imageName;
 
            //设置文档密码,如果没有密码,则设置为null
            string password = null;
 
            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;
 
            //设置生成文档的路径及文档名称
            string destFilePath = "output/BackgroundImg.docx";
 
            //调用方法设置背景
            backgroundApi.SetBackgroudImage(name, imagePath, folder, storage, password, destFilePath);
        }
    }
}

背景图片设置效果:

C#怎么设置、删除、读取Word文档背景

【示例3】删除背景(包括背景颜色及背景图片)

using Spire.Cloud.Word.Sdk;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System;
 
namespace DeleteBackground
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";
        static void Main(string[] args)
        {
            //配置账号信息
            Configuration wordConfiguration = new Configuration(appId, appKey);
 
            //创建BackgroundApi实例
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
 
            //源文档
            var fileName = "BackgroundImg.docx";
            string name = fileName;
 
            //源文档所在文件夹,若没有文件夹则设置为null
            string folder = "output";
 
            //设置文档密码,如果没有密码,则设置为null
            string password = null;
 
            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;
 
            //设置生成文档的路径及文档名称
            string destFilePath = "output/DeleteBackground.docx";
 
            //调用方法删除文档中背景
            backgroundApi.DeleteBackground(name, password, folder, storage, destFilePath);
        }
    }
}

文档背景删除效果:

C#怎么设置、删除、读取Word文档背景

【示例4】读取背景颜色

using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Model;
using System;
 
namespace GetBackground
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";
        static void Main(string[] args)
        {
            //配置账号信息
            Configuration wordConfiguration = new Configuration(appId, appKey);
 
            //创建BackgroundApi实例
            BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration);
 
            //源文档
            var fileName = "BackgroundColor.docx";         
            string name = fileName;            
 
            //源文档密码,若无密码可设置为null
            string password = null;
 
            //源文档所在文件夹,若没有文件夹则设置为null
            string folder = "output";
 
            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;
            
            //获取文档背景色
            System.Console.WriteLine(backgroundApi.GetBackgroudColor(name, password, folder, storage));
            System.Console.ReadLine();       
        }
    }
}

背景色读取结果:

C#怎么设置、删除、读取Word文档背景

感谢各位的阅读,以上就是“C#怎么设置、删除、读取Word文档背景”的内容了,经过本文的学习后,相信大家对C#怎么设置、删除、读取Word文档背景这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI