Click here to Skip to main content
15,867,895 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 965.6K   120.3K   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

 
GeneralRe: (Solved) Error when reading some document (page missing) [modified] Pin
JBress22-Jun-11 7:40
JBress22-Jun-11 7:40 
GeneralNew line problem Pin
dejan19dejan194-Jan-11 3:53
dejan19dejan194-Jan-11 3:53 
Generalnot working on scaned image pdf file Pin
gaurav.ipec16-Dec-10 18:52
gaurav.ipec16-Dec-10 18:52 
GeneralRe: not working on scaned image pdf file Pin
dr_csci10-Jan-11 4:07
dr_csci10-Jan-11 4:07 
GeneralMy vote of 5 Pin
stefan_lahnor25-Nov-10 22:54
stefan_lahnor25-Nov-10 22:54 
GeneralMy vote of 1 Pin
Aaron Craft27-Oct-10 7:16
Aaron Craft27-Oct-10 7:16 
AnswerSupport for Unicode strings [modified] Pin
Vasiliy Zverev29-Sep-10 6:25
Vasiliy Zverev29-Sep-10 6:25 
GeneralRe: Support for Unicode strings Pin
gulak28-Jan-11 23:19
gulak28-Jan-11 23:19 
I can't get this work with my PDF documents.

For example PDF which has content like below return very strange characters. Could this be character set problem or something?

BT
308.8 521.8 Td /F1 10 Tf[<1110>7<10>-3<0E>4<12>]TJ
ET
Q
q 0 0 0 rg
BT
344.8 510.4 Td /F1 10 Tf[<2E>4<08>3<01>-3<08>3<16>1<04>2<0B17>-4<0C>3<04>2<18>-5<0C>-17<15>1<15>1<1B>-1<27>-2<02>9<2B>-5<1C>]TJ
ET
Q
q 0 0 0 rg
BT
92.9 489.8 Td /F1 14 Tf[<29>1<0C>-3<07>3<18>-1<04>-1<24>-4<04>-1<04>-1<18>-1<18>-1<06>3<15>-2<27>1<2F>1<02>-2<1B>]TJ
ET
Q
q 0 0 0 rg
BT
92.9 461.7 Td /F1 10 Tf[<0D>3<0E>4<0F>-4<10>-3<1112>]TJ
GeneralRe: Support for Unicode strings Pin
Vasiliy Zverev29-Jan-11 9:02
Vasiliy Zverev29-Jan-11 9:02 
GeneralRe: Support for Unicode strings Pin
gulak31-Jan-11 3:47
gulak31-Jan-11 3:47 
Generaltext position Pin
user37824759-Sep-10 1:51
user37824759-Sep-10 1:51 
Questionchar " - ascii (34) Pin
doomelo30-Aug-10 23:54
doomelo30-Aug-10 23:54 
General[My vote of 2] Techinchal mistake Pin
umairaslam2230-Aug-10 21:52
umairaslam2230-Aug-10 21:52 
GeneralRe: [My vote of 2] Techinchal mistake Pin
mdimad27-Sep-10 10:14
mdimad27-Sep-10 10:14 
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 

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.