using System; using System.Data; using Ease.Core.Model; using Ease.Core.DataAccess; using Ease.Core; using System.Collections.Generic; using Ease.Core.Utility; using HRM.BO; using System.IO; using System.Linq; using HRM.BO.Basic; using NPOI.SS.Formula.Functions; using iTextSharp.text.pdf.parser; using iTextSharp.text.pdf; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; //using static Org.BouncyCastle.Math.EC.ECCurve; using System.Text.RegularExpressions; using System.Text; using UglyToad.PdfPig; using UglyToad.PdfPig.Content; using UglyToad.PdfPig.XObjects; using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource; using Array = System.Array; using NPOI.POIFS.Crypt.Dsig; namespace HRM.DA { public class CVParserService { List listItems = new List(); const string MatchEmailPattern = @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})"; public List readPDF(IFormFileCollection files) { List cvList = new List(); try { int serial = 0; foreach (var formFile in files) { CvReaderAndExtractor cv = new CvReaderAndExtractor(); cvList.Add(cv); byte[] profilePictures = null; StringBuilder text = new StringBuilder(); serial++; var fileSize = formFile.Length; var originalFile = formFile.FileName; var fileExt = System.IO.Path.GetExtension(originalFile).Substring(1); var fileName = formFile.FileName; var ms = new MemoryStream(); formFile.CopyTo(ms); cv.SerialId = serial; cv.FileName = originalFile; cv.ProfilePicByteArray = profilePictures; cv.Ext = fileExt; ExtractFile(cv, ms); } } catch (Exception ex) { } return cvList; } public void ExtractFile(CvReaderAndExtractor ocvitem, MemoryStream ms) { string errorText = ""; string base64String = ""; try { byte[] profilePictures = null; StringBuilder text = new StringBuilder(); byte[] textAsBytes = ms.ToArray(); string fileType = null; base64String = Convert.ToBase64String(textAsBytes); ocvitem.FileAsByteArray = textAsBytes; using (PdfReader pdfReader = new PdfReader(textAsBytes)) { for (int page = 1; page <= pdfReader.NumberOfPages; page++) { ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy); currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText))); text.Append(currentText); } } var text1 = text.ToString(); string[] wordList = text1.Split(' '); string tempPhoneNumber = null; string tempEmail = null; errorText = "Error in Extract Name:"; var name = extractNames(wordList, ocvitem); ocvitem.Name = name; errorText = "Error in Extract Mobile:"; tempPhoneNumber = extractMobile(wordList); ocvitem.MobileNumber = tempPhoneNumber; errorText = "Error in Extract Email:"; tempEmail = extractEamil(wordList); ocvitem.Email = tempEmail; errorText = "Error in Extract Education:"; var education = extractEducation(wordList); ocvitem.Education = education; errorText = "Error in Extract Experience:"; var experience = "";// extractExperience(wordList); errorText = "Error in Extract Skill:"; var skill = "";//extractSkills(text1); //var dateofBirth = ""; var dateofBirth = DateofBirth(wordList); ocvitem.ProfilePicByteArray = profilePictures; ocvitem.Ext = fileType; ocvitem.Experience = experience; ocvitem.Skill = skill; ocvitem.BirthDate = dateofBirth; } catch (Exception ex) { var etext = (ex.Message.Length > 100) ? ex.Message.Substring(0, 50) : ex.Message; errorText = errorText + " " + etext; } } public void extractPicture(byte[] textAsBytes) { byte[] profilePictures = null; string fileType = null; using (MemoryStream pdfStream = new MemoryStream(textAsBytes)) // assuming pdfBytes is your byte array containing the PDF document { using (UglyToad.PdfPig.PdfDocument pdfDocument = UglyToad.PdfPig.PdfDocument.Open(pdfStream)) { int imageCount = 1; foreach (Page page in pdfDocument.GetPages()) { List images = page.GetImages().Cast().ToList(); foreach (XObjectImage image in images) { byte[] imageRawBytes = image.RawBytes.ToArray(); if (imageRawBytes != null) { profilePictures = imageRawBytes; fileType = GetFileType(imageRawBytes); break; } imageCount++; } if (imageCount > 0) { break; } } } } } public string extractMobile(string[] wordList) { string tempPhoneNumber = null; string numericPart = null; for (var i = 0; i < wordList.Length; i++) { bool isNumber = false; // mobile Number if (string.IsNullOrEmpty(tempPhoneNumber)) { if (!string.IsNullOrEmpty(wordList[i])) { if (wordList[i].Trim().Any(char.IsDigit)) { var word = wordList[i]; numericPart = new string(wordList[i].Where(char.IsDigit).ToArray()); if (wordList[i].Trim().Contains('+')) { if (wordList[i].Trim().Contains('+') && wordList[i].Trim().Contains('(')) { if (wordList[i].Trim().Contains('(') && wordList[i].Trim().Contains(')')) { var digitPart = wordList[i].Where(char.IsDigit).ToArray(); if (digitPart.Length == 13) { numericPart = new string(digitPart); } else { var digitPart1 = wordList[i + 1].Where(char.IsDigit).ToArray(); if ((digitPart.Length + digitPart1.Length) == 13) { numericPart = new string(digitPart) + new string(digitPart1); } } } numericPart = "+" + numericPart; } else { if ((wordList[i].Where(char.IsDigit).ToArray().Length) > 13) { int endIndex = Math.Min(13, wordList[i].Length); numericPart = string.Join(" ", wordList[i], 0, endIndex); } numericPart = "+" + numericPart; } } isNumber = true; } if (isNumber) { const string MatchPhonePattern = @"\b(\+?8801\d{9}|01\d{9})\b"; Regex rx = new Regex(MatchPhonePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); MatchCollection matches = rx.Matches(numericPart); int noOfMatches = matches.Count; foreach (System.Text.RegularExpressions.Match match in matches) { tempPhoneNumber = match.Value.ToString(); } if (tempPhoneNumber != null) { if (numericPart.Contains("+") && !tempPhoneNumber.Contains("+")) { tempPhoneNumber = "+" + tempPhoneNumber; } } } } } } return tempPhoneNumber; } public string extractEamil(string[] wordList) { string tempEmail = null; for (var i = 0; i < wordList.Length; i++) { bool isNumber = false; if (wordList[i].Contains('@') && string.IsNullOrEmpty(tempEmail)) { Regex rx = new Regex( MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); MatchCollection matches = rx.Matches(wordList[i]); int noOfMatches = matches.Count; foreach (System.Text.RegularExpressions.Match match in matches) { tempEmail = match.Value.ToString(); } } } return tempEmail; } public string DateofBirth(string[] wordList) { string tempBrith = ""; int index = -1; for (var i = 0; i < wordList.Length; i++) { if (wordList[i].Contains("birth", StringComparison.OrdinalIgnoreCase)) { index = i; break; // index = wordList[i].IndexOf("1"); //if(index == -1) index = wordList[i].IndexOf("2"); //if (index == -1) index = wordList[i].IndexOf("3"); //if (index == -1) index = wordList[i].IndexOf("4"); //if (index == -1) index = wordList[i].IndexOf("5"); //if (index == -1) index = wordList[i].IndexOf("6"); //if (index == -1) index = wordList[i].IndexOf("7"); //if (index == -1) index = wordList[i].IndexOf("8"); //if (index == -1) index = wordList[i].IndexOf("9"); //if (index != -1) tempBrith = wordList[i].Substring(index); } //if ((wordList[i].Contains("b", StringComparison.OrdinalIgnoreCase)) // && (wordList[i+1].Contains("i", StringComparison.OrdinalIgnoreCase)) // && (wordList[i+2].Contains("r", StringComparison.OrdinalIgnoreCase)) // && (wordList[i+3].Contains("t", StringComparison.OrdinalIgnoreCase)) // && (wordList[i+4].Contains("h", StringComparison.OrdinalIgnoreCase))) // { // index = i+5; // break; // } } if (index != -1) { for (var i = index+1; i < index +5; i++) { int intvlalue = 0; if(int.TryParse(wordList[i][0].ToString(), out intvlalue)) { tempBrith = wordList[i]; } } } return tempBrith; } public string extractNames(string[] wordList, CvReaderAndExtractor ocvitem) { bool extratxWords = false; bool newLineWords = false; string extractingWords = ""; int count = 0; for (var i = 0; i < wordList.Length; i++) { string currentWord = wordList[i].ToString().ToLower(); if (!string.IsNullOrEmpty(currentWord) && !string.IsNullOrWhiteSpace(currentWord)) { if (currentWord.Contains("resume") || currentWord.Contains("cv")) { if (wordList[i + 1].ToString().ToLower().Contains("of")) // if resume of found then looking for next words { i++; newLineWords = true; continue; } } else if (currentWord.Contains("name")) { extratxWords = true; } else if (newLineWords) // extended version of resume of { if (count > 0) { if (currentWord.Contains("\n")) //serching for next new line { int newlineIndex = currentWord.IndexOf('\n'); string wordsBeforeNewLine = wordList[i].Substring(0, newlineIndex); extractingWords += " " + wordsBeforeNewLine; break; } else { extractingWords += " " + wordList[i]; continue; } } extractingWords += " " + wordList[i]; //just extracting next word and not checking new line count++; continue; } else { if (extratxWords == false) { extratxWords = true; extractingWords += " " + wordList[i]; continue; } } } if (extratxWords && !newLineWords) { if (currentWord.Contains("\n")) //serching for next new line { int newlineIndex = currentWord.IndexOf('\n'); string wordsBeforeNewLine = wordList[i].Substring(0, newlineIndex); extractingWords += " " + wordsBeforeNewLine; extratxWords = false; break; } else { extractingWords += " " + wordList[i]; //extracting words before finding the new line } } } bool checkValidation = false; extractingWords = extractingWords.Trim(); if (extractingWords.Where(char.IsDigit).Any()) extractingWords = null; if (newLineWords ==false && extractingWords != null && extractingWords !="") { var filenameSp = ocvitem.FileName.Split(' '); var extractName = extractingWords.Split(' '); for(int i =0;i< extractName.Length; i++ ) { for (int j = 0; j < filenameSp.Length; j++) { if (extractName[i].ToLower() == filenameSp[j].ToLower()) { checkValidation = true; break; } } } } if (checkValidation == false) extractingWords = null; if (extractingWords == null) { string fileName = ocvitem.FileName; fileName =fileName.Replace('(', ' '); fileName = fileName.Replace(')', ' '); fileName = fileName.Replace('1', ' '); fileName = fileName.Replace('2', ' '); fileName = fileName.Replace('3', ' '); fileName = fileName.Replace('4', ' '); fileName = fileName.Replace('5', ' '); fileName = fileName.Replace('6', ' '); fileName = fileName.Replace('7', ' '); fileName = fileName.Replace('8', ' '); fileName = fileName.Replace('9', ' '); fileName = fileName.Replace('0', ' '); fileName = fileName.Replace("Copy", "", true, null); fileName = fileName.Replace("of", "", true, null); fileName = fileName.Replace("cv", "", true, null); fileName = fileName.Replace("Resume", "", true, null); fileName = fileName.Replace(".pdf", "", true, null); extractingWords = fileName.Trim(); } return extractingWords; } public string extractExperience(string[] wordList) { string extractingWords = ""; bool isAllExtract = false; bool isPrevWordWork = false; bool isExperienceFound = false; string[] next50Strings = null; string[] experienceKeywords = { "experience", "experinces" }; for (var i = 0; i < wordList.Length; i++) { string currentWord = wordList[i].ToString().ToLower(); if (isPrevWordWork) { if (currentWord.Trim().Contains("experience") || currentWord.Trim().Contains("experiences")) { isExperienceFound = true; continue; } } if (!string.IsNullOrEmpty(currentWord) && !string.IsNullOrWhiteSpace(currentWord)) { if (currentWord.Trim().Contains("work")) { if (currentWord.Trim().Contains("experience") || currentWord.Trim().Contains("experiences")) { isExperienceFound = true; break; } else { isPrevWordWork = true; continue; } } if (isExperienceFound) { next50Strings = new string[Math.Min(200, wordList.Length - i)]; Array.Copy(wordList, i, next50Strings, 0, next50Strings.Length); break; } } } int isDigitCount = 0; bool istilwordfound = false; string tillWords = null; if (next50Strings != null) { for (var i = 0; i < next50Strings.Length; i++) { string currentWord = next50Strings[i].ToString().ToLower(); //if (currentWord.Trim().Contains("present")) //{ // extractingWords += " " + next50Strings[i]; // for(var j = i; j < 5; j++) // { // extractingWords += " " + next50Strings[j]; // } // istilwordfound = true; // break; //} if (currentWord.Trim().Contains("till")) { tillWords += " " + next50Strings[i] + " " + next50Strings[i + 1]; for (var j = i; j < 5; j++) { tillWords += " " + next50Strings[j]; } istilwordfound = true; break; } else { tillWords += " " + next50Strings[i]; } } if (istilwordfound) { extractingWords = tillWords; } if (!istilwordfound) { string lastString = null; List digits = new List(); foreach (string str in next50Strings) { if (str.ToString().ToLower().Contains("present")) { digits.Add(str); } var numericPart = new string(str.Where(char.IsDigit).ToArray()); if (!string.IsNullOrEmpty(numericPart)) { digits.Add(numericPart); } } if (digits != null && digits.Count > 0) lastString = digits[digits.Count - 1]; if (!string.IsNullOrEmpty(lastString)) { for (var i = 0; i < next50Strings.Length; i++) { string currentWord = next50Strings[i].ToString().ToLower(); if (currentWord.Trim().Contains(lastString)) { extractingWords += " " + next50Strings[i]; bool isDigitinSameWord = false; string extractingWords1 = null; for (var j = i + 1; j < next50Strings.Length; j++) { string next = next50Strings[j].ToString().ToLower(); if (next.Trim().Contains(lastString)) { isDigitinSameWord = true; extractingWords1 += " " + next50Strings[j]; break; } else { extractingWords1 += " " + next50Strings[j]; } } if (isDigitinSameWord) { extractingWords = next50Strings + extractingWords1; } } else { extractingWords += " " + next50Strings[i]; } } } } } int index = extractingWords.IndexOf("education", StringComparison.OrdinalIgnoreCase); if (index != -1) { extractingWords = extractingWords.Substring(0, index); } return extractingWords.Trim(); } public string extractSkills(string input) { string extractWords = ""; int index = -1; string targetWord = ""; if (input.ToLower().Contains("skill")) { if (input.ToLower().Contains("skills")) { index = input.ToLower().IndexOf("skills"); targetWord = "skills"; } else if (input.ToLower().Contains("skill")) { index = input.ToLower().IndexOf("skill"); targetWord = "skill"; } if (index != -1) { //int newlineIndex = input.IndexOf('\n', index); // Extract the substring starting from the index of "MySql" to the next 200 characters string result = input.Substring(index + targetWord.Length, Math.Min(500, input.Length - (index + targetWord.Length))); string[] lines = result.Split('\n'); string[] firstFiveLines = new string[Math.Min(10, lines.Length)]; Array.Copy(lines, firstFiveLines, firstFiveLines.Length); foreach (string line in firstFiveLines) { extractWords += line; } } } //if (input.ToLower().Contains("skills")) //{ // int index = input.ToLower().IndexOf("skills"); // if (index != -1) // { // // Extract the substring starting from the index of "MySql" to the next 200 characters // string result = input.Substring(index + "skills".Length, Math.Min(500, input.Length - (index + "skills".Length))); // string[] lines = result.Split('\n'); // string[] firstFiveLines = new string[Math.Min(4, lines.Length)]; // Array.Copy(lines, firstFiveLines, firstFiveLines.Length); // foreach (string line in firstFiveLines) // { // extractWords += line; // } // } //} return extractWords.Trim(); } public string extractEducation(string[] wordList) { string extractingWords = ""; string[] educationalKeywords = { "education", "educational qualifications", "academic qualification", "academic" }; string[] extractingKeywords = { "secondary", "s.s.c", "ssc" }; int startindex = 0; for (var i = 0; i < wordList.Length; i++) { string currentWord = wordList[i].ToString().ToLower(); if (!string.IsNullOrEmpty(currentWord) && !string.IsNullOrWhiteSpace(currentWord)) { foreach (string keyword in educationalKeywords) { if (currentWord.Contains(keyword, StringComparison.CurrentCultureIgnoreCase)) { if (currentWord.Trim().Contains("educational") || currentWord.Trim().Contains("education") || currentWord.Trim().Contains("academic")) { startindex = i; break; } } } } if (startindex != 0) break; } if (startindex == 0) return ""; int maxSearch = startindex + 100; if (maxSearch > wordList.Length) maxSearch = wordList.Length; int endIndex = 0; for (var i = startindex + 1; i < maxSearch; i++) { string currentWord = wordList[i].ToString().ToLower(); if (!string.IsNullOrEmpty(currentWord) && !string.IsNullOrWhiteSpace(currentWord)) { foreach (string keyword in extractingKeywords) { if (currentWord.Contains(keyword, StringComparison.OrdinalIgnoreCase)) { endIndex = i; break; } } } if (endIndex != 0) break; } if (endIndex != 0) { bool endfound = false; for (var i = endIndex; i < endIndex + 15; i++) { if (i > wordList.Length) break; string currentWord = wordList[i].ToString().ToLower(); if (!string.IsNullOrEmpty(currentWord) && !string.IsNullOrWhiteSpace(currentWord)) { if (currentWord.Contains("\n")) { endfound = true; endIndex = i; break; } if(currentWord ==" " && wordList[i-1] ==" ") { endfound = true; endIndex = i; break; } } } if (endfound == false) endIndex = endIndex + 8; } else endIndex = startindex + 20; extractingWords = ""; for (var i = startindex ; i < endIndex; i++) { extractingWords = extractingWords +" "+ wordList[i].ToString().ToLower(); } // sl subject degree result institute passing year // qualification: // exam title concentration / major duration extractingWords = extractingWords.Replace("sl", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("subject", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("degree", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("result", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("institute", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("passing", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("year", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("qualification", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("exam", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("concentration", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("/", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("major", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("duration", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("title", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("education", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("background", "", StringComparison.OrdinalIgnoreCase); extractingWords = extractingWords.Replace("academic", "", StringComparison.OrdinalIgnoreCase); int nIndex= extractingWords.IndexOf(':',0,20); if(nIndex >0) { } // invalid parse if (extractingWords.Contains("university", StringComparison.OrdinalIgnoreCase) ==false && extractingWords.Contains("college", StringComparison.OrdinalIgnoreCase) == false && extractingWords.Contains("school", StringComparison.OrdinalIgnoreCase) == false) { extractingWords = ""; } return extractingWords.Trim(); } public static string GetFileType(byte[] bytes) { if (bytes == null || bytes.Length == 0) { return "Unknown"; } // JPEG if (bytes.Length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xD8) { return "image/jpeg"; } // PNG if (bytes.Length >= 8 && bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47 && bytes[4] == 0x0D && bytes[5] == 0x0A && bytes[6] == 0x1A && bytes[7] == 0x0A) { return "image/png"; } // PDF if (bytes.Length >= 4 && bytes[0] == 0x25 && bytes[1] == 0x50 && bytes[2] == 0x44 && bytes[3] == 0x46) { return "application/pdf"; } // Add more checks for other file types as needed return "Unknown"; } } }