当前位置: 首页 > news >正文

一个DevExpress的Docx文件处理的Bug的解决

事情是这样的

用DevExpress的批量替换Docx文件文件中的内容。就是把  {数据项}  替换成相应的数据。用DevExpress实现这个处理,简单方便。

            string pattern = @"\{.*?\}";
Regex regex = new Regex(pattern);
DocumentRange[] ranges = document.FindAll(regex, document.Range);

            for (int idx = fields.Count - 1; idx >= 0; idx--)
{
if (idx >= ranges.Length)
continue;
DocumentRange range = ranges[idx];
string txt = document.GetText(range);
txt = txt.Substring(1, txt.Length - 2);
range = ranges[idx];

                。。。。。。

            }

DevExpress 支持使用正则表达式进行find。所以特别简单。

直到有一天,客户说替换的Docx有问题,少了2条线。就是下图的左右两边的线。

用DevExpress 处理后,这2条线就会丢失。

查找原因

这2条线是啥 ,俺也不知道。问了一下别人,说是Borders。俺以前也没有见过这种格式Docx。

对于Office的文件,俺之前发过一个博客《使用C#学习Office文件的处理(pptx docx xlsx)》

里面有讲解如何处理这类的问题。

首先运行

然后打开文件,点击Reflect Code

把Docx逆向为C#的代码 

然后搜索PageBorders

找到了核心的代码

            PageBorders pageBorders1 = new PageBorders();
LeftBorder leftBorder35 = new LeftBorder(){ Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)4U };
RightBorder rightBorder35 = new RightBorder(){ Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)4U };

            pageBorders1.Append(leftBorder35);
pageBorders1.Append(rightBorder35); 

如果,对Docx的行号的显示感兴趣的话,这里也有行号的处理

LineNumberType lineNumberType1 = new LineNumberType(){ CountBy = 5 };

解决问题

找出了核心代码,就好办了 。写了下面这个函数。执行这个函数就可以把那2根线加回去。

        public static void AddSectionBorders(string filePath){using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true)){MainDocumentPart mainPart = doc.MainDocumentPart;Body body = mainPart.Document.Body;// 获取第一个SectionPropertiesSectionProperties sectionProps = body.GetFirstChild<SectionProperties>();if (sectionProps == null){sectionProps = new SectionProperties();body.AppendChild(sectionProps);}// 创建页面边框PageBorders pageBorders = new PageBorders(){LeftBorder = new LeftBorder(){Val = BorderValues.Single,Size = 4,Space = 4,Color = "auto"},RightBorder = new RightBorder(){Val = BorderValues.Single,Size = 4,Space = 4,Color = "auto"},TopBorder = new TopBorder(){Val = BorderValues.Nil},BottomBorder = new BottomBorder(){Val = BorderValues.Nil}};// 移除现有的页面边框和边距sectionProps.RemoveAllChildren<PageBorders>();// 添加新的边框和边距设置sectionProps.Append(pageBorders);// 保存更改mainPart.Document.Save();}}

总结

微软给的工具很方便,虽然是十几年前的老工具。

http://www.dtcms.com/a/548719.html

相关文章:

  • Ubuntu(④Mysql)
  • Docker 拉取配置教程:解决镜像拉取连接超时问题
  • 开始改变第六天 MySQL(1)
  • 电脑网站自适应怎么做企业型网站建设怎样收费
  • 阿里培训网站建设杭州网站制作模板
  • Rust 所有权系统:如何为内存安全保驾护航
  • HarmonyOS WindowExtension深度解析:构建跨窗口交互的创新体验
  • PDF导出服务
  • 20251030在AIO-3576Q38开发板的Android14下确认TF卡
  • 《模仿人类皮肤层与环层小体的社交交互机器人皮肤》2024 IEEE/ASME TMECH 论文解读
  • PHP Laravel 10 框架:使用队列处理异步任务(邮件发送 / 数据导出)
  • 拉丝机东莞网站建设怎样做软件开发
  • 苍山网站建设网站设计分享
  • 一个蛇形填充n×n矩阵的算法
  • git index lockFile exists 问题
  • 一段音频/视频分离成人声与伴奏,Windows + Anaconda 快速跑通 Spleeter(离线可用)
  • RecyclerView设置边缘渐变失效
  • 网站续费模版徐州建设工程网上交易平台
  • [系统架构设计师]云原生架构
  • spec-kit深度解析:AI驱动的规范驱动开发(SDD)的架构、哲学与实践启示
  • 怎么学做网站哈尔滨十大广告公司
  • AI应用开发
  • Rust 中精确大小迭代器(ExactSizeIterator)的深度解析与实践
  • 培训/课程预约小程序开发-提升教育机构的管理效率和用户体验
  • 【Unity基础详解】(3)Unity核心类:GameObject
  • oj题 ——— 单链表oj题
  • 企业网站建设推广含义网站建设开发上线流程
  • Unity-AutoHand插件手势跟踪响应研究
  • 数据结构——三十三、Dijkstra算法(王道408)
  • MQTTX:全能的 MQTT 客户端工具简化物联网开发