【问题标题】:Searching pdfs in a C# winform在 C# winform 中搜索 pdf
【发布时间】:2012-07-30 13:03:03
【问题描述】:

我需要使用 C# 在 Visual Studio 2010 中创建一个 winform,它在目录中搜索 pdf 文件,然后在 pdf 中搜索某些文本。例如,用户可以在 winform 中输入“John Smith”。该程序需要在给定目录中的所有 pdf 中搜索文本“John Smith”。我目前没有 Adob​​e Acrobat,可能无法购买它或任何非免费插件。我被告知要查看 Apache Solr 和 Ghostscript,但看不到如何在 winform 中使用它们。我已经搜索了很多并看到了很多建议,但是找不到任何关于如何设置 winform 来搜索 pdf 的简单示例或教程。有人可以提供一些关于如何在 winform 中搜索 pdf 的示例代码吗?

【问题讨论】:

  • 我已经下载了 solr 和 ghostscript,但不知道如何在我的 Visual Studio 项目中设置或使用它们。

标签: c# winforms pdf solr ghostscript


【解决方案1】:

要在 PDF 中搜索特定文本,您可以使用 ITextSharp 库,网址为 http://sourceforge.net/projects/itextsharp/

这是一个简单的例子

var reader = new PdfReader(pdfPath); 
StringWriter output = new StringWriter();  

for (int i = 1; i <= reader.NumberOfPages; i++) 
    output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));

//now you can search for the text from outPut.ToString();

【讨论】:

  • 谢谢。知道如何用 Ghostscript 做同样的事情吗?另外,使用 itextsharp 需要包含哪些内容(例如将 using #itextsharp 放在顶部或将 .dll 复制到某个文件夹)?
  • Ghostscript 在 Windows 上构建为 DLL,并且由于它是作为源提供的,因此显然可以将其重新构建为静态库或其他组件。我对 Winform 一无所知,但我不明白为什么这应该是一个不可逾越的障碍。可以使用“txtwrite”设备提取文本。我会说 GS 对于您的目的来说可能太大了,如果您只想处理 PDF 文件,您可能想考虑 MuPDF。
  • 我使用来自wibit.net/blog/integrating_ghostscript_c 的测试脚本将pdf 转换为ps 文件,它可以工作。我尝试将其更改为文本文件(使用 txtwrite),但输出文件为空白。即使这些工作,我仍然不知道如何从 C# 搜索新的 ps 或文本文件。谁能告诉我如何做到这一点?
  • 这是我试过但不起作用的代码(生成一个空白的 txt 文件):WiBit.Net.WGhostScript gs = new WiBit.Net.WGhostScript(); gs.AddParam("-q"); gs.AddParam("-dNOPAUSE"); gs.AddParam("-dBATCH"); gs.AddParam("-dQUIET"); gs.AddParam("-sDEVICE=txtwrite"); gs.AddParam("-dLanguageLevel=" + psLanguageLevel.ToString()); gs.AddParam("-dSetPageSize"); gs.AddParam("-sOutputFile=" + psFilePath); gs.AddParam(pdfFilePath); gs.Execute();
  • 看起来 iTextSharp 是唯一的答案。谢谢!
猜你喜欢
  • 2011-09-06
  • 2012-01-09
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
  • 2011-12-30
  • 1970-01-01
相关资源
最近更新 更多