Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Article

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

Rate me:
Please Sign up or sign in to vote.
3.67/5 (60 votes)
20 May 2006CPOL1 min read 967.7K   120.4K   174   106
A simple class to extract plain text from PDF documents with ITextSharp

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:

C#
// 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)


Written By
Web Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDoesn't work Pin
Hale McBraske17-Aug-10 10:35
Hale McBraske17-Aug-10 10:35 
GeneralMy vote of 1 Pin
mycode.mycode@rocketmail.com13-Aug-10 21:34
mycode.mycode@rocketmail.com13-Aug-10 21:34 
GeneralMy vote of 3 Pin
DotnetSniper9-Aug-10 19:23
DotnetSniper9-Aug-10 19:23 
QuestionHow do i port this console application to .net web application? Pin
Fabrizio R.5-May-10 9:21
Fabrizio R.5-May-10 9:21 
GeneralIt works only english text language but not work with the bengali or hindi or chinese or any other language. Pin
zqonline15-Mar-10 23:04
zqonline15-Mar-10 23:04 
GeneralMore explanation of how it works Pin
Simon P Stevens12-Jan-10 23:38
Simon P Stevens12-Jan-10 23:38 
GeneralVery useful Pin
Roberto Zanardo14-Oct-09 6:29
Roberto Zanardo14-Oct-09 6:29 
GeneralPDFParser vs PDFBox Pin
talbot_c5-Oct-09 19:36
talbot_c5-Oct-09 19:36 
Generaldon't want to show PDFproducer name... Pin
raj2313629-Aug-09 23:29
raj2313629-Aug-09 23:29 
QuestionHOW TO UPDATE IMAGE ON TEXT..... Pin
raj2313629-Aug-09 21:28
raj2313629-Aug-09 21:28 
Generalcould not get all text Pin
chuckdawit27-Jul-09 13:24
chuckdawit27-Jul-09 13:24 
GeneralRe: could not get all text Pin
Marco Tenuti22-Dec-09 7:27
Marco Tenuti22-Dec-09 7:27 
GeneralSupport for non-ASCII solved Pin
MrVeloso19-Jun-09 8:29
MrVeloso19-Jun-09 8:29 
GeneralRe: Support for non-ASCII solved Pin
Zlate8725-Jun-09 11:32
Zlate8725-Jun-09 11:32 
GeneralRe: Support for non-ASCII solved Pin
MrVeloso25-Jun-09 12:16
MrVeloso25-Jun-09 12:16 
GeneralRe: Support for non-ASCII solved Pin
Zlate8725-Jun-09 12:55
Zlate8725-Jun-09 12:55 
GeneralRe: Support for non-ASCII solved Pin
MrVeloso26-Jun-09 1:56
MrVeloso26-Jun-09 1:56 
GeneralRe: Support for non-ASCII solved Pin
Zlate8727-Jun-09 12:50
Zlate8727-Jun-09 12:50 
GeneralDoesn't work for code behind (OCR)... Pin
will_affinity1-Apr-09 11:00
will_affinity1-Apr-09 11:00 
GeneralIt does not work Pin
Ram.Cse3-Nov-08 10:48
Ram.Cse3-Nov-08 10:48 
GeneralText runs all together Pin
navyjax210-Sep-08 9:20
navyjax210-Sep-08 9:20 
GeneralRe: Text runs all together Pin
Chris.Procter30-Aug-10 9:25
Chris.Procter30-Aug-10 9:25 
RantDoesn't Work.. off to the PDFBox version Pin
MichaelSimons28-Aug-08 6:22
MichaelSimons28-Aug-08 6:22 
Questionproblem making it work Pin
icezulu1-Jul-08 5:42
icezulu1-Jul-08 5:42 
AnswerRe: problem making it work Pin
tamagotchi4-Jan-09 9:28
tamagotchi4-Jan-09 9:28 

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

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