一个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根线加回去。

cs 复制代码
        public static void AddSectionBorders(string filePath)
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))
            {
                MainDocumentPart mainPart = doc.MainDocumentPart;
                Body body = mainPart.Document.Body;

                // 获取第一个SectionProperties
                SectionProperties 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();
            }
        }

总结

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

相关推荐
PfCoder15 小时前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform
一只自律的鸡17 小时前
【Linux驱动】bug处理 ens33找不到IP
linux·运维·bug
人工智能AI技术1 天前
【C#程序员入门AI】本地大模型落地:用Ollama+C#在本地运行Llama 3/Phi-3,无需云端
人工智能·c#
MyBFuture1 天前
C#数组详解:一维二维与交错数组
开发语言·windows·c#·visual studio·vision pro
有来技术1 天前
ASP.NET Core 权限管理系统(RBAC)设计与实现|vue3-element-admin .NET 后端
vue.js·后端·c#·asp.net·.net
张人玉1 天前
C#WinFrom中show和ShowDialog的区别
开发语言·microsoft·c#
m0_748233171 天前
C#:微软的现代编程利器
开发语言·microsoft·c#
Traced back1 天前
SQL Server数据自动清理系统最终版(C# WinForms完整源码)
数据库·c#·.net
人工智能AI技术1 天前
【C#程序员入门AI】Microsoft Extensions for AI (MEAI):统一LLM调用接口,告别厂商绑定
人工智能·c#