ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

解决Devexpress的RichEditControl控件保存为docx文件后在word里打开字体显示不正确的问题

2021-12-17 19:05:31  阅读:246  来源: 互联网

标签:node 控件 docx word zip doc item using


目录

问题复现

用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;
        }
    }
}

标签:node,控件,docx,word,zip,doc,item,using
来源: https://www.cnblogs.com/zzy0471/p/devexpressricheditcontrolfont.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有