首页 > 解决方案 > 存储 ITextSharp 数据

问题描述

所以更新一下,我的代码能够读取单个 pdf 文件并将信息解析为文本文件。伟大的。现在我想弄清楚如何做以下两件事。

  1. 让程序能够读取超过 1 个 pdf 文件。如果我能让它读取整个文件夹,那将是最好的。我不确定如何更改代码来做到这一点,但我知道它不可能有那么不同。

  2. 更改激活方法。如果我能得到它,以便在将新文件放入文件夹时运行代码,那将是绝对惊人的。这必须是可能的,以某种方式拥有一个事件侦听器,该侦听器在将文件放入文件夹并解析信息时激活。

     public static string ExtractTextFromPdf(string path)
     {
         ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
    
         using (PdfReader reader = new PdfReader(path))
         {
             StringBuilder text = new StringBuilder();
    
             for (int page = 1; page <= reader.NumberOfPages; page++)
             {
                 ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
    
                 string currentText = PdfTextExtractor.GetTextFromPage(reader, page, strategy);
    
                 currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                 text.Append(currentText);
             }
    
             System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\kttricic\OneDrive - Burns & McDonnell\Desktop\test file\POs\test");
             file.WriteLine(text);
    
             file.Close();
    
             return text.ToString();
         }
     }
    
     static void Main(string[] args)
     {
         Console.WriteLine(ExtractTextFromPdf(@"C:\Users\kttricic\OneDrive - Burns & McDonnell\Desktop\test file\POs\PO 4505234816 Siemens Industry, Inc. 6.15.21.pdf"));
     }
    

标签: c#.netdirectory

解决方案


推荐阅读