Highlight duplicate question in MS Word using C# in ASP.Net

Last Reply on Jul 19, 2016 06:15 AM By AnandM

Posted on Jul 19, 2016 01:51 AM

Hi! I have question and answer in MS Word. But I want use C# define duplicate question and change color duplicate question. First of all add MS Word document by using open dialog in program after processing define duplicate change color and save result in MS Word document. How I can solve this? Please help me!!! Here I shared result with example.

 

@1. The capital of Afghanistan.

$A) Kabul; $B) Delhi; $C)Dushanbe; $D)Tokyo.

@2. The capital of India.

$A) Kabul; $B) Delhi; $C)Dushanbe; $D)Tokyo.

@3. The capital of Tajikistan.

$A) Kabul; $B) Delhi; $C)Dushanbe; $D)Tokyo.

@4. The capital of Japan.

$A) Kabul; $B) Delhi; $C)Dushanbe; $D)Tokyo.

@5. The capital of Russia.

$A) Kabul; $B) Delhi; $C)Dushanbe; $D)Moscow.

@6. The capital of Afghanistan.

$A) Kabul; $B) Delhi; $C)Dushanbe; $D)Tokyo.

@7. The capital of Turkey.

$A) Kabul; $B) Delhi; $C)Istanbul; $D)Tokyo.

@8. The capital of Tajikistan.

$A) Kabul; $B) Delhi; $C)Dushanbe; $D)Tokyo.

 

   Here automatically color duplicate question changed after processing.

Posted on Jul 19, 2016 06:15 AM Modified on on Jul 19, 2016 06:22 AM

Hi PRA,

Please refer below code

HTML

<div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
</div>

C#

protected void Upload(object sender, EventArgs e)
{
    if (this.FileUpload1.HasFile)
    {
        Application word = new Application();
        Document doc = new Document();
        string filePath = Server.MapPath("~/Word/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(filePath);
        object missing = System.Type.Missing;
        object fileName = filePath;
        doc = word.Documents.Open(ref fileName,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);        
        List<string> data = new List<string>();
        for (int i = 0; i < doc.Paragraphs.Count; i++)
        {
            string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
            if (temp != string.Empty)
                data.Add(temp);
        }
        var duplicateQuestions = data.Where(x => x.Contains("@")).Select(y => y.Split('.')[1].Trim()).GroupBy(x => x).Where(z => z.Count() > 1).Select(k => k.Key);
        foreach (string item in duplicateQuestions)
        {
            int count = 0;
            for (int i = 0; i < doc.Paragraphs.Count; i++)
            {
                string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
                if (temp.Split('.').Length > 1)
                {
                    if (item == temp.Split('.')[1].Trim())
                    {
                        count++;
                    }
                    if (item == temp.Split('.')[1].Trim() && count > 1)
                    {
                        doc.Paragraphs[i + 1].Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGreen;
                    }
                }
            }
        }
        doc.Save();
        doc.Close(ref missing, ref missing, ref missing);
        ((_Application)word).Quit();
    }
}

ScreenShot

Hope this works for you