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.