【问题标题】:Filtering PDF files that contain given keyword过滤包含给定关键字的 PDF 文件
【发布时间】:2023-03-23 01:30:02
【问题描述】:

我正在尝试制作一个listBox,它将显示包含textBox 中给定关键字的PDF 文件。
我正在使用 iTextSharp 7。所有文件都作为完整路径字符串加载到 listBox
这是我到目前为止所做的:

查找给定关键字的函数:

private int ReadPdfFile(string fileName, String searthText)
{
    int indicator = 0; 

    if (File.Exists(fileName))
    {
        PdfReader pdfReader = new PdfReader(fileName);
        PdfDocument pdfDocument = new PdfDocument(pdfReader);
        {
            for (int page = 1; page <= pdfDocument.GetNumberOfPages(); page++)
            {
                ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

                string currentPageText = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(page), strategy);

                if (currentPageText.Contains(searthText))
                {
                    indicator++;
                }
            }
        }
    }
    return indicator;
}

还有textBox 代码:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    listBox2.Items.Clear();

    for (int i = 0; i < items.Count; i++)
    {
        if (ReadPdfFile(items[i].ToString(), textBox1.Text)>0)
        {
            listBox2.Items.Add(items[i]);
        }
    }          
}

但是当我尝试在文本框中输入任何内容时,我在 PdfDocument pdfDocument = new PdfDocument(pdfReader); 处收到以下异常

System.IO.FileNotFoundException: '无法加载文件或程序集 'Common.Logging,版本=3.4.1.0,文化=中性, PublicKeyToken=af08829b84f0328e' 或其依赖项之一。这 系统找不到指定的文件。'

有什么建议吗? 我是否朝着正确的方向前进?

【问题讨论】:

  • IText7 有一些依赖关系,其中之一是Common.Logging。我建议重新安装 NuGet 包。 (顺便说一句,在每个TextChanged 事件上搜索 PDF 文件的完整列表将导致其他问题,稍后,IMO)。 (顺便说一句,我会检查两次 AGPL 许可证)
  • 我会努力解决的!谢谢你的关心,但我只是在学习,我不能或不打算做任何实用的事情,我的知识至少不能说,在这方面完全是菜鸟......
  • 我的回答对您有帮助吗?
  • 解决了这个问题!我已经接受了你的回答。但正如@Jimi 所说,在每个 TextChanged 事件上搜索 PDF 文件的完整列表会导致其他问题。如果我输入的字母过多,它就会冻结,所以我需要找到一种方法来处理这个问题。

标签: c# winforms pdf itext


【解决方案1】:

你需要检查文件Common.Logging.dll是否在你项目的bin目录下,如果没有

在包管理器控制台中的 Visual Studio 中试试这个。

PM> Install-Package Common.Logging

【讨论】:

    猜你喜欢
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2011-10-13
    • 1970-01-01
    • 2015-06-08
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多