打开word文档并搜索突出显示的文本。 [英] Opening word document and search highlighted text.

查看:106
本文介绍了打开word文档并搜索突出显示的文本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有

我是新手,目前正在学习C#和Visual Studio。我希望有人可以用片段代码帮助我。我希望开发一个程序,它将打开一个word文档并搜索整个文档,看看是否有任何高亮的文本。如果是这样,希望
将突出显示的文本写入另一个word文档。

I am new to this and currently learning C# and Visual Studio. I wish if someone could help me with piece code. I wish development a program that will open a word document and search the entire document to see if there is any highlighed text. If so, wish to write the highlighted text to another word document.

我在网上搜索并且编码我有代码就是错误。

I have searched on the net and codess that I have code is simply erroring.

有任何建议吗?

请期待您的回复。

谢谢。

推荐答案

以下解决方案适合我。它基于第三方库 - Spire.Doc for .NET,您可以从
获得 NuGet

using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Word
{
    class Program
    {
        static void Main(string[] args)
        {   
            //Load the word file
            Document doc = new Document();
            doc.LoadFromFile("input.docx");

            //Create a new word file
            Document newDoc = new Document();
            //Add a paragraph
            Paragraph newPara = newDoc.AddSection().AddParagraph();

            //Loop through sections
            foreach (Section section in doc.Sections)
            {
                //Loop through paragraphs
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    //Loop through child objects
                    foreach (DocumentObject obj in paragraph.ChildObjects)
                    {
                        //Add the highlighted text (including formatting) to the new word file
                        if (obj is TextRange)
                        {
                            if (!(obj as TextRange).CharacterFormat.HighlightColor.IsEmpty)
                            {
                                newPara.ChildObjects.Add(obj.Clone());
                                newPara.AppendText("\n");
                            }
                        }
                    }
                }
            }

            //Save
            newDoc.SaveToFile("HighlightedText.docx", FileFormat.Docx2010);
            
}


这篇关于打开word文档并搜索突出显示的文本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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