Click here to Skip to main content
15,908,437 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 975.3K   120.5K   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

 
QuestionToo simplistic - why I voted 1 Pin
atlaste10-Jul-13 22:28
atlaste10-Jul-13 22:28 
Sorry to be the bearer of bad news for all the good intentions. I know the PDF standard quite well, even implemented it a couple of times, and to be honest this is *not* what you want. Extracting text from PDF is a *very hard* thing to implement, not something to take lightly. To name a few issues with this code:

- Different styles of encodings are not supported; it's not just ascii out there!
- Unicode cmaps are not implemented; you'll get jibberish, lots of it
- Different ways to encode strings are not supported
- Characters are positioned absolutely in PDF; you cannot just grab them and hope you end up with text, you need some type of OCR-like text merging

... I can go on for quite a while here ... Yes, there's a reason all implementations are megabytes in size.

If you really want to extract text from PDF, read the standard and then if you're still up to it, start coding. Then download a couple of 1000 pdf's from the internet, see all your code go to hell, fix all the issues and go on. This simplistic "solution" will just give you lots and lots of bad and unpredictable results.
AnswerRe: Too simplistic - why I voted 1 Pin
Perry Orr7-Aug-13 17:22
Perry Orr7-Aug-13 17:22 
GeneralRe: Too simplistic - why I voted 1 Pin
atlaste2-Mar-14 21:30
atlaste2-Mar-14 21:30 
QuestionHow it work!? Pin
reza2168116-Apr-13 6:51
reza2168116-Apr-13 6:51 
Questionlayout Pin
tmac1210-Mar-13 23:32
tmac1210-Mar-13 23:32 
QuestionThank you! Pin
Joseph guidry8-Jan-13 9:15
Joseph guidry8-Jan-13 9:15 
BugFound bug Pin
MunissoR24-Apr-12 1:57
MunissoR24-Apr-12 1:57 
AnswerRe: Found bug Pin
fborelli4-Jul-12 8:33
fborelli4-Jul-12 8:33 
GeneralGreat Post, Works Great! Pin
Member 202264516-Apr-12 5:13
Member 202264516-Apr-12 5:13 
QuestionGreat post! Pin
Eric Castellon9-Apr-12 9:35
Eric Castellon9-Apr-12 9:35 
GeneralMy vote of 5 Pin
brinda roy20-Feb-12 23:41
brinda roy20-Feb-12 23:41 
GeneralMy vote of 1 Pin
mjkhan78620-Jan-12 21:35
mjkhan78620-Jan-12 21:35 
Questionhow to export data from excel to PDF ? Pin
nimolZero28-Aug-11 6:10
nimolZero28-Aug-11 6:10 
Questionnot work Pin
cutithongtin1-Aug-11 14:45
cutithongtin1-Aug-11 14:45 
QuestionDosn't work. Pin
sasirekam29-Jun-11 19:13
sasirekam29-Jun-11 19:13 
GeneralAlternate Solution Pin
kaaskop7-May-11 3:44
kaaskop7-May-11 3:44 
GeneralRe: Alternate Solution Pin
Wizdave052-Feb-12 9:04
Wizdave052-Feb-12 9:04 
GeneralRe: Alternate Solution Pin
Member 864124213-Feb-12 15:45
Member 864124213-Feb-12 15:45 
GeneralRe: Alternate Solution Pin
Member 909494814-Aug-12 13:45
Member 909494814-Aug-12 13:45 
GeneralRe: Alternate Solution Pin
James Henze29-Nov-13 5:39
James Henze29-Nov-13 5:39 
General(Solved) Error when reading some document (page missing) Pin
Lord TaGoH8-Apr-11 0:13
Lord TaGoH8-Apr-11 0:13 
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 

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.