C# 如何从 Pdf、Word 和 Excel 文档中提取文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10982156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract text from Pdf, Word and Excel documents?
提问by The Light
I'd need a .NET library so that using which I can extract text data from PDF, Excel and Word files.
我需要一个 .NET 库,以便使用它从 PDF、Excel 和 Word 文件中提取文本数据。
Ideally, a free tool!
理想情况下,一个免费的工具!
Would you recommend any?
你会推荐任何吗?
many thanks,
非常感谢,
采纳答案by Christopher Currens
As someone who has spent many days looking for free solutions for (nearly) this exact problem, I can tell you fairly honestly that you will not find a free library that will be able to extract text from allof those formats well. The only library that I'm aware of that does a great job with all of those formats (and more) is a commercial library, and it's not actually native to .NET, it's a C++/COM library, with a C++/CLI .NET wrapper.
作为一个花了很多天寻找(几乎)这个确切问题的免费解决方案的人,我可以诚实地告诉你,你找不到一个能够很好地从所有这些格式中提取文本的免费库。我所知道的唯一一个可以很好地处理所有这些格式(以及更多格式)的库是一个商业库,它实际上不是 .NET 的原生库,它是一个 C++/COM 库,带有 C++/CLI 。 NET 包装器。
What are some options?
有哪些选择?
iTextSharp-- This one is absolutely fantastic in extracting text from PDFs. While later versions of this library were commercial friendly (LGPL), the authors have decided instead that they want to charge for the software, so they've instead released it under the AGPL, so unless you want to release all of your source code, you probably don't want to use one of those versions. However, the last version (4.1.6) licensed under the LGPL can be found all over the internet. This SO questionhas a link to a version that is under the LGPL.
PdfBox-- Another PDF library. This one, IMO, is better because it's under the Apache 2.0 license. There are a few issues with it, as it sometimes(perhaps rarely) will not do as good of a job as iTextSharp. I attribute this more to the fact that it's a newer library than anything else. However,my experience with this library is from monthsago. This project is actively developed, and just in the last month, 52 issues have been resolved. I would keep my eye on this one. Please note this is a java library. (Keep reading below for more information on why I've included this.)
POIor NPOI-- These are libraries specifically written for Microsoft office documents, particularly the pre-2007 formats, OLE binary file formats. It does support the newer OpenXML formats, though I'm not sure how mature that part of the library is. POI is the java version (Keep reading below for more information on why I've included this.), where NPOI is a native .NET version. However, NPOI only supports excel documents, where POI can do text extraction on many more types.
Open XML SDK 2.0-- A library for reading/modifying office 2007+ (unencrypted OpenXML) documents created my Microsoft themselves! This is an amazing library for working with these kinds of documents. However, it is a lower-level library and therefore doesn't actually (as far as I know of), have a it does everythingtext extraction class. There's a fairly good example, (I'm not sure it covers certain cases like text in tables, etc), of text extraction from a word document at this SO answer
Tika-- Once again, another Java library (I'm not telling you about java libraries for no reason. Keep on reading! :)), and this will be as close to "one library" for text extraction as you can get. Tika can extract metadata and structured text contentfrom many different kinds of files, using existing parsing libraries. It actually uses POI and PdfBox under the hood for office and PDF documents.
iTextSharp——这在从 PDF 中提取文本方面非常棒。虽然这个库的后期版本是商业友好的 (LGPL),但作者决定要对软件收费,所以他们改为在 AGPL 下发布它,所以除非你想发布所有源代码,您可能不想使用这些版本之一。然而,在 LGPL 许可下的最新版本 (4.1.6) 可以在互联网上找到。这个 SO 问题有一个链接到 LGPL 下的版本。
PdfBox-- 另一个 PDF 库。这个,IMO,更好,因为它在 Apache 2.0 许可下。它有一些问题,因为它有时(可能很少)不会像 iTextSharp 那样好。我将此更多地归因于它是一个比其他任何东西都更新的库。 但是,我对这个库的体验是几个月前的。该项目正在积极开发中,仅在上个月就解决了 52 个问题。我会一直关注这个。请注意,这是一个 Java 库。(请继续阅读以下内容,了解有关我为何包含此内容的更多信息。)
POI或NPOI-- 这些是专门为 Microsoft 办公文档编写的库,尤其是 2007 年之前的格式、OLE 二进制文件格式。它确实支持较新的 OpenXML 格式,但我不确定该部分库的成熟程度。POI 是 Java 版本(请继续阅读下面的内容以了解我为何包含此内容的更多信息。),其中 NPOI 是本机 .NET 版本。但是NPOI只支持excel文档,POI可以做更多类型的文本提取。
Open XML SDK 2.0-- 一个用于读取/修改 office 2007+(未加密的 OpenXML)文档的库,我自己创建了我的 Microsoft!这是一个用于处理此类文档的惊人库。但是,它是一个较低级别的库,因此实际上(据我所知)并没有一个它可以完成所有文本提取类。有一个相当好的例子,(我不确定它是否涵盖某些情况,如表格中的文本等),在这个 SO 答案中从 word 文档中提取文本
Tika—— 再一次,另一个 Java 库(我不会无缘无故地告诉你 Java 库。继续阅读!:)),这将尽可能接近用于文本提取的“一个库”。Tika 可以使用现有的解析库从许多不同类型的文件中提取元数据和结构化文本内容。它实际上使用 POI 和 PdfBox 来处理 Office 和 PDF 文档。
Non-Commercial
非商业
- dtSearch-- This is a library I'm very familiar with. It does a fantastic job, and can parse a ridiculous amount of file formats. However, it costs money and is probably overkill for what you need. It's actually exactlywhat we need, but we're trying to get rid of it ourselves, because we only use it for parsing (it's actually a full-text search engine), and there's plenty of parsing libraries out there that we can use or modify to suit our needs, but it honestly blows all these other libraries out of the water. As I mentioned before, it is also not native .NET code. A C++/CLI wrapper is used to intertop between the DLL and the .NET runtime.
- dtSearch——这是一个我非常熟悉的库。它做得非常出色,并且可以解析数量惊人的文件格式。但是,它需要花钱,而且对于您的需要可能有点过分。它实际上正是我们所需要的,但我们试图自己摆脱它,因为我们只将它用于解析(它实际上是一个全文搜索引擎),并且有很多解析库可供我们使用或修改以满足我们的需求,但老实说,它把所有这些其他库都吹了出来。正如我之前提到的,它也不是本机 .NET 代码。C++/CLI 包装器用于在 DLL 和 .NET 运行时之间互通。
iFilters can be used, and are mentioned in several other SO answers on different questions, but the text you will get back is unstructured. Sometimes it's just bad...unreadable for humans, at least. I believe that iFilters are also deprecated, and depending on license issues, you might not be able to redistribute them.
可以使用 iFilters,并在其他几个关于不同问题的 SO 答案中提到,但您将得到的文本是非结构化的。有时它很糟糕……至少对人类来说是不可读的。我相信 iFilter 也已弃用,并且根据许可问题,您可能无法重新分发它们。
Why did I mention all of those Java libraries? Well, for two reasons. First, there are no free.NET equivalents that come close to the quality of these Java libraries. Secondly, you can use these libraries in .NET (I've personally done this myself with these libraries, so I can at least vouch for that) using IKVM. It's an implementation of Java inside of .NET. Here is a good exampleon using IKVM to convert Tika into a .NET assembly that can be used in your project. Perhaps the scariest thing about IKVM, is that it just works!
为什么我要提到所有这些 Java 库?嗯,有两个原因。首先,没有与这些 Java 库的质量相媲美的免费.NET 等价物。其次,您可以使用IKVM在 .NET 中使用这些库(我亲自使用这些库完成了此操作,因此我至少可以保证这一点)。它是 .NET 内部的 Java 实现。 这是使用 IKVM 将 Tika 转换为可在您的项目中使用的 .NET 程序集的一个很好的示例。也许 IKVM 最可怕的地方在于它可以正常工作!
EDIT: I forgot that the author of that blog had actually posted the code and converted libraries on a github project. So, if you want to quickly check it out, you can do so there. However, it's a much older version of Tika and over a year old. If the results aren't as you expected, I would suggest trying it yourself with the latest version.
编辑:我忘了那个博客的作者实际上已经在github 项目上发布了代码和转换库。所以,如果你想快速检查一下,你可以在那里这样做。但是,它是 Tika 的旧版本并且已经使用了一年多。如果结果不符合您的预期,我建议您自己尝试使用最新版本。
回答by NKamrath
Here's a link to extracting from word document:
这是从word文档中提取的链接:
How to extract text from MS office documents in C#
and for the pdf I would use PDFsharp, it is open source and has some good examples and such on their website:
对于 pdf,我会使用 PDFsharp,它是开源的,并且在他们的网站上有一些很好的例子等:
回答by paparazzo
If you just need text then you can use iFilter. It is not a single product but it is free. iFilter is used to extract the text to support Microsoft Index Service. Search on iFilter .NET C# for examples on how to use it. If you need formatted text then not the right tool. It extracts raw text only with lot of line breaks.
如果您只需要文本,那么您可以使用 iFilter。它不是单一产品,而是免费的。iFilter 用于提取文本以支持 Microsoft 索引服务。在 iFilter .NET C# 上搜索有关如何使用它的示例。如果您需要格式化文本,那么这不是正确的工具。它仅提取带有大量换行符的原始文本。
回答by Md Kamruzzaman Sarker
For text extracting from pdf itextsharpis awesome. it is free and open source.
对于从 pdf 中提取文本,itextsharp非常棒。它是免费和开源的。
to read text from pdf it is very easy using this library.
要从 pdf 读取文本,使用这个库非常容易。
回答by ElvisLives
I would recommend Aspose Total for this. A few years ago I did a project on doing pretty much exactly what you are asking and compared to using the Office Interop stuff between different versions of Office (Prior to the change to XML) Aspose was the most robust library. You will probably have to do some OCR based on what you are talking about too. It's not cheapbut I found their API's pretty solid and it works on most versions of the file types you are asking about. You should be able to use the free trial to see if it will fit for you project. I have no affiliation with Aspose other than that I used their tools in a production environment.
我会为此推荐 Aspose Total。几年前,我做了一个项目,几乎完全符合您的要求,并与在不同版本的 Office 之间使用 Office Interop 进行比较(在更改为 XML 之前)Aspose 是最强大的库。您可能也必须根据您所谈论的内容进行一些 OCR。 它并不便宜,但我发现他们的 API 非常可靠,并且适用于您询问的大多数文件类型版本。您应该能够使用免费试用版来查看它是否适合您的项目。除了我在生产环境中使用他们的工具之外,我与 Aspose 没有任何关系。
回答by Tony Qu
You can take a look at toxy.codeplex.com. Toxy is a pure .NET text extraction framework.
你可以看看toxy.codeplex.com。Toxy 是一个纯 .NET 文本提取框架。
It's very simple to use Toxy. For example, to extract a Excel spreadsheet file called test.xlsx.
使用 Toxy 非常简单。例如,提取名为 test.xlsx 的 Excel 电子表格文件。
ParserContext context = new ParserContext("test.xlsx");
ISpreadsheetParser parser = ParserFactory.CreateSpreadsheet(context);
ToxySpreadsheet ss = parser.Parse();
//then you can start handle the result - a ToxySpreadsheet object

