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的更多相关文章

  1. C# 添加文本、图片到PDF文档(基于Spire.Cloud.PDF.SDK)

    Spire.Cloud.PDF.SDK提供了接口PdfTextApi及PdfImagesApi用于添加文本和图片到PDF文档,添加文本时,可格式化文本样式,包括文本字体类型.字号.字体样式.文本颜色. ...

  2. C# 加密、解密PDF文档(基于Spire.Cloud.SDK for .NET)

    Spire.Cloud.SDK for .NET提供了接口PdfSecurityApi可用于加密.解密PDF文档.本文将通过C#代码演示具体加密及解密方法. 使用工具: Spire.Cloud.SDK ...

  3. Java 设置、删除、获取Word文档背景(基于Spire.Cloud.SDK for Java)

    本文介绍使用Spire.Cloud.SDK for Java 提供的BackgroundApi接口来操作Word文档背景的方法,可设置背景,包括设置颜色背景setBackgroundColor().图 ...

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

    Word是我们日常生活.学习和工作中必不可少的文档处理工具.精致美观的文档能给人带来阅读时视觉上的美感.在本篇文章中,将介绍如何使用组件Free Spire.Doc for .NET(社区版)给Wor ...

  5. ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)

    ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)      上述博文里提到的在线浏览pdf的方案不错,但word转pdf的那个dll只支持doc不支持docx,附上最新的下载链 ...

  6. C# 添加、删除、读取Word形状(基于Spire.Cloud.Word.SDK)

    本文介绍调用Spire.Cloud.Word.SDK提供的接口shapesApi来操作Word形状,包括添加形状AddShape(),添加形状时,可设置形状类型.颜色.大小.位置.倾斜.轮廓.文本环绕 ...

  7. asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用

    最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...

  8. C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)

    本文介绍通过调用Spire.Cloud.Word.SDK提供的ConvertApi接口将Word转换为PDF.XPS.Epub.RTF以及将Docx转为Doc格式等.调用接口方法及步骤参考以下步骤: ...

  9. 打开word文档时提示“Microsoft Office Word已停止工作”

    我的电脑(Win10)有Office 2003和2013两个版本,可能由于之前超长待机等原因导致word 2003的文件(.doc)不能正常打开,没次都会提示“Microsoft Office Wor ...

随机推荐

  1. [网络]HTTP

    HTTP HTTP 简介 HTTP 协议是 Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web)服务器传输超文本到本 ...

  2. css3软键盘不盖住输入框的方法

    css3软键盘不盖住输入框的方法 弹出软键盘的时候 最外面的容器高度就发生了变化 要减去软键盘高度了<pre>var bodyheight bodyheight = $('body').h ...

  3. .NET Core 对龙芯的支持情况和对 .NET Core 开发嵌入式的思考

    目录 .NET Core 对龙芯的支持情况和对 .NET Core 开发嵌入式的思考 一,遗憾的尝试 二,.NET Core在嵌入式下的几点不足 三,.NET Core 龙芯移植的进展和资料 .NET ...

  4. 误删tree命令如何恢复

    误删tree命令如何恢复 考察rpm,yum的用法 一.删除tree命令,tree命令不可用 [root@centos7 ~]# which tree /usr/bin/tree [root@cent ...

  5. 轻松实现C/C++各种常见进制相互转换

    其它进制转为十进制 在实现这个需求之前,先简单介绍一个c标准库中的一个函数: long strtol( const char *str, char **str_end, int base); 参数详细 ...

  6. [javascript] 编写一个计算器,实现加减法

    1.代码 <script> function sum(){ //加法 var value1 = document.getElementById("num1").valu ...

  7. 力扣(LeetCode)删除排序链表中的重复元素 个人题解

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 这题思路比较简单,同样是快慢针的思路. 用一个整数类型val对应最新的只出现过一次的那个值, 如果节点的下一个节点的值和这个对应则不做别 ...

  8. 领扣(LeetCode)检测大写字母 个人题解

    给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...

  9. shell配置文件

    个人配置主要集中在-/.profile文件中 打开新的交互式shell时,配置文件的执行顺序是/etc/profile  /etc/bashrc  ~/.profile   最后是~/.bashrc ...

  10. webapi跨域使用session

    在之前的项目中,我们设置跨域都是直接在web.config中设置的. 这样是可以实现跨域访问的.因为我们这边一般情况下一个webapi会有多个网站.小程序.微信公众号等访问,所以这样设置是没有问题的. ...