-
C#教程之C# 添加、删除、读取Word形状(基于Spire.Cloud.Word.SDK)
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
本文介绍调用Spire.Cloud.Word.SDK提供的接口shapesApi来操作Word形状,包括添加形状AddShape(),添加形状时,可设置形状类型、颜色、大小、位置、倾斜、轮廓、文本环绕方式、顺序);删除形状DeleteShape()和读取形状属性GetShapeProperties()等。调用接口方法及步骤参考以下步骤:
步骤一:dll文件获取及导入。通过官网本地下载SDK文件包。(须在e-iceblue中国官网在线编辑板块中注册账号并登录)
下载后,解压文件,将Spire.Cloud.Word.Sdk.dll文件及其他三个dll添加引用至VS程序;或者在程序中通过Nuget搜索下载,直接导入所有dll。dll引用结果如下图所示:
步骤二:App ID及Key获取。在“我的应用”板块中创建应用以获得App ID及App Key。
步骤三:源文档上传。在“文档管理”板块,上传源文档。这里可以建文件夹,将文档存放在文件夹下。不建文件夹时,源文档及结果文档直接保存在根目录。本文示例中,建了两个文件夹,分别用于存放源文档及结果文档。(云平台提供免费1 万次调用次数和 2G 文档内存)
C# 示例代码
1. 添加形状到Word
using System;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Model;
namespace AddShape
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static void Main(string[] args)
{
//配置AppID和AppKey
Configuration wordConfiguration = new Configuration(appId, appKey);
//实例化ShapesApi类
ShapesApi shapesApi = new ShapesApi(wordConfiguration);
string name = "test.docx";//源文档
string paragraphPath = "sections/0/paragraphs/0";//段落路径
int indexInParagraph = 1;//添加形状的段落
string folder = "input";//源文档所在文件夹
string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null
string password = null;//源文档密码
//设置形状属性(包括形状类型、位置、填充颜色、旋转方向、边框宽度/颜色、文本环绕类型/方式
ShapeFormat shapeProperties = new ShapeFormat(50, 50, ShapeFormat.ShapeTypeEnum.Star)
{
HorizontalOrigin = ShapeFormat.HorizontalOriginEnum.Page,
VerticalOrigin = ShapeFormat.VerticalOriginEnum.Page,
VerticalPosition = 40,
HorizontalPosition = 230,
FillColor = new Color(255, 69, 0),
Rotation = 45,
StrokeWeight = 2,
StrokeColor = new Color(255, 255, 0),
TextWrappingType = ShapeFormat.TextWrappingTypeEnum.Both,
TextWrappingStyle = ShapeFormat.TextWrappingStyleEnum.InFrontOfText,
ZOrder = 1
};
string destFilePath = "output/AddShape.docx";//结果文档路径
//调用方法添加形状
shapesApi.AddShape(name, paragraphPath, shapeProperties, folder, storage, indexInParagraph, password, destFilePath);
}
}
}
形状添加效果:
2. 删除Word中的形状
using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace DeleteShape
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static void Main(string[] args)
{
//配置AppID和AppKey
Configuration wordConfiguration = new Configuration(appId, appKey);
//实例化ShapesApi类
ShapesApi shapesApi = new ShapesApi(wordConfiguration);
string name = "AddShape.docx";//源文档
string paragraphPath = "sections/0/paragraphs/0";//段落路径
int index = 0;//要删除形状的索引
string folder = "output";//源文档所在文件夹
string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null
string password = null;//源文档密码
string destFilePath = "output/DeleteShape.docx";//结果文档路径
//调用方法删除形状
shapesApi.DeleteShape(name, paragraphPath, index, folder, storage, password, destFilePath);
}
}
}
形状删除效果:
3. 读取Word形状属性
using System;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Api;
namespace GetShapeProperties
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static void Main(string[] args)
{
//配置AppID和AppKey
Configuration wordConfiguration = new Configuration(appId, appKey);
//实例化ShapesApi类
ShapesApi shapesApi = new ShapesApi(wordConfiguration);
string name = "AddShape.docx";//源文档
string paragraphPath = "sections/0/paragraphs/0";
int index = 0;//读取的形状索引
string folder = "output";//源文档所在文件夹
string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null
string password = null;//源文档密码
//读取属性
System.Console.WriteLine(shapesApi.GetShapeProperties(name, paragraphPath, index, folder, storage, password));
System.Console.ReadLine();
}
}
}
属性读取结果:
栏目列表
最新更新
Winform中怎样跨窗体获取另一窗体的控件对
Winform中使用FastReport的PictureObject时通过代
三分钟掌握,使用Quqrtz.Net实现定时发送邮
NET/Regex 处理连续空格
QR 码详解(下)
C#中的等值判断1
C#编写了一个基于Lucene.Net的搜索引擎查询
使用FastReport报表工具生成报表PDF文档
基于JieBaNet+Lucene.Net实现全文搜索
C#取视频某一帧图片
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
Dubbo(五):深入理解Dubbo核心模型Invok
vfp教程之VFP与Excel交互编程
vfp教程之在VFP中实现跟变式组合框及椭圆
SQL SERVER查询数据库所有表的大小,按照记
使用 SQL 服务器时,"评估期已过期"错
sql server无法连接本地服务器
使用sql语句创建表
VB操作Access数据库小记 ————————
access数据库远程连接
java web操作Access数据库