如何从* .docx文件中删除所有无关的空格? [英] How can I remove all extraneous spaces from a *.docx file?

查看:432
本文介绍了如何从* .docx文件中删除所有无关的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从.docx文件中删除所有多余的空格。如果有两个以上的情况,为了完成这个手动操作,我需要多次搜索并替换两个空格来获取所有这些空间,而且很难说出我什么时候完成。

解决方案

这段代码使用了docx库,完成了它:

<$ p private void RemoveSuperfluousSpaces(string filename)
{
bool superfluousSpacesFound = true;
using(DocX document = DocX.Load(filename))
{
List< int> multipleSpacesLocs;
while(superfluousSpacesFound)
{
document.ReplaceText(,);
multipleSpacesLocs = document.FindAll();
superfluousSpacesFound = multipleSpacesLocs.Count> 0;
}
document.Save();




$ b $ p $从 here


I want to remove all superfluous spaces from a .docx file. If there are cases where there are more than two, to accomplish this manually I need to do a search-and-replace for two spaces multiple times to get all of them, and it's hard to tell when I'm "finished."

解决方案

This code, using the docx library, accomplishes it:

private void RemoveSuperfluousSpaces(string filename)
{
    bool superfluousSpacesFound = true;
    using (DocX document = DocX.Load(filename))
    {
        List<int> multipleSpacesLocs;
        while (superfluousSpacesFound)
        {
            document.ReplaceText("  ", " ");
            multipleSpacesLocs = document.FindAll("  ");
            superfluousSpacesFound = multipleSpacesLocs.Count > 0;
        }
        document.Save();
    }
}

Download docx from here.

这篇关于如何从* .docx文件中删除所有无关的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆