private void SearchInPDFButton_Click(object sender, EventArgs e) { //This will look through all the pdfs in the list and search for a keyword // then spit out a list of the pdfs and pages they were found List <string> filesIncludingSearch = new List <string>(); try { for (int i = 0; i < FileList.Items.Count; i++) { var filePath = FileList.Items[i].Text; PdfDocument pdfDocument = new PdfDocument(filePath); for (int j = 0; j < pdfDocument.Pages.Count; j++) { PdfPage pdfPage = pdfDocument.Pages[j]; //string content; string pdfText = pdfPage.ToString(); var pdfToText = new PdfToText(); pdfToText.Load(filePath); var content = pdfToText.GetText(); if (content.Contains(SearchQueryTextBox.ToString())) { filesIncludingSearch.Add($"Found in {filePath} on page {j}"); } } } ; MessageBox.Show($"Sucessfully searched. Found on {String.Join(",\n", filesIncludingSearch)}"); } catch (Exception) { MessageBox.Show("Failed To Search"); } //for (int i = 0; i < FileList.Items.Count; i++) //{ // string fileName = FileList.Items[i].Text; // PdfDocument pdfDocument = new PdfDocument(fileName); // for (int j = 0; j < pdfDocument.Pages.Count; j++) // { // PdfPage pdfPage = pdfDocument.Pages[j]; // //string content; // string pdfText = pdfPage.ToString(); // var content = new PdfToText().Search(SearchQueryTextBox.Text, false, false); // if (content.ToString().Contains(SearchQueryTextBox.ToString())) // { // filesIncludingSearch.Add($"Found in {fileName} on page {j}"); // } // } //} }
public List <GerenciadorArquivoModel> CheckPdf(List <GerenciadorArquivoModel> arquivos) { try { foreach (var item in arquivos) { #region ////Abre o arquivo pdf //using (PdfReader leitor = new PdfReader(item.CaminhoFisico)) //{ // //Cria uma String // StringBuilder texto = new StringBuilder(); // for (int i = 1; i <= leitor.NumberOfPages; i++) // { // //Adiciona o texto que extraiu do pdf // texto.Append(PdfTextExtractor.GetTextFromPage(leitor, i)); // string[] teste = texto.ToString().Split('\n'); // } //} #endregion // instantiate a pdf to text converter object PdfToText pdfToText = new PdfToText(); // load PDF file pdfToText.Load(item.CaminhoFisico); // set the properties pdfToText.Layout = TextLayout.Original; pdfToText.StartPageNumber = 1; pdfToText.EndPageNumber = 0; // extract the text string text = pdfToText.GetText(); string freetrial = "===========================================================================================================\r\n\r\nYou are currently using Demo Version - Select.Pdf SDK. With the free trial version,\r\nonly the first 3 pages of the PDF document are converted to text.\r\n\r\n===========================================================================================================\r\n\r\n\r\n\r\nDemo Version - Select.Pdf SDK - http://selectpdf.com\r\n"; if (text == freetrial) { text = ""; } //transforma em string var textopdf = text.ToString(); //char teste = ControlChars.Lf; //var vamotropa = text.Split(teste); //verifica se veio algum texto do pdf if (textopdf != "") { //Valida como pdf e insere o texto na classe item.Pdf = true; item.Texto = text; } } return(arquivos); } catch (Exception ex) { return(new List <GerenciadorArquivoModel>()); } }