如何用C#移除RTF文档中Arial 12格式的小节编号(保留章节号)
需求与问题
我在Visual Studio 2019 Community中用C#开发文本转语音测试项目,现有一本RTF格式书籍:
- 章节号:Times New Roman 16号字体
- 小节(verse)编号:Arial 12号字体
需要移除所有Arial 12格式的小节编号,保留Times New Roman 16的章节号,避免朗读时读出小节编号。
已尝试但失败的方案
1. 初始Word Interop代码
逻辑错误,仅检查RichTextBox整体文本是否为数字,未定位到指定格式的小节编号:
public string LocateFont(string myBook) { if (myBook.ToString() == "xxxxxx") { if (rtbox1.Text != "") { Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); Document myDoc = wordApp.Documents.Open(rtbox1.Text); Microsoft.Office.Interop.Word.Range range = myDoc.Range(0, myDoc.Content.End); Regex reNum = new Regex(@"^\d+$"); bool isNumeric = reNum.Match(rtbox1.Text).Success; if (isNumeric.Equals(true) & range.Find.Font.Name == "Arial" & range.Font.Size.Equals("12")) { range.Font.Equals(""); } } } return rtbox1.Text.ToString(); }
2. 优化后的Word Interop代码(COM错误)
运行时提示RichTextBox字符串超过255字符,且文档创建方式错误(直接用Documents.Add(rtbox1.Text)会把文本当模板路径而非内容):
private void tsbtnDelFont_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = new Document(); doc.Application.Documents.Add(rtbox1.Text); word.ScreenUpdating = false; Microsoft.Office.Interop.Word.Range range = word.ActiveDocument.Content; var tempVar = range.Find; tempVar.ClearFormatting(); tempVar.Font.Size = 12; tempVar.Font.Name = "Arial"; tempVar.Text = "<[0-9]@>"; tempVar.Replacement.Text = ""; tempVar.MatchWildcards = true; tempVar.Wrap = WdFindWrap.wdFindContinue; tempVar.Execute(Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll); object filename = @"Path to my.rtf"; doc.SaveAs2(ref filename); word.ScreenUpdating = true; }
3. VBA宏(无效果)
通配符匹配逻辑有误,未考虑小节编号的独立边界,无法精准匹配目标内容:
Sub RemoveNumericVerses() Application.ScreenUpdating = False With ActiveDocument.Range.Find .ClearFormatting .Font.Size = 12 .Font.Name = "Arial" .Text = "<[0-9]@>" .Replacement.Text = "" .MatchWildcards = True .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub
另外,用^#通配符会误删章节号,不符合需求。
文档片段示例:
31 他们在那里埋葬了亚伯拉罕和他的妻子撒拉,也埋葬了以撒和他的妻子利百加,我还在那里埋葬了利亚。32 那块田和田间的洞是从赫人子孙手里买来的。”
33 雅各给儿子们吩咐完了,就把脚收在床上,断了气,归到他列祖那里去了。
Chapter 50
1 约瑟伏在他父亲的脸上哭,与他亲嘴。2 约瑟吩咐伺候他的医生用香料薰他父亲,医生就薰了以色列。3 薰尸的日子满了四十天,因为薰尸常例是四十天,埃及人为他哀哭了七十天。
可行解决方案
方案1:修正Word Interop代码(推荐)
核心调整:正确打开RTF文档,优化通配符匹配规则,确保仅匹配独立的Arial 12小节编号:
private void RemoveVerseNumbers() { Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); wordApp.Visible = false; wordApp.ScreenUpdating = false; try { // 注意:rtbox1.Text需为RTF文件的实际路径,而非文本内容 string rtfFilePath = rtbox1.Text; Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(rtfFilePath); Microsoft.Office.Interop.Word.Find findObj = doc.Content.Find; findObj.ClearFormatting(); // 指定目标格式:Arial 12号 findObj.Font.Name = "Arial"; findObj.Font.Size = 12; // 通配符匹配:独立数字+空格,确保匹配小节编号 findObj.Text = "<[0-9]@> "; findObj.Replacement.Text = ""; findObj.MatchWildcards = true; findObj.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue; findObj.Execute(Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll); // 保存修改后的文档 object savePath = @"C:\ModifiedBook.rtf"; doc.SaveAs2(ref savePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF); doc.Close(); } catch (Exception ex) { MessageBox.Show($"操作失败:{ex.Message}"); } finally { // 释放Word应用,避免后台进程残留 wordApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); } }
方案2:修正VBA宏
调整通配符规则,限定编号的边界,避免误匹配章节号:
Sub RemoveVerseNumbers() Application.ScreenUpdating = False With ActiveDocument.Range.Find .ClearFormatting .Font.Size = 12 .Font.Name = "Arial" // 匹配换行或非数字后的数字+空格,保留前缀避免破坏格式 .Text = "(^13|[^0-9])<[0-9]@> " .Replacement.Text = "\1" .MatchWildcards = True .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll // 处理文档开头的编号 .Text = "^13<[0-9]@> " .Replacement.Text = "^13" .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub
关键注意事项
- 字体名称准确性:确认Word中Arial的精确名称(部分版本可能显示为"Arial Unicode MS",需对应调整)
- 通配符规则:
<[0-9]@>匹配连续数字,加上后续空格确保匹配独立的小节编号;用(^13|[^0-9])限定前缀,避免误匹配章节号中的数字 - RichTextBox使用:如果rtbox1用于显示RTF内容,需确保它存储的是RTF文件的实际路径,而非文本内容
内容的提问来源于stack exchange,提问作者CodeMann
相关产品推荐
相关产品推荐

