15

I'd need a .NET library so that using which I can extract text data from PDF, Excel and Word files.

Ideally, a free tool!

Would you recommend any?

many thanks,

The Light
  • 26,341
  • 62
  • 176
  • 258
  • 1
    possible duplicate of [PDF Reader](http://stackoverflow.com/questions/905683/pdf-reader) – cdhowie Jun 11 '12 at 14:34
  • 2
    It's not quite a duplicate. It asks about one tool performing pdf, excel and word extraction. – The Light Jun 11 '12 at 14:36
  • I did search and found some but not very good...Isn't it better to have one tool able to extract the 3 file types? – The Light Jun 11 '12 at 14:42
  • 1
    To paraphrase the Unix philosophy, "write [libraries] that do one thing and do it well." Would you rather use one excellent PDF reader library and one excellent Word reader library, or a half-assed library that does both? (See also, "if you try to do everything you will accomplish nothing.") – cdhowie Jun 11 '12 at 14:44
  • an Excellent PDF/Word/Excel Reader; be positive ;). yeah, in practice, I'll have to use separate libraries perhaps but one could create one library handling all 3 excellently e.g. TextExtractionGod! I just gave you a good business/product idea ;) – The Light Jun 11 '12 at 14:49
  • Possible? Sure, anything is possible. However, if writing a general text-extraction library, I would expect that library to further delegate to other libraries for the actual parsing and inspection of the documents. – cdhowie Jun 11 '12 at 14:52
  • @cdhowie, For sure internally it can use whatever it desires but externally the end developer can simply pass the strategy/document type and extract the contents; making the life much easier. – The Light Jun 11 '12 at 14:53

6 Answers6

32

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 all of 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.

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 question has 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 months ago. 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.)

  • POI or 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 everything text 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 content from many different kinds of files, using existing parsing libraries. It actually uses POI and PdfBox under the hood for office and PDF documents.

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 exactly what 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.

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.


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 example on 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!

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.

Community
  • 1
  • 1
Christopher Currens
  • 29,917
  • 5
  • 57
  • 77
  • Worth mentioning the Open XML only works for the x version (e.g. xdoc). Even an Office 2010 saved as a native .doc will not work. To my knowledge. – paparazzo Jun 11 '12 at 17:28
  • @Blam, that's correct. I mentioned it as only being able to read/modify office 2007+ documents, meaning only the open xml formats. – Christopher Currens Jun 11 '12 at 17:52
  • Hey nice one! any idea on how snowtide and latest ghostscript works? yes, there are many SO posts, but you seemed to have a great level of research.. – Dexters Jul 19 '13 at 02:37
  • Check out new library, Apitron PDF Kit, able to extract any formatted text. – Hugo Moreno Jan 08 '15 at 10:02
  • 1
    Tika on dot net is available through NuGet: https://www.nuget.org/packages/TikaOnDotNet/ Works great in my initial tests, super easy to use, and supports a wide variety of file formats. Pretty impressive. – David Hammond Apr 09 '15 at 22:11
7

You can take a look at toxy.codeplex.com. Toxy is a pure .NET text extraction framework.

It's very simple to use Toxy. For example, to extract a Excel spreadsheet file called test.xlsx.

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
Tony Qu
  • 676
  • 8
  • 14
2

Here's a link to extracting from word document:

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:

http://pdfsharp.com/PDFsharp/

Community
  • 1
  • 1
NKamrath
  • 536
  • 3
  • 9
  • PDFSharp doesn't seem to be so great in extracting text: http://stackoverflow.com/questions/9144640/alter-pdf-text-repositioning/9161732#9161732 – The Light Jun 11 '12 at 15:08
  • It works just fine, just need to do some text parsing...via the response link in the post you referenced via the forum for pdfsharp which has great support which I referenced. – NKamrath Jun 11 '12 at 16:07
1

For text extracting from pdf itextsharp is awesome. it is free and open source.

to read text from pdf it is very easy using this library.

Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
  • 1
    itextsharp is not entirely free to use in commercial applications: http://itextpdf.com/terms-of-use/index.php or http://itextpdf.com/summit.php#talk9 – The Light Jun 11 '12 at 15:44
  • @TheLight - Version 4.1.6 and earlier of iTextSharp are licensed under the LGPL and can be used freely in commercial applications. – Christopher Currens Jun 11 '12 at 16:08
1

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 cheap but 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

ElvisLives
  • 2,275
  • 2
  • 18
  • 24
0

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.

paparazzo
  • 44,497
  • 23
  • 105
  • 176