Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我能扫描文档并提取某些单词吗?_C#_Ms Word - Fatal编程技术网

C# 我能扫描文档并提取某些单词吗?

C# 我能扫描文档并提取某些单词吗?,c#,ms-word,C#,Ms Word,我有一个word文档,它的标签用“[[]”表示,例如[[sqlscript1]]。我想扫描文档并在文本框中显示sqlscript1。我是否只能阅读包含[[]]的单词?正如millimoose所说,OpenXMLSDK是您所需要的。在动态powerpoint幻灯片的文档生成中,我们就是这样做的。SDK使您能够对相关文档的对象模型进行编程处理,并根据需要对其进行更改/搜索/操作。首先,将其放入内存中。第二,使用查找由双方括号表示的标记(必需的模式:“\[\[(?[^\]]*)\]\]\]”) 您需要

我有一个word文档,它的标签用“[[]”表示,例如[[sqlscript1]]。我想扫描文档并在文本框中显示sqlscript1。我是否只能阅读包含[[]]的单词?

正如millimoose所说,OpenXMLSDK是您所需要的。在动态powerpoint幻灯片的文档生成中,我们就是这样做的。SDK使您能够对相关文档的对象模型进行编程处理,并根据需要对其进行更改/搜索/操作。

首先,将其放入内存中。第二,使用查找由双方括号表示的标记(必需的模式:
“\[\[(?[^\]]*)\]\]\]”

您需要使用Interop DLL从Word文档中提取文本。 看看这个:

然后用如下方式读取文件:

object file = Path.GetDirectoryName(Application.ExecutablePath) + @"\Answer.doc";

Word.Application wordObject = new Word.ApplicationClass();
wordObject.Visible = false;

object nullobject = Missing.Value;
Word.Document docs = wordObject.Documents.Open
    (ref file, ref nullobject, ref nullobject, ref nullobject,
    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
    ref nullobject, ref nullobject, ref nullobject, ref nullobject);

String strLine;
bool bolEOF = false;

docs.Characters[1].Select();

int index = 0;
do
{
    object unit = Word.WdUnits.wdLine;
    object count = 1;
    wordObject.Selection.MoveEnd(ref unit, ref count);

    strLine = wordObject.Selection.Text;
    richTextBox1.Text += ++index + " - " + strLine + "\r\n"; //for our understanding

    object direction = Word.WdCollapseDirection.wdCollapseEnd;
    wordObject.Selection.Collapse(ref direction);

    if (wordObject.Selection.Bookmarks.Exists(@"\EndOfDoc"))
        bolEOF = true;
} while (!bolEOF);

docs.Close(ref nullobject, ref nullobject, ref nullobject);
wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);
docs = null;
wordObject = null;
资料来源:

现在将每行复制到变量中,并使用此Regex命令检查您的模式:

Regex.Match(MYTEXT, @"\[[([^)]*)\]]").Groups[1].Value

@SystemDown如何准确处理Word文档OCR?Word不是图形格式,你说得对。我看到了扫描,我的思维跳转到OCR。我的错,我想看看这个。(我还没有实际使用过它,所以请注意)假设文档是Word 2007和更高版本,这是正确的做法。谢谢,我会看一看,为了提取数据,我会看一下OpenXML SDK而不是Interop。OpenXML的速度要快得多,互操作仅限于在用户机器上工作,例如,任何服务器端工作都应该避免。