Warning
June 29, 2012: It turns out that although the IKVM.NET bridge brings some overhead this is still one of the best ways to parse PDF files in .NET.
The article and the Visual Studio project are updated and work with the latest PDFBox version (1.7.0). It's also possible to download the project with all dependencies (resolving the dependencies proved to be a bit tricky).
How to parse PDF files
When extending the indexing solution for an intranet built using the Lucene.NET library I decided to add support for PDF files. But DotLucene can only handle plain text so the PDF files had to be converted.
After hours of Googling I found a reasonable solution that uses "pure" .NET - at least there are no other dependencies than a few IKVM.NET assemblies. Before we start with the solution let's take a look at the other ways I tried.
Using Adobe PDF IFilter
Using Adobe PDF IFilter requires:
- Using unreliable COM interop that handles IFilter interface (and the combination of IFilter COM and Adobe PDF IFilter is especially troublesome) and
- A separate installation of Adobe IFilter on the target system. This can be painful if you need to distribute your indexing solution to someone else.
Using iTextSharp
iTextSharp is a .NET port of iText, a PDF manipulation library for Java. It is primarily focused on creating and not reading PDFs but there are some classes that allow you to read PDF - especially PdfReader. But extracting the text from the hierarchy of objects is not an easy task (PDF is not a simple format, the PDF Reference is 7 MB - compressed - PDF file). I was able to get to PdfArray, PdfBoolean, PdfDictionary and other objects but after some hours of trying to resolve PdfIndirectReference I gave up and threw away the iTextSharp based parser.
Finally: PDFBox
PDFBox is another Java PDF library. It is also ready to be used with the original Java Lucene (see LucenePDFDocument).
Fortunately, there is a .NET version of PDFBox that is created using IKVM.NET (just download the PDFBox package).
Using PDFBox in .NET requires adding references to:
- IKVM.OpenJDK.Core.dll
- IKVM.OpenJDK.SwingAWT.dll
- pdfbox-1.7.0.dll
and copying the following files the bin directory:
- commons-logging.dll
- fontbox-1.7.0.dll
- IKVM.OpenJDK.Util.dll
- IKVM.Runtime.dll
Using the PDFBox to parse PDFs is fairly easy:
private static string parseUsingPDFBox(string filename)
{
PDDocument doc = PDDocument.load(filename);
PDFTextStripper stripper = new PDFTextStripper();
string text = stripper.getText(doc);
doc.close();
return text;
}
The size of the required assemblies adds up to almost 18 MB:
- IKVM.OpenJDK.Core.dll (4 MB)
- IKVM.OpenJDK.SwingAWT.dll (6 MB)
- pdfbox-1.7.0.dll (4 MB)
- commons-logging.dll (82 kB)
- fontbox-1.7.0.dll (180 kB)
- IKVM.OpenJDK.Util.dll (2 MB)
- IKVM.Runtime.dll (1 MB)
The speed is not so bad: Parsing the U.S. Copyright Act PDF (5.1 MB) took about 13 seconds.
Related information
History
- June 20, 2012 - Updated to work with the latest PDFBox release