【问题标题】:Extract text between 2 strings from word document using aspose.words in C#在 C# 中使用 aspose.words 从 word 文档中提取 2 个字符串之间的文本
【发布时间】:2020-12-30 15:40:41
【问题描述】:

我有一个 word 文档,我需要从中提取几行文本。我需要提取的文本可以在两个字符串之间找到:“must haves”和“could haves”。有谁知道我应该怎么做才能实现这一目标?

【问题讨论】:

    标签: c# aspose.words


    【解决方案1】:

    您可以使用IReplacingCallback 来实现您所需要的。例如看下面的代码:

    Document doc = new Document(@"C:\temp\in.docx");
    FindReplaceOptions opt = new FindReplaceOptions();
    opt.ReplacingCallback = new MyReplacingCallback();
    Regex regex = new Regex(@"\<mytag\>(.*?)\<\/mytag\>");
    doc.Range.Replace(regex, "", opt);
    
    private class MyReplacingCallback : IReplacingCallback
    {
        public ReplaceAction Replacing(ReplacingArgs args)
        {
            Console.WriteLine(args.Match.Groups[1].Value);
            return ReplaceAction.Skip;
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用 tika 从 docx... 中提取文本: https://www.nuget.org/packages/TikaOnDotNet.TextExtractor

      var str = new TikaOnDotNet.TextExtraction.TextExtractor().Extract(@"C:\Users\Inconnu\Downloads\test.docx").Text;
      
                  int pForm = str.IndexOf("must haves") + "must haves".Length;
                  int pTo = str.LastIndexOf("could haves");
      
                  string result = str.Substring(pForm, pTo - pForm);
              
      

      【讨论】:

      • 当我尝试这个时,我在第一行得到一个错误: MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System. Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多