Click here to Skip to main content
Click here to Skip to main content

Converting PDF to Text in C#

By , 17 Aug 2012
 

Warning    

June 29, 2012: It turns out that although the IKVM.NET bridge brings some overhead this is still one of the best ways to parse PDF files in .NET.  

The article and the Visual Studio project are updated and work with the latest PDFBox version (1.7.0). It's also possible to download the project with all dependencies (resolving the dependencies proved to be a bit tricky).   

How to parse PDF files     

When extending the indexing solution for an intranet built using the Lucene.NET 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 than a few IKVM.NET assemblies. 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. 

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 be used 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). 

Using PDFBox in .NET requires adding references to:  

  • IKVM.OpenJDK.Core.dll 
  • IKVM.OpenJDK.SwingAWT.dll 
  • pdfbox-1.7.0.dll 

and copying the following files the bin directory: 

  • commons-logging.dll 
  • fontbox-1.7.0.dll 
  • IKVM.OpenJDK.Util.dll 
  • IKVM.Runtime.dll 

Using the PDFBox to parse PDFs is fairly easy: 

private static string parseUsingPDFBox(string filename)
{
    PDDocument doc = PDDocument.load(filename);
    PDFTextStripper stripper = new PDFTextStripper();
    string text = stripper.getText(doc);
    doc.close();
    return text;
}  

The size of the required assemblies adds up to almost 18 MB:

  • IKVM.OpenJDK.Core.dll (4 MB) 
  • IKVM.OpenJDK.SwingAWT.dll (6 MB) 
  • pdfbox-1.7.0.dll  (4 MB)  
  • commons-logging.dll (82 kB)  
  • fontbox-1.7.0.dll (180 kB)  
  • IKVM.OpenJDK.Util.dll (2 MB)  
  • IKVM.Runtime.dll (1 MB) 

The speed is not so bad: Parsing the U.S. Copyright Act PDF (5.1 MB) took about 13 seconds. 

Related information 

History

  • June 20, 2012 - Updated to work with the latest PDFBox release 

License

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

About the Author

Dan Letecky
Czech Republic Czech Republic
Member
My open-source AJAX controls:
 
DayPilot
DayPilot MVC
DayPilot Java
Outlook-Like Calendar/Scheduling Controls

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionshare my methodmemberdanny rough8 May '13 - 17:03 
thanks for sharing your way to convert pdf to text in c# . i have always looked for a suitable software to do that, but failed . you remind me that i can just do it by myself. i am used to be a developer , and i believe there is no software on the internet will both good to use and free. if a good converter is employed , our works will be much more relaxed, i can give you some advices. my friend once recommed this pdf to text converter to me , it can be applied in c#, net, or vb.net. if you don;t want to do it by yourself, you can use this. thanks again for sharing, good luck.
http://www.rasteredge.com/how-to/csharp-imaging/pdf-convert-text/[^]
QuestionGreek CharactermemberWeslley29 Apr '13 - 22:38 
Hi Dan,
 
I need to convert PDF with Greek character. I'm trying use your example, but without sucess. Need I setting something? How can I define it?
 
Other question is: My document have spaces beetween lines or lines in blank. Can I create the text file with this lines in blank?
 
Other question (sorry): I have more on page. The application will convert?
 
Thanks a lot!
QuestionNeed some infomemberTridip Bhattacharjee26 Mar '13 - 4:56 
suppose if there will be many images and tables etc then what will happen?
tbhattacharjee

AnswerRe: Need some infomemberDan Letecky26 Mar '13 - 9:14 
It should extract text from tables but you will need to use OCR to read text from images.
--
My open-source AJAX controls:
DayPilot - Calendar/Scheduling Control for ASP.NET WebForms
DayPilot for MVC - Calendar/Scheduling Control for ASP.NET MVC
DayPilot for Java - Calendar/Scheduling Control for Java

GeneralMy vote of 5memberHumayun Kabir Mamun14 Mar '13 - 23:38 
Very Helpful
GeneralRe: My vote of 5memberDan Letecky26 Mar '13 - 9:15 
Thanks!
--
My open-source AJAX controls:
DayPilot - Calendar/Scheduling Control for ASP.NET WebForms
DayPilot for MVC - Calendar/Scheduling Control for ASP.NET MVC
DayPilot for Java - Calendar/Scheduling Control for Java

Questionretain format?memberKalpana Volety11 Jan '13 - 7:22 
Does it retain the format of the text? For example bold text retained as bold etc.
 
Kalpana Volety
PDF to Text
SuggestionPdf to text conversion in c#memberHighCommand18 Dec '12 - 8:22 
we can also convert pdf to text with free utility. (pdf to text)
 
here is the demonstration
pdf to text in asp.net
QuestionTo know the coordinates of each extracting word.membertjimenez6 Dec '12 - 6:44 
Hello,
First of all, congratulations for the project. Great work !!
I would like to ask you a question:
When we are extracting the text, it is possible to know the coordinates of each word?
I mean knowing the rounding position; for instance: upper left x,y and lower right x,y.
QuestionGetting "the invoked member is not supported in a dynamic assembly" exceptionmemberroyk1231 Nov '12 - 18:14 
I get this exception only on Win 7.
On XP it runs w/o errors.
 

When I run the code from VS with either debug or release - it runs smoothly.
However, when I install my project using a setup project - The pdf2text code fails with the following exception: "the invoked member is not supported in a dynamic assembly"
 
I've added a reference to all of the DLLs that come with pdf2text, and made sure they are also included in the setup project.
 
Any idea?
 
tnx

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 17 Aug 2012
Article Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid