惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
S
Secure Thoughts
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
G
GRAHAM CLULEY
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
Martin Fowler
Martin Fowler
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
T
Threatpost
S
Securelist
T
Tenable Blog
博客园_首页
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
美团技术团队
Project Zero
Project Zero
The Cloudflare Blog
L
Lohrmann on Cybersecurity
The Register - Security
The Register - Security
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
有赞技术团队
有赞技术团队
IT之家
IT之家
A
Arctic Wolf
Scott Helme
Scott Helme
Latest news
Latest news
T
Tailwind CSS Blog
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
Cyberwarzone
Cyberwarzone
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
S
Schneier on Security
Y
Y Combinator Blog

博客园 - 会长

本地部署DeepSeek并用Python调用 一个用来将数字转换为英文的MySql函数 Snowflake算法生成Id 用C语言实现ElGamal算法 如何用C#代码验证XML文件是否符合DTD规范 如何调试WebBrowser控件中的JS代码 《水浒传》中的物价 诗词记录 对开发流程优化的建议 如何在HP ProLiant-DL580 Gen9 上安装ESXi NumPy基础 闪存客户端 北京某软件园小公园 MySQL优化技巧 大叔学ML第五:逻辑回归 大叔学ML第四:线性回归正则化 大叔学ML第三:多项式回归 大叔学ML第二:线性回归 大叔学ML第一:梯度下降
解决Devexpress的RichEditControl控件保存为docx文件后在word里打开字体显示不正确的问题
会长 · 2021-12-17 · via 博客园 - 会长

问题复现

用Richeditcontrol编辑如下内容并保存为.docx文件:

image

用word或wps打开的效果:

image

寻找原因

我用word编辑个一样内容的文件,将这两个文件的扩展名修改为zip,解压后经过对比,发现document.xml文件这里不同:

image

我用bing搜索了这个关键字,搜索到了相关文档,奈何和谐社会。

解决办法

增加一个“保存“按钮,保存为docx文件后,把其中的document.xml文件修改之。代码如下:

using DevExpress.XtraRichEdit;
using Ionic.Zip;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

namespace RicheditcontrolFontDemo
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void barButtonItemSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog
            {
                Filter = "doc|*.docx"
            };
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                this.richEditControl.SaveDocument(sfd.FileName, DocumentFormat.OpenXml);
                MondifyFonts(sfd.FileName);
            }
        }

        private void MondifyFonts(string path)
        {
            using (ZipFile zip = new ZipFile(path)) // DotNetZip
            {
                var entry = zip.First(e => e.FileName == "word/document.xml");

                string tempPath = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                entry.Extract(tempPath);
                AddEastAsiaFont($"{tempPath}//word/document.xml");
                zip.RemoveEntry(entry);
                zip.AddFile($"{tempPath}//word/document.xml", "word");
                zip.Save();

                Directory.Delete(tempPath, true);
            }
        }

        private void AddEastAsiaFont(string file)
        {
            XmlDocument doc = new XmlDocument();
            XmlNamespaceManager xmlm = new XmlNamespaceManager(doc.NameTable);
            xmlm.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            doc.Load(file);
            var nodes = doc.SelectNodes("//w:rFonts", xmlm);

            foreach (XmlNode node in nodes)
            {
                if (!AnyEastAsiaAttribute(node))
                {
                    string fontName = GetFontName(node);
                    if (fontName != null)
                    {
                        XmlAttribute atr = doc.CreateAttribute("w", "eastAsia", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                        atr.Value = fontName;
                        node.Attributes.Append(atr);
                    }
                }
            }

            doc.Save(file);
        }

        private string GetFontName(XmlNode node)
        {
            foreach (XmlAttribute item in node.Attributes)
            {
                if (item.Name == "w:ascii" || item.Name == "w:hAnsi")
                {
                    return item.Value;
                }
            }
            return null;
        }

        private bool AnyEastAsiaAttribute(XmlNode node)
        {
            foreach (XmlAttribute item in node.Attributes)
            {
                if (item.Name == "w:eastAsia")
                {
                    return true;
                }
            }

            return false;
        }
    }
}