C# 设置、删除、读取Word文档背景——基于Spire.Cloud.Word
Spire.Cloud.Word.Sdk提供了接口SetBackgroudColor()、SetBackgroudImage()、DeleteBackground()、GetBackgroudColor()用于设置、删除及读取Word文档背景。本文将以C#程序为例演示如何来调用API接口实现以上内容操作。
必要步骤:
步骤一:dll文件获取及导入。通过官网下载SDK文件包。

下载后,解压文件,将Spire.Cloud.Word.Sdk.dll文件及其他三个dll添加引用至VS程序(如下图);或者在程序中通过Nuget搜索安装,直接导入。
步骤二:App ID及Key获取。在云端创建账号,并在“我的应用”板块中创建应用以获得App ID及App Key。

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

【示例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(, , ); //设置文档密码,如果没有密码,则设置为null
string password = null; //使用冰蓝云配置的2G空间存贮文档,可设置为null
string storage = null; //设置生成文档的路径及文档名称
string destFilePath = "output/BackgroundColor.docx"; //调用方法设置背景颜色
backgroundApi.SetBackgroudColor(name,color, folder, storage, password, destFilePath);
}
}
}
背景颜色设置结果:

【示例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);
}
}
}
背景图片设置效果:

【示例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);
}
}
}
文档背景删除效果:

【示例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();
}
}
}
背景色RGB值读取结果:

(本文完)
C# 设置、删除、读取Word文档背景——基于Spire.Cloud.Word的更多相关文章
- C# 添加文本、图片到PDF文档(基于Spire.Cloud.PDF.SDK)
Spire.Cloud.PDF.SDK提供了接口PdfTextApi及PdfImagesApi用于添加文本和图片到PDF文档,添加文本时,可格式化文本样式,包括文本字体类型.字号.字体样式.文本颜色. ...
- C# 加密、解密PDF文档(基于Spire.Cloud.SDK for .NET)
Spire.Cloud.SDK for .NET提供了接口PdfSecurityApi可用于加密.解密PDF文档.本文将通过C#代码演示具体加密及解密方法. 使用工具: Spire.Cloud.SDK ...
- Java 设置、删除、获取Word文档背景(基于Spire.Cloud.SDK for Java)
本文介绍使用Spire.Cloud.SDK for Java 提供的BackgroundApi接口来操作Word文档背景的方法,可设置背景,包括设置颜色背景setBackgroundColor().图 ...
- C# 设置Word文档背景(纯色/渐变/图片背景)
Word是我们日常生活.学习和工作中必不可少的文档处理工具.精致美观的文档能给人带来阅读时视觉上的美感.在本篇文章中,将介绍如何使用组件Free Spire.Doc for .NET(社区版)给Wor ...
- ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)
ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF) 上述博文里提到的在线浏览pdf的方案不错,但word转pdf的那个dll只支持doc不支持docx,附上最新的下载链 ...
- C# 添加、删除、读取Word形状(基于Spire.Cloud.Word.SDK)
本文介绍调用Spire.Cloud.Word.SDK提供的接口shapesApi来操作Word形状,包括添加形状AddShape(),添加形状时,可设置形状类型.颜色.大小.位置.倾斜.轮廓.文本环绕 ...
- asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用
最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...
- C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)
本文介绍通过调用Spire.Cloud.Word.SDK提供的ConvertApi接口将Word转换为PDF.XPS.Epub.RTF以及将Docx转为Doc格式等.调用接口方法及步骤参考以下步骤: ...
- 打开word文档时提示“Microsoft Office Word已停止工作”
我的电脑(Win10)有Office 2003和2013两个版本,可能由于之前超长待机等原因导致word 2003的文件(.doc)不能正常打开,没次都会提示“Microsoft Office Wor ...
随机推荐
- JAVA Rest High Level Client如何取聚合后得数据
对于刚刚学习es的童鞋来说,很容易不清楚怎么获取客户端对es文档的聚合结果,下面就演示一下模仿DSL写聚合,然后获取到聚合对结果. 一, 对于下面这个简单的聚合,目的是对于文档全文匹配,聚合颜色字段. ...
- 「动态规划」-数位dp专题
数位dp,今天学长讲的稍玄学,课下花了一会时间仔细看了一下,发现板子是挺好理解的,就在这里写一些: 数位dp主要就是搞一些在区间中,区间内的数满足题目中的条件的数的个数的一类题,题目一般都好理解,这时 ...
- haproxy+keepalived练习
小的网站结构 说明:如果部署在云上,比如阿里云上,不需要自己部署keepalived,直接买阿里云的slb即可,slb然后分发流量到两台haproxy机器 一.先部署两个web服务器 编译安装ngin ...
- JavaScript 运行原理
i{margin-right:4px;margin-top:-0.2em}.like_comment_tips .weui-icon-success{background:transparent ur ...
- 除了获取 MAC 地址还能干啥
以前写过一篇<在Web中获取MAC地址>的文章,文章的地址是:https://www.cnblogs.com/tosser/p/9022187.html,我当时使用 OCX ...
- NetCore基于EasyNetQ的高级API使用RabbitMq
一.消息队列 消息队列作为分布式系统中的重要组件,常用的有MSMQ,RabbitMq,Kafa,ActiveMQ,RocketMQ.至于各种消息队列的优缺点比较,在这里就不做扩展了,网上资源很多. 更 ...
- 【Java】抽象类和接口详解
抽象类 一.抽象类的概述 现在请思考一个问题:假如我现在又Dog.Cat.Pig等实例对象,现在我们把它们都抽象成一个Animal类,这个类应该包含了这些Dog.Cat.Pig等实例对象eat的功能, ...
- bat脚本知识总结
1常用基本命令 1.1 @ 它的作用是让执行窗口中不显示它后面这一行的命令本身 1.2 echo 它其实是一个开关命令,就是说它只有两种状态:打开和关闭.于是就有了echo on 和echo off两 ...
- java的Io流机制的学习
IO流机制 File类的使用 File类的构造方法 File(URI uri) File(String pathname) File(File parent, String child) File(S ...
- think PHP 查询、更改
最近公司没有什么新项目,故准备搞搞PHP,正好后端有一些小东西需要搞一下,我就来试试吧. PHP 基于think PHP 3 实现功能: 1.为销售绑定虚拟号码分组(查询可以绑定的分组 -> 绑 ...