5,702,921 members and growing! (20,744 online)
Email Password   helpLost your password?
Languages » C# » General     Beginner License: The Code Project Open License (CPOL)

Extract Text from PDF in C# (100% .NET)

By Zollor

A simple class to extract plain text from PDF documents with ITextSharp
C#, Windows, .NET, Visual Studio, Dev

Posted: 20 May 2006
Updated: 20 May 2006
Views: 49,827
Bookmarked: 49 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 3.92 Rating: 3.52 out of 5
3 votes, 23.1%
1
1 vote, 7.7%
2
1 vote, 7.7%
3
2 votes, 15.4%
4
6 votes, 46.2%
5

Introduction

This is a 100% .NET solution to extract text from PDF documents.

Background

Dan Letecky posted a nice code on how to extract text from PDF documents in C# based on PDFBox. Although his solution works well it has a drawback, the size of the required additional libraries is almost 16 MB. Using iTextSharp the size of required additional libraries is only 2.3 MB.

Using the Code

In order to use this solution in your projects, you need to do the following steps:

  • Add references to itextsharp.dll and SharpZiplib.dll
  • Add the PDFParser.cs class to your project

Then you can use the newly added class in the following way:

// create an instance of the pdfparser class
PDFParser pdfParser = new PDFParser();
   
// extract the text
String result = pdfParser.ExtractText(pdfFile);

I also created a small console application which uses the class and shows the progress of the conversion. Please keep in mind that if you try to extract text from big PDF files, keeping all the resultant text in memory is not the best solution, in these cases you should write the extracted text to the file after parsing every page.

How Is It Working?

My code is based on the algorithm in C ExtractPDFText. Using iTextSharp's PdfReader class to extract the deflated content of every page, I use a simple function ExtractTextFromPDFBytes to extract the text contents from the deflated page.

Further Improvements

Although the code worked well for me, I didn't find in Adobe's PDF reference how to parse special characters. So if someone knows how to do this, just post it and I will update the class.

History

  • 20th May, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Zollor



Occupation: Web Developer
Location: Romania Romania

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralIt does not workmemberRam.Cse11:48 3 Nov '08  
GeneralText runs all togethermemberMember 350908010:20 10 Sep '08  
RantDoesn't Work.. off to the PDFBox versionmemberMichaelSimons7:22 28 Aug '08  
Questionproblem making it workmemberMember 38795026:42 1 Jul '08  
GeneralFree Text Mining Tool that can convert PDF files to textmemberVitaliy Petrenko23:19 23 Nov '07  
GeneralRe: Free Text Mining Tool that can convert PDF files to textmemberblackjack21500:29 25 Feb '08  
Questionany improvements / alternative 100% .NET solutions?membercwenger0514:04 19 Nov '07  
GeneralBug Fix - Error reading document \ Index out of bounds errormemberwww.kilon.co.uk4:50 21 Aug '07  
GeneralUnable to pull footer informationmemberPorter36:20 6 Aug '07  
Questionthe pdf of the Complex-Chinese edition can't extract the text-file...Help!!memberPoChungLi17:16 12 Jul '07  
GeneralUsing this in a web applicationmemberBaxterBressler3:46 22 May '07  
QuestionErrormembersrochford@ardrua.com10:29 16 Apr '07  
GeneralError with some pdf'smembergodsvision354:54 27 Nov '06  
GeneralDoes not extract any text with some pdf, but pdfbox canmemberpetoulachi3:58 23 May '06  
GeneralRe: Does not extract any text with some pdf, but pdfbox canmemberrajaher1:54 14 Sep '06  
GeneralRe: Does not extract any text with some pdf, but pdfbox canmemberWobba6:06 8 Mar '08  
GeneralDoesn't extract all textmemberKevin Whitefoot3:50 23 May '06  
GeneralRe: Doesn't extract all textmemberManuel__833:31 10 Oct '06  
Generalnot supporting non-ASCII charactersmemberUnruled Boy17:08 21 May '06  
AnswerRe: not supporting non-ASCII charactersmemberpetoulachi4:51 22 May '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 20 May 2006
Editor: Deeksha Shenoy
Copyright 2006 by Zollor
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project