Click here to Skip to main content
Licence 
First Posted 1 Dec 2005
Views 399,260
Downloads 6,828
Bookmarked 232 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.9%
1
1 vote, 1.4%
2
3 votes, 4.2%
3
12 votes, 16.9%
4
48 votes, 67.6%
5
4.67/5 - 71 votes
8 removed
μ 4.29, σa 2.22 [?]

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
QuestionCould not generate text using parseUsingPDFBox function PinmemberMember 771558920:40 26 Dec '11  
QuestionUnable to scrap text from pfd to html Pinmembernileshsmarathe0:57 6 Dec '11  
GeneralMy vote of 1 Pinmemberdavid rahul0:45 29 Jun '11  
GeneralRead PDF from VB.NET using PDFBox 1.5 Pinmembertusharap3:49 21 Apr '11  
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 PinmemberPonzano Paolo21:52 19 May '11  
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pinmembertusharap14:51 10 Jun '11  
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 PinmemberMember 79252623:16 14 Jun '11  
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 PinmemberMember 771959312:33 10 Jul '11  
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pinmembergigi_bujie11:41 25 Jul '11  
GeneralPDFBox 1.6.0 .net PinmemberSoren.Persian10:15 7 Sep '11  
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pinmemberpreethipoornima23:58 18 Oct '11  
Generalreference FontBox-0.1.0-dev.dll PinmemberKubin6:30 30 Mar '11  
GeneralRe: reference FontBox-0.1.0-dev.dll Pinmemberjschaenzle15:13 9 Aug '11  
GeneralMy vote of 5 Pinmemberomkar jante23:51 14 Mar '11  
QuestionGetting "Object reference not set to an instance of an object." Pinmemberevergreen50120:32 13 Mar '11  
AnswerRe: Getting "Object reference not set to an instance of an object." PinmemberMember 787434914:55 26 Apr '11  
AnswerRe: Getting "Object reference not set to an instance of an object." PinmemberJoseLuis119:07 2 May '11  
GeneralRe: Getting "Object reference not set to an instance of an object." PinmemberMarcio Suenaga9:39 15 Jun '11  
Generalthank you Pinmemberseeed197721:16 10 Mar '11  
QuestionHow to get only few field values from PDF document using the PDFBox in c# Pinmemberaurosish21:08 14 Feb '11  
GeneralMy vote of 5 PinmemberIsbjorn13:26 27 Jan '11  
QuestionFile not found issue PinmemberchrisGwise1:40 9 Dec '10  
AnswerRe: File not found issue PinmemberIsbjorn13:17 27 Jan '11  
GeneralMy vote of 1 Pinmemberjaganarv1:17 9 Dec '10  
GeneralMy vote of 1 PinmemberMember 42372272:13 1 Nov '10  

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
Web02 | 2.5.120206.1 | Last Updated 12 Dec 2005
Article Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid