How can I remove all extraneous spaces from a *.docx file?

妖精的绣舞 提交于 2019-12-25 07:46:26

问题


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."


回答1:


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.



来源:https://stackoverflow.com/questions/20910408/how-can-i-remove-all-extraneous-spaces-from-a-docx-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!