0

I am trying to search for text in each PDF inside of a directory using itext7. I can figure out how to search just one PDF.

I managed to search one pdf using the below code, how can I make this work for each PDF in a directory?

   public List<int> ReadPdfFile(string fileName, String searchString)
        {
            List<int> pages = new List<int>();
            if (File.Exists(fileName))
            {
                PdfReader pdfReader = new PdfReader(fileName);
                for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                {
                    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

                    string currentPageText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
                    if (currentPageText.Contains(searchString))
                    {
                        MessageBox.Show("Found COLLIN GRADY");
                    }
                    else
                    {
                        MessageBox.Show("Could not find COLLIN GRADY");
                    }

                }
                pdfReader.Close();
            }
            return pages;
        }

This works, by calling

 ReadPdfFile("C:\\Users\\Billy\\Desktop\\All custom flyers\\ALBANY Ketchup Nov 2018 2.pdf", "COLLIN GRADY");
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Something like `foreach (fileName in directory where file extension is "pdf") { ReadPdfFile(fileName);}` ? - and who is Collin Grady?? – stuartd Oct 31 '18 at 00:13
  • 1
    See [Directory.GetFiles of certain extension](https://stackoverflow.com/questions/13301053/directory-getfiles-of-certain-extension) – stuartd Oct 31 '18 at 00:15

0 Answers0