.net使用Aspose.Words进行Word替换操作的实现代码
时间:2023-03-23 21:28:59
之前在工作中,需要实现Word打印功能,并且插入图片。当时采取的方式则是使用书签进行操作。首先在word内插入书签,完成后,存为模板。程序加载该模板,找到书签,并在指定位置写入文字即可。
后期维护过程中,发现模板经常需要变更,但是书签在word中不方便查看,用户在编辑word的时候容易出错。于是想采取特殊字符串标识的方式进行替换。此时,图片的插入就存在问题,光标无法直接移动到指定字符串。
资源下载:
源代码 https://www.jb51.net/codes/84240.html
开发思路:
查阅 Aspose.Words提供的API,发现有Range类有该方法:
public int Replace(Regex pattern, IReplacingCallback handler, bool isForward);
该方法则是在使用正则表达式进行文档内替换的同时可以执行IReplacingCallback接口。
具体实现代码如下:
/* ==============================================================================
* 文 件 名:Program
* 功能描述:
* Copyright (c) 2013 武汉经纬视通科技有限公司
* 创 建 人: alone
* 创建时间: 2013/4/2 11:16:19
* 修 改 人:
* 修改时间:
* 修改描述:
* 版 本: v1.0.0.0
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Aspose.Words;
namespace WordDemo
{
class Program
{
static void Main(string[] args)
{
var dic = new Dictionary<string, string>();
dic.Add("姓名", "杨幂");
dic.Add("学历", "本科");
dic.Add("联系方式", "02759597666");
dic.Add("邮箱", "304885433@qq.com");
dic.Add("头像", ".//1.jpg");
//使用书签操作
Document doc = new Document(".//1.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (var key in dic.Keys)
{
builder.MoveToBookmark(key);
if (key != "头像")
{
builder.Write(dic[key]);
}
else
{
builder.InsertImage(dic[key]);
}
}
doc.Save("书签操作.doc");//也可以保存为1.doc 兼容03-07
Console.WriteLine("已经完成书签操作");
//使用特殊字符串替换
doc = new Document(".//2.doc");
foreach (var key in dic.Keys)
{
if (key != "头像")
{
var repStr = string.Format("&{0}&", key);
doc.Range.Replace(repStr, dic[key], false, false);
}
else
{
Regex reg = new Regex("&头像&");
doc.Range.Replace(reg, new ReplaceAndInsertImage(".//1.jpg"), false);
}
}
doc.Save("字符串替换操作.doc");//也可以保存为1.doc 兼容03-07
Console.WriteLine("已经完成特殊字符串替换操作");
Console.ReadKey();
}
}
public class ReplaceAndInsertImage : IReplacingCallback
{
/// <summary>
/// 需要插入的图片路径
/// </summary>
public string url { get; set; }
public ReplaceAndInsertImage(string url)
{
this.url = url;
}
public ReplaceAction Replacing(ReplacingArgs e)
{
//获取当前节点
var node = e.MatchNode;
//获取当前文档
Document doc = node.Document as Document;
DocumentBuilder builder = new DocumentBuilder(doc);
//将光标移动到指定节点
builder.MoveTo(node);
//插入图片
builder.InsertImage(url);
return ReplaceAction.Replace;
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现拍照、录像、录音代码范例
C#中使用资源的方法分析
Lombok使用@Tolerate实现冲突兼容问题
![](https://img.aspxhome.com/file/2023/4/91104_0s.png)
Spring定时任务无故停止又不报错的解决
C#学习笔记整理_深入剖析构造函数、析构函数
JAVA IDEA 打开assert 设置方式
![](https://img.aspxhome.com/file/2023/7/77137_0s.png)
C#实现Xml序列化与反序列化的方法
C#绘制时钟的方法
![](https://img.aspxhome.com/file/2023/0/91240_0s.jpg)
Swift洗牌动画效果的实现方法
java模拟实现斗地主发牌小程序
SpringBoot实现多个子域共享cookie的示例
![](https://img.aspxhome.com/file/2023/8/77938_0s.jpg)
Spring Boot应用监控的实战教程
![](https://img.aspxhome.com/file/2023/5/83595_0s.png)
idea前后跳转箭头的快捷键
![](https://img.aspxhome.com/file/2023/8/131998_0s.jpg)
Java使用Graphics2D绘制SVG和PNG的方法
![](https://img.aspxhome.com/file/2023/9/118829_0s.webp)
android studio生成aar包并在其他工程引用aar包的方法
![](https://img.aspxhome.com/file/2023/2/130302_0s.jpg)
使用log4j MDC实现日志追踪
Java设计模式之享元模式实例详解
C#调用存储过程详解(带返回值、参数输入输出等)
实现分布式WebSocket集群的方法
![](https://img.aspxhome.com/file/2023/3/82433_0s.png)
Java反射(Class类,Class对象获取)
![](https://img.aspxhome.com/file/2023/4/63444_0s.png)