Click here to Skip to main content
Licence 
First Posted 1 Dec 2005
Views 401,180
Downloads 6,956
Bookmarked 233 times

Converting PDF to Text in C#

By Dan Letecky | 12 Dec 2005
Parsing PDF files in .NET using PDFBox and IKVM.NET (managed code).
7 votes, 9.7%
1
1 vote, 1.4%
2
3 votes, 4.2%
3
12 votes, 16.7%
4
49 votes, 68.1%
5
4.66/5 - 72 votes
8 removed
μ 4.30, σa 2.21 [?]

How to parse PDF files

While extending the indexing solution for an intranet built using the DotLucene fulltext search 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 other than a few assemblies of IKVM.NET. 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:

  1. Using unreliable COM interop that handles IFilter interface (and the combination of IFilter COM and Adobe PDF IFilter is especially troublesome) and
  2. 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.

Read more about using IFilter in Microsoft Office Documents Parsing.

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 use 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, it's in the bin directory).

Using PDFBox in .NET requires adding references to:

  • PDFBox-0.7.2.dll
  • IKVM.GNU.Classpath

and copying IKVM.Runtime.dll to the bin directory.

Using the PDFBox to parse PDFs is fairly easy:

private static string parseUsingPDFBox(string filename)
{
    PDDocument doc = PDDocument.load(filename);
    PDFTextStripper stripper = new PDFTextStripper();
    return stripper.getText(doc);
}

The size of the required assemblies adds up to almost 16 MB:

  • IKVM.GNU.Classpath.dll (7 MB)
  • IKVM.Runtime.dll (360 kB)
  • PDFBox-0.7.2.dll (8 MB)

The speed is not so bad: Parsing the U.S. Copyright Act PDF (1.4 MB) took about 7 seconds.

Related information

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Dan Letecky



Czech Republic Czech Republic

Member
My open-source ASP.NET 2.0 controls:
 
DayPilot - Outlook-like calendar/scheduling control
DayPilot MonthPicker - Light-weight month picker
MenuPilot - Hover context menu

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: System Null Reference PinmemberVan Trinh 19855:58 23 Dec '09  
GeneralRe: System Null Reference PinmemberSteve Holle10:21 2 Feb '10  
Generalproblem in using ikvm Pinmemberz_gh_n1:43 13 Sep '09  
Generalthe beginner problem wiith text reading Pinmembermaroch448:34 28 Aug '09  
Generalhey bro I have a problem Converting PDF to Text in japanese; any advise? [modified] Pinmembertxdtjpu17:54 20 Aug '09  
GeneralMy vote of 1 Pinmemberubik1:14 22 Jul '09  
Questionhow i can convert pdf to HTML in C# ?? Pinmemberre7et_3esh2:48 2 Jul '09  
hi , i have application elearn system that need to convert PDF to html ??
 
how ??
GeneralDocument is not getting loaded [modified] PinmembersanjivaniVB0:44 9 Jun '09  
GeneralRead PDF file in C# with Images PinmemberSaurabh_ClearDevelop22:56 26 May '09  
GeneralRe: Read PDF file in C# with Images Pinmembereslsys8:03 19 Feb '10  
QuestionExtracting tables data from PDF file ? PinmemberHanan Harush10:51 21 Mar '09  
AnswerRe: Extracting tables data from PDF file ? Pinmemberivanclay3:41 18 Nov '09  
GeneralRe: Extracting tables data from PDF file ? PinmemberVijay Chandra Sekhar Parepalli7:19 9 Jul '10  
GeneralThe type or namespace name 'GNU' does not exist in the namespace 'IKVM' Pinmemberm09651017:30 2 Feb '09  
GeneralRe: The type or namespace name 'GNU' does not exist in the namespace 'IKVM' PinmemberSundararajan8:25 10 Nov '09  
GeneralIs there a way to use this for URL PinmemberMeCode12310:57 30 Jan '09  
GeneralWrappedIOException C# not working; but vb.net is working Pinmembersaif_200619:54 18 Jan '09  
GeneralRe: WrappedIOException C# not working; but vb.net is working Pinmembersaif_20060:51 19 Jan '09  
GeneralRe: its a request Pinmembertkmsabarish21:29 8 Jan '10  
Generalsolution for the type initializer for "java.io.file" threw an exception PinmemberDaniel_Wilson1:01 12 Jan '09  
GeneralRe: solution for the type initializer for "java.io.file" threw an exception PinmemberFlashr3:52 19 Feb '09  
GeneralMy vote of 1 Pinmemberclarkapp18:50 3 Jan '09  
QuestionCan anybody tell me this? Pinmembere40s15:30 27 Dec '08  
GeneralI can't find a PDFBox-0.7.x.dll with a strong name. [modified] Pinmembere40s12:39 26 Dec '08  
GeneralRe: I can't find a PDFBox-0.7.x.dll with a strong name. PinmemberDaniel_Wilson1:11 12 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 12 Dec 2005
Article Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid