【发布时间】:2020-06-04 20:35:32
【问题描述】:
目前,我的代码成功地将 word 文档 (.docx) 中的所有文本转换为 .txt 文件中的纯文本,但无论 word doc 中有图像,它都会在我的输出文件中被替换为“/”。如何忽略图像?
我的代码存根:
Word.Application app = new Word.Application();
Word.Document doc;
object missing = Type.Missing;
object readOnly = true;
doc = app.Documents.Open(ref path, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
string text = doc.Content.Text;
System.IO.File.WriteAllText(txtPath, text);
Console.WriteLine("File converted to .txt!");
【问题讨论】:
-
嗯,我不确定 Content 对象是否有能力做到这一点。读出文本时可能只需要替换这些字符。字符串文本 = doc.Content.Text.Replace("'/'","");或者您是否明确需要忽略最初从内容读取的图像?
-
如果我使用替换方法,我可能会丢失原始 word 文件中的任何“/”字符,所以我认为我最好的选择是在初始读取时忽略...除非有更好的方法。
-
啊,好吧,我正在阅读,因为图像被替换“包括”正斜杠周围的单引号,我认为这种情况很少见。
-
这些
ref missing在你的代码中做了什么?
标签: c# office-interop docx