Click here to Skip to main content
6,596,602 members and growing! (19,654 online)
Email Password   helpLost your password?
General Programming » String handling » Strings     Intermediate

Converting PDF to Text in C#

By Dan Letecky

Parsing PDF files in .NET using PDFBox and IKVM.NET (managed code).
C#, Windows, .NET, Visual Studio, Dev
Posted:1 Dec 2005
Updated:12 Dec 2005
Views:184,866
Bookmarked:165 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
52 votes for this article.
Popularity: 7.42 Rating: 4.32 out of 5
4 votes, 7.7%
1
1 vote, 1.9%
2
3 votes, 5.8%
3
5 votes, 9.6%
4
39 votes, 75.0%
5

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


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
Location: Czech Republic Czech Republic

Other popular String handling articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 110 (Total in Forum: 110) (Refresh)FirstPrevNext
GeneralPDFBox in trust level = medium Pinmemberfnajib4:02 6 Nov '09  
Questionproblem with Hebrew PinmemberJY16:19 19 Oct '09  
GeneralExtracting Text and Images Pinmemberangstrey4:53 19 Oct '09  
GeneralConvert PDF to Text Pinmemberzelandiya17:07 13 Oct '09  
GeneralMy vote of 1 Pinmemberbabu93:39 2 Oct '09  
GeneralSystem Null Reference Pinmemberwopfather118:15 22 Sep '09  
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  
Generalhow i can convert pdf to HTML in C# ?? Pinmemberre7et_3esh2:48 2 Jul '09  
GeneralDocument is not getting loaded [modified] PinmembersanjivaniVB0:44 9 Jun '09  
GeneralRead PDF file in C# with Images PinmemberSaurabh_ClearDevelop22:56 26 May '09  
QuestionExtracting tables data from PDF file ? PinmemberHanan Harush10:51 21 Mar '09  
GeneralThe type or namespace name 'GNU' does not exist in the namespace 'IKVM' Pinmemberm09651017:30 2 Feb '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  
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  
GeneralCan 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  
QuestionNot working when convertring gujarati langugage PinmemberAmitsp0:37 25 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Dec 2005
Editor: Rinish Biju
Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project