在 C# 中使用 Interop 将 word 文档转换为文本时忽略图像
Ignore images when converting word document to text using Interop in C#
目前,我的代码成功地将 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!");
在访问内容之前删除所有图像怎么样?
像这样:
while (doc.InlineShapes.Count > 0)
{
doc.InlineShapes(1).Delete(); //Collection is 1-based, first element is 1, not 0; at least when using it within VBA (weird language...)
}
// and with Shapes as well
while (doc.Shapes.Count > 0)
{
doc.Shapes(1).Delete();
}
string text = doc.Content.Text;
一种不同的方法,而不是我上面建议的方法
只需将文档另存为文本
object path = txtPath;
const int wdFormatText = 2;
object fileFormat = wdFormatText;
doc.SaveAs (ref path, ref fileFormat, ref missing, ...) // other missing parameter
还有一个方法 SaveAs2
,如果您继续传递 missing
以获得更多参数,我认为它是相同的
这是我的解决方案。 class 读取 Word 文档,删除所有图像,然后将其转换为 RTF 文件。
using Microsoft.Office.Interop.Word;
using System.IO;
using OW = Microsoft.Office.Interop.Word;
namespace WordImagesCruncher
{
public class WordImagesCruncher
{
public string SourceFilePath { private set; get; }
public WordImagesCruncher(string sourceFilePath)
{
SourceFilePath = sourceFilePath;
}
public void DoWork()
{
var wordApp = new OW.Application();
OW.Document doc = wordApp.Documents.Open(SourceFilePath);
for (int i = doc.InlineShapes.Count; i > 0; i--)
{
doc.InlineShapes[1].Delete();
}
for (int i = doc.Shapes.Count; i >0 ; i--)
{
doc.Shapes[1].Delete();
}
doc.SaveAs(Path.GetFileNameWithoutExtension(SourceFilePath) + "_tmp.rtf", OW.WdSaveFormat.wdFormatRTF);
doc.Close();
wordApp.Quit();
}
}
}
不要忘记添加互操作参考
并将 属性 Embed Interop Types
更改为 Yes
。
目前,我的代码成功地将 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!");
在访问内容之前删除所有图像怎么样?
像这样:
while (doc.InlineShapes.Count > 0)
{
doc.InlineShapes(1).Delete(); //Collection is 1-based, first element is 1, not 0; at least when using it within VBA (weird language...)
}
// and with Shapes as well
while (doc.Shapes.Count > 0)
{
doc.Shapes(1).Delete();
}
string text = doc.Content.Text;
一种不同的方法,而不是我上面建议的方法
只需将文档另存为文本
object path = txtPath;
const int wdFormatText = 2;
object fileFormat = wdFormatText;
doc.SaveAs (ref path, ref fileFormat, ref missing, ...) // other missing parameter
还有一个方法 SaveAs2
,如果您继续传递 missing
以获得更多参数,我认为它是相同的
这是我的解决方案。 class 读取 Word 文档,删除所有图像,然后将其转换为 RTF 文件。
using Microsoft.Office.Interop.Word;
using System.IO;
using OW = Microsoft.Office.Interop.Word;
namespace WordImagesCruncher
{
public class WordImagesCruncher
{
public string SourceFilePath { private set; get; }
public WordImagesCruncher(string sourceFilePath)
{
SourceFilePath = sourceFilePath;
}
public void DoWork()
{
var wordApp = new OW.Application();
OW.Document doc = wordApp.Documents.Open(SourceFilePath);
for (int i = doc.InlineShapes.Count; i > 0; i--)
{
doc.InlineShapes[1].Delete();
}
for (int i = doc.Shapes.Count; i >0 ; i--)
{
doc.Shapes[1].Delete();
}
doc.SaveAs(Path.GetFileNameWithoutExtension(SourceFilePath) + "_tmp.rtf", OW.WdSaveFormat.wdFormatRTF);
doc.Close();
wordApp.Quit();
}
}
}
不要忘记添加互操作参考
并将 属性 Embed Interop Types
更改为 Yes
。