Monday, March 5, 2012

Resume Import and Parsing in Asp.net

Code.aspx:
 <div>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:Button ID="BtnFileUpload" runat="server" Text="Upload" OnClick="BtnFileUpload_Click" />
        <br />
        <asp:TextBox ID="txtPreview" runat="server" TextMode="MultiLine" Width="467" Height="199px"
            ReadOnly="true"></asp:TextBox>
        <br />
        <asp:Button ID="btnPreview" runat="server" Text="Preview" OnClick="btnPreview_Click" /><br />
        First Name:
        <asp:TextBox ID="txtFirstName" runat="server">
        </asp:TextBox>
        Last Name:<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        <br />
        TelePhone :<asp:TextBox ID="txtCellPhone" runat="server"></asp:TextBox>
        Key skills:
        <asp:TextBox ID="txtKeySkills" runat="server"></asp:TextBox>
        Email:
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
    </div>
code.aspx.cs:
First Add the dll as Microsoft.Office.Interop.Word
 int j = 0;
    //string skill = "";
    string str2 = "";
    int z = 0;
    int t = 0;
protected void BtnFileUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string FileName = FileUpload1.FileName;
            string extn = string.Empty;
            int j = 0;
          string s=Path.GetExtension(FileName);
            while (File.Exists(Server.MapPath("Resumes/" + FileName)))
            {
                FileName = FileName.Replace(Path.GetExtension(FileName), "_" + j + Path.GetExtension(FileName));
                extn = Path.GetExtension(FileName);
                extn = extn.Remove(0, 1).ToLower();
                if ((extn != "doc") && (extn != "docx"))
                {
                    //lblMessage.Text = "The File You Have Uploaded Is Invalid";
                    return;
                }
                j++;
            }
            FileName = FileName.Replace(Path.GetExtension(FileName), "_" + j + Path.GetExtension(FileName));
            extn = Path.GetExtension(FileName);
            extn = extn.Remove(0, 1).ToLower();
            if ((extn != "doc") && (extn != "docx"))
            {
                //lblMessage.Text = "The File You Have Uploaded Is Invalid";
                //lblMessage.ForeColor = System.Drawing.Color.Red;
                return;
            }
            name("name");
            skills("skills");
            Emails("Email");
            telephone("telephone");
            txtPreview.Visible = true;
            Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass();
            object nullobj = System.Reflection.Missing.Value;
            FileUpload1.SaveAs(Server.MapPath("Resumes/" + FileUpload1.FileName));
            object file = Server.MapPath("Resumes/" + FileUpload1.FileName);
            Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            doc.Activate();
            txtPreview.Text = doc.Content.Text;
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
        }
    }
    protected void btnPreview_Click(object sender, EventArgs e)
    {
        txtPreview.Visible = true;
        if (FileUpload1.HasFile)
        {
            try
            {

                Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass();
                object nullobj = System.Reflection.Missing.Value;
                FileUpload1.SaveAs(Server.MapPath("Resumes/" + FileUpload1.FileName));
                object file = Server.MapPath("Resumes/" + FileUpload1.FileName);
                Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj, ref nullobj);
                doc.Activate();
                txtPreview.Text = doc.Content.Text;
                doc.Close(ref nullobj, ref nullobj, ref nullobj);
                File.Delete(Server.MapPath("Resumes/" + FileUpload1.FileName));
            }
            catch (Exception)
            {

            }
        }
    }
    public void telephone(string str1)
    {
        try
        {
            Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass();
            object nullobj = System.Reflection.Missing.Value;
            FileUpload1.SaveAs(Server.MapPath("Resumes/" + FileUpload1.FileName));
            object file = Server.MapPath("Resumes/" + FileUpload1.FileName);
            Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            doc.Activate();
            string Doc_Content = doc.Content.Text;
            string str = Doc_Content;
            object var = ExtractNumbers(str);
            string strData = var.ToString();
            char[] separator = new char[] { ',' };
            string[] strSplitArr = strData.Split(separator);
            for (int i = 0; i < strSplitArr.Length; i++)
            {
                if (strSplitArr[i] != "")
                {
                    string str10 = strSplitArr[i].ToString();
                    str2 = str2 + str10;
                    j++;
                    if (str2.Length == 11 || str2.Length == 10 || str2.Length == 12)
                    {
                        if (t == 0)
                        {
                            txtCellPhone.Text = str2;
                            t++;
                            j = 0;
                        }
                        else if (t == 1)
                        {
                            txtCellPhone.Text = str2;
                            t++;
                            j = 0;
                        }
                        str2 = "";
                        j = 1;
                    }
                    j = 0;
                }
                else
                {
                    if (j < 3)
                    {
                        if (z < 3)
                        {
                            string str10 = strSplitArr[i].ToString();
                            str2 = str2 + str10;
                            z++;
                            if (z == 2)
                            {
                                z = 0;
                                str2 = "";
                            }
                        }
                    }
                }
            }
            doc.Close(ref nullobj, ref nullobj, ref nullobj);

        }
        catch (Exception)
        {

            throw;
        }
    }
    static string ExtractNumbers(string Expression)
    {
        return string.Join(",", System.Text.RegularExpressions.Regex.Split(Expression, "[^\\d]"));
    }

    public void name(string str1)
    {
        try
        {

            Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass();
            object nullobj = System.Reflection.Missing.Value;
            string a = Server.MapPath("Resumes/" + FileUpload1.FileName);
            FileUpload1.SaveAs(Server.MapPath("Resumes/" + FileUpload1.FileName));
            object file = Server.MapPath("Resumes/" + FileUpload1.FileName);
            Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            doc.Activate();
            string Doc_Content = doc.Content.Text;
            string str = Doc_Content;
            var words = str.Split(new char[] { ' ', ':', '\r', '\t' });

            for (int i = 0; i < words.Length; i++)
            {
                string val = words[i].ToString();
                string val2 = "";
                int result = string.Compare(val, val2, true);
                if (result != 0)
                {
                    if (j == 0)
                    {
                        string val5 = words[i].ToString();
                        j++;
                        if (words[i + 1].ToString() != "")
                        {
                            txtFirstName.Text = words[i].ToString();
                            txtLastName.Text = words[i + 1].ToString();
                            doc.Close(ref nullobj, ref nullobj, ref nullobj);
                            return;
                        }
                        else
                        {
                            txtLastName.Text = words[i + 2].ToString();
                            doc.Close(ref nullobj, ref nullobj, ref nullobj);
                            return;
                        }
                    }
                }
            }
            doc.Close(ref nullobj, ref nullobj, ref nullobj);

        }
        catch (Exception)
        {

        }
    }

    public void skills(string str1)
    {
        try
        {

            Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass();
            object nullobj = System.Reflection.Missing.Value;
            FileUpload1.SaveAs(Server.MapPath("Resumes/" + FileUpload1.FileName));
            object file = Server.MapPath("Resumes/" + FileUpload1.FileName);
            Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            doc.Activate();
            string Doc_Content = doc.Content.Text;
            string str = Doc_Content;
            var words = str.Split(new char[] { ' ', ':', '\r', '\t' });
            var abcwords = words.Where(w => w.Equals(str1, StringComparison.OrdinalIgnoreCase));
            foreach (var item in abcwords)
            {
                for (int i = 0; i < words.Length; i++)
                {
                    string val = words[i].ToString();
                    string val2 = item;
                    int result = string.Compare(val, val2, true);
                    if (result == 0)
                    {
                        string skill = words[i + 1].ToString(); ;
                        int j = 2;
                        string val5 = words[i].ToString();
                        do
                        {
                            skill = skill + words[i + j].ToString() + " ";
                            j++;

                        } while (words[i + j].ToString() != "");
                        txtKeySkills.Text = skill;
                        doc.Close(ref nullobj, ref nullobj, ref nullobj);
                        return;
                    }
                }
            }
            doc.Close(ref nullobj, ref nullobj, ref nullobj);

        }
        catch (Exception)
        {

        }
    }

    public void Emails(string str1)
    {
        try
        {

            Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass();
            object nullobj = System.Reflection.Missing.Value;
            FileUpload1.SaveAs(Server.MapPath("Resumes/" + FileUpload1.FileName));
            object file = Server.MapPath("Resumes/" + FileUpload1.FileName);
            Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj,
                                              ref nullobj, ref nullobj, ref nullobj, ref nullobj);
            doc.Activate();
            string Doc_Content = doc.Content.Text;
            string str = Doc_Content;
            var words = str.Split(new char[] { ' ', ':', '\r', '\t' });
            for (int i = 0; i < words.Length; i++)
            {
                string val = words[i].ToString();
                string val2 = "@";
                int result = val.IndexOf(val2);
                if (result != -1)
                {
                    string val5 = words[i].ToString();
                    txtEmail.Text = val5;
                    doc.Close(ref nullobj, ref nullobj, ref nullobj);
                    return;
                }
            }
            doc.Close(ref nullobj, ref nullobj, ref nullobj);

        }
        catch (Exception)
        {
        }
    }

8 comments:

  1. Not working....
    I am getting different errors at different times. One such error is

    Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\Roshan\Documents\Visual Studio 2008\Projects\WebApplication3\WebApplication3\Resumes\Resume Latest.doc'.


    Can you help me sort it out?
    Thanks.

    ReplyDelete
    Replies
    1. Create folder named Resumes in the same folder of your aspx page

      Delete
  2. Really excellent startup for resume parsing.. can any one provide any such code which can parse all tyoe of documents(pdf,rtf,doc,txt....)

    ReplyDelete
  3. hello,
    When i used above code with download microsoft.office.interop.word with version 10.0.4504.0 ,
    i faced error is:

    Could not load file or assembly 'Office, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    ReplyDelete