使用C#突出显示文本DOCX [英] highlighting text in Docx using c#

查看:286
本文介绍了使用C#突出显示文本DOCX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要突出的docx文件中的一句话,我有这样的代码,它的许多文件工作正常,但我注意到,对于一些文档的文档中的文本由词集字,而不是整个句子,我的意思有自己运行的每个字,所以那句话搜索时,它找不到,因为它是一字一句的DOCX。
注:我与阿拉伯文本

 私人无效HighLightText_userSentence(逐段字符串文本,字符串标题,笔者字符串,百分比数,串_Color)
{
串textOfRun =的String.Empty;
VAR runCollection = paragraph.Descendants<润GT;();
运行ru​​nAfter = NULL;

//发现其中包含字符
的foreach(在runCollection RUN RUN)
{
运行的一部分,如果(run.GetFirstChild<文本>()!= NULL)
{
textOfRun = run.GetFirstChild<文本>()Text.Trim()。
如果(textOfRun.Contains(文本))
{
//从thsi运行部分
run.GetFirstChild<删除字符,文字及GT;()文本= textOfRun.Replace(。文本,);
runAfter =运行;
中断;

}
}
}

//创建您的自定义字体的新的运行和字符作为其文本
运行HighLightRun =新的运行();
RunProperties runPro =新RunProperties();
RunFonts runFont =新RunFonts(){Ascii码=Curlz MT,HighAnsi =Curlz MT};
黑体加粗=新款Bold();


DocumentFormat.OpenXml.Wordpro​​cessing.Color颜色=新DocumentFormat.OpenXml.Wordpro​​cessing.Color(){瓦尔= _Color};
DocumentFormat.OpenXml.Wordpro​​cessing.FontSize fontSize的=新DocumentFormat.OpenXml.Wordpro​​cessing.FontSize(){瓦尔=22};
FontSizeComplexScript fontSizeComplex =新FontSizeComplexScript(){瓦尔=24};

文本runText =新文本(){文字=文本};
//runPro.Append(runFont);
runPro.Append(黑体);
runPro.Append(彩色);
//runPro.Append(fontSize);
// runPro.Append(fontSizeComplex);

HighLightRun.Append(runPro);
HighLightRun.Append(runText);
//HighLightRun.AppendChild(new歇());
//HighLightRun.PrependChild(new歇());




//将新创建的运行一部分
paragraph.InsertBefore(HighLightRun,runAfter);
}


解决方案

我最近使用的docx,是

面临着搜索和文本higlighting问题。我尝试一种间接的方式。它简单,作品在大多数情况下。我把它用REPLACE语句做。这里
搜索文本是你想要使用(DOCX文档= DocX.Load(D突出

 全文: \\Sample.docx))
{
的for(int i = 0; I< doc.Paragraphs.Count;我++)
{
的foreach(VAR项目在doc.Paragraphs [I])
{
如果(doc.Paragraphs [i]为一段)
{
款仙= doc.Paragraphs [I]作为段落;
格式化形式=新的格式();
form.Highlight = Highlight.yellow;
form.Bold = TRUE;
sen.ReplaceText(SEARCHTEXT,SEARCHTEXT,虚假,
System.Text.RegularExpressions.RegexOptions.IgnoreCase,
形式,空,MatchFormattingOptions.ExactMatch);
}
}
}
doc.Save();
}


I need to highlight a sentence in docx file, I have this code, and its working fine for many documents , but i noticed that for some document the text inside the document is set word by word, not whole sentence, I mean each word with its own Run, so when searching for that sentence, it is not found because it is word by word in the docx. NOTE: I am working with Arabic text.

    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);
}

解决方案

I recently used docX and was facing problems with searching and higlighting text. I tried an indirect way. It simple and works in most situations. I do it using the replace statement. here search text is the text you want to highlight

    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();
    }

这篇关于使用C#突出显示文本DOCX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆