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 ...
随机推荐
- 最新JetBrains PyCharm 使用教程--安装教程(一)
安装过程 Pycharm 激活码 license server选项里边输入:http://intellij.mandroid.cn/
- 关于Jvm的见解(一)
Jvm组成结构 硬件体系(如Intel体系.spac等)——>操作系统(如Windows.Linux等)——>Java Virtual Machine 所以虚拟机与硬件系统并没有直接的交 ...
- 深入理解 DNS
深入理解 DNS 简介 DNS(Domain Name System)域名系统,它是一个将域名和 IP 地址相互映射的一个分布式数据库,把容易记忆的主机名转换成主机 IP 地址. DNS使用 TCP ...
- Flutter 构建的 Mac 桌面应用上无法发出网络?
在上一篇文章中我们分享了,如何开发桌面应用.在本章文章中,来解决一下为何在 Mac 中无法发出网络情况的原因. 起因 事情起因是这样的:我总觉得写一个 Demo 不足以体现我们开发同学的能力.直到最 ...
- 021.掌握Pod-Pod调度策略
一 Pod生命周期管理 1.1 Pod生命周期 Pod在整个生命周期过程中被系统定义了如下各种状态. 状态值 描述 Pending API Server已经创建该Pod,且Pod内还有一个或多个容器的 ...
- nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)
17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...
- MySQL 备份数据那点事
mysqldump 什么是 mysqldump ? mysqldump 是 MySQL 用于执行逻辑备份的一款工具,可以根据原始数据库对象以及表的定义和数据来生成一系列可以被执行的 SQL 语句. 通 ...
- python描述:链表
单链表结构: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点 ...
- ubuntu 16.04上源码编译glog和gflags 编写glog-config.cmake和gflags-config.cmake | compile glog and glags on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/977f5125/,欢迎阅读! compile glog and glags on ubuntu 16.04 Series comp ...
- [ubuntu篇] 使用Hexo建立个人博客,自定义域名https加密,搜索引擎google,baidu,360收录
为了更好的阅读体验,欢迎阅读原文.原文链接在此. Part 1: Using Github Pages and Hexo to manage personal blogs. Series Part 1 ...