-
C# 设置、删除、读取Word文档背景——基于Spire.Cloud.SDK for .NET
Spire.Cloud. SDK for .NET提供了接口SetBackgroudColor()、SetBackgroudImage()、DeleteBackground()、GetBackgroudColor()用于设置、删除及读取Word文档背景。本文将以C#程序为例演示如何来调用API接口实现以上内容操作。
必要步骤:
步骤一:dll文件获取及导入。在程序中通过Nuget搜索安装,直接导入。
dll添加引用效果:
步骤二: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(255, 255, 205); //设置文档密码,如果没有密码,则设置为null string password = null; //使用冰蓝云配置的2G空间存贮文档,可设置为null string storage = null; //设置生成文档的路径及文档名称 string destFilePath = "output/BackgroundColor.docx"; //调用方法设置背景颜色 backgroundApi.SetBackgroundColor(name,color,destFilePath,folder,storage,password); } } }
背景颜色设置结果:
【示例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,destFilePath,password,folder,storage); } } }
文档背景删除效果:
【示例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.GetBackgroundColor(name,password,folder,storage)); System.Console.ReadLine(); } } }
背景色RGB值读取结果:
(本文完)
原文链接:https://www.cnblogs.com/Yesi/p/12034407.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数