如何解决在C#中使用aspose.words从word文档中提取2个字符串之间的文本
我有一个 word 文档,我需要从中提取几行文本。我需要提取的文本可以在两个字符串之间找到:“必须拥有”和“可以拥有”。有谁知道我应该怎么做才能实现这一目标?
解决方法
您可以使用 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;
}
}
,
使用 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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。