コード例 #1
0
        public ActionResult SearchText([FromBody] string fileName, [FromBody] string keyWord)
        {
            List <string>    extractedText = new List <string>();
            ExtractorFactory factory       = new ExtractorFactory();
            string           filePath      = Server.MapPath("../App_Data//Uploads//" + fileName);

            try
            {
                //ExStart:SearchTextInDocuments
                //get file actual path

                using (WordsTextExtractor extractor = new WordsTextExtractor(filePath))
                {
                    ListSearchHandler handler = new ListSearchHandler();
                    extractor.Search(new SearchOptions(SearchHighlightOptions.CreateFixedLengthOptions(10)), handler, null, new string[] { keyWord });

                    if (handler.List.Count == 0)
                    {
                        Console.WriteLine("Not found");
                    }
                    else
                    {
                        for (int i = 0; i < handler.List.Count; i++)
                        {
                            extractedText.Add("Text at Left: " + handler.List[i].LeftText);

                            extractedText.Add("Found Text: " + handler.List[i].FoundText);

                            extractedText.Add("Text at Right: " + handler.List[i].RightText);
                        }
                    }
                }
                //ExEnd:SearchTextInDocuments
            }
            catch (Exception ex)
            {
                extractedText.Add(ex.Message);
            }
            return(Json(extractedText, JsonRequestBehavior.AllowGet));
        }