C# 使用c突出显示Docx中的文本#
我需要突出显示docx文件中的一个句子,我有这段代码,它对许多文档都很有效,但我注意到,对于某些文档,文档中的文本是逐字设置的,而不是整个句子,我指的是每个单词都有自己的运行方式,因此在搜索该句子时,找不到它,因为它是docx中逐字设置的。 注:我正在使用阿拉伯语文本C# 使用c突出显示Docx中的文本#,c#,openxml,highlight,docx,C#,Openxml,Highlight,Docx,我需要突出显示docx文件中的一个句子,我有这段代码,它对许多文档都很有效,但我注意到,对于某些文档,文档中的文本是逐字设置的,而不是整个句子,我指的是每个单词都有自己的运行方式,因此在搜索该句子时,找不到它,因为它是docx中逐字设置的。 注:我正在使用阿拉伯语文本 private void HighLightText_userSentence(Paragraph paragraph, string text, string title,
private void HighLightText_userSentence(Paragraph paragraph, string text, string title, string author, decimal percentage, string _color)
{
string textOfRun = string.Empty;
var runCollection = paragraph.Descendants<Run>();
Run runAfter = null;
//find the run part which contains the characters
foreach (Run run in runCollection)
{
if (run.GetFirstChild<Text>() != null)
{
textOfRun = run.GetFirstChild<Text>().Text.Trim();
if (textOfRun.Contains(text))
{
//remove the character from thsi run part
run.GetFirstChild<Text>().Text = textOfRun.Replace(text, "");
runAfter = run;
break;
}
}
}
// create a new run with your customization font and the character as its text
Run HighLightRun = new Run();
RunProperties runPro = new RunProperties();
RunFonts runFont = new RunFonts() { Ascii = "Curlz MT", HighAnsi = "Curlz MT" };
Bold bold = new Bold();
DocumentFormat.OpenXml.Wordprocessing.Color color = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = _color };
DocumentFormat.OpenXml.Wordprocessing.FontSize fontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize() { Val = "22" };
FontSizeComplexScript fontSizeComplex = new FontSizeComplexScript() { Val = "24" };
Text runText = new Text() { Text = text };
//runPro.Append(runFont);
runPro.Append(bold);
runPro.Append(color);
//runPro.Append(fontSize);
// runPro.Append(fontSizeComplex);
HighLightRun.Append(runPro);
HighLightRun.Append(runText);
//HighLightRun.AppendChild(new Break());
//HighLightRun.PrependChild(new Break());
//insert the new created run part
paragraph.InsertBefore(HighLightRun, runAfter);
}
private void HighLightText\u用户语句(段落、字符串文本、字符串标题、字符串作者、小数百分比、字符串颜色)
{
string textOfRun=string.Empty;
var runCollection=段落.子体();
runAfter=null;
//查找包含字符的运行部件
foreach(在runCollection中运行)
{
if(run.GetFirstChild()!=null)
{
textOfRun=run.GetFirstChild().Text.Trim();
if(textOfRun.Contains(text))
{
//从thsi运行部件中删除字符
run.GetFirstChild().Text=textOfRun.Replace(Text,“”);
runAfter=运行;
打破
}
}
}
//使用自定义字体和字符作为文本创建新的跑步记录
运行HighLightRun=新运行();
RunProperties runPro=新的RunProperties();
RunFonts runFont=newrunfonts(){Ascii=“Curlz MT”,HighAnsi=“Curlz MT”};
粗体=新粗体();
DocumentFormat.OpenXml.Wordprocessing.Color Color=新DocumentFormat.OpenXml.Wordprocessing.Color(){Val=\u Color};
DocumentFormat.OpenXml.Wordprocessing.FontSize FontSize=新的DocumentFormat.OpenXml.Wordprocessing.FontSize(){Val=“22”};
FontSizeComplexScript fontSizeComplex=新的FontSizeComplexScript(){Val=“24”};
Text runText=newtext(){Text=Text};
//runPro.Append(runFont);
runPro.Append(粗体);
runPro.Append(彩色);
//runPro.Append(fontSize);
//runPro.Append(fontSizeComplex);
HighLightRun.Append(runPro);
HighLightRun.Append(runText);
//HighLightRun.AppendChild(新的Break());
//HighLightRun.PrependChild(新的Break());
//插入新创建的管路零件
段落.插入前(突出显示,突出显示后);
}
我最近使用了docX,在搜索和编辑文本方面遇到了问题。我试过一种间接的方法。它很简单,在大多数情况下都有效。我使用replace语句来完成它。
此处“搜索文本”是要突出显示的文本
using (DocX doc = DocX.Load("d:\\Sample.docx"))
{
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
foreach (var item in doc.Paragraphs[i])
{
if (doc.Paragraphs[i] is Paragraph)
{
Paragraph sen = doc.Paragraphs[i] as Paragraph;
Formatting form = new Formatting();
form.Highlight = Highlight.yellow;
form.Bold = true;
sen.ReplaceText(searchText, searchText, false,
System.Text.RegularExpressions.RegexOptions.IgnoreCase,
form, null, MatchFormattingOptions.ExactMatch);
}
}
}
doc.Save();
}
使用(DocX doc=DocX.Load(“d:\\Sample.DocX”))
{
对于(int i=0;i