Click here to Skip to main content
14,603,017 members

Converting PDF to Text in C#

Rate this:
4.80 (142 votes)
Please Sign up or sign in to vote.
4.80 (142 votes)
19 Apr 2015CPOL
Parsing PDF files in .NET using PDFBox and IKVM.NET (managed code).

Update

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

February 27, 2014: This article originally described parsing PDF files using PDFBox. It has been extended to include samples for IFilter and iTextSharp.

How to Parse PDF Files

There are several main methods for extracting text from PDF files in .NET:

  • Microsoft IFilter interface and Adobe IFilter implementation.
  • iTextSharp
  • PDFBox

None of these PDF parsing solutions is perfect. We will discuss all these methods below.

1. Parsing PDF using Adobe PDF IFilter

In order to parse PDF files using IFilter interface you need the following:

Sample code:

using IFilter;

// ...

public static string ExtractTextFromPdf(string path) {
  return DefaultParser.Extract(path); 
} 

Download a sample project:

If you are using the PDF IFilter that comes with Adobe Acrobat Reader you will need to rename the process to "filtdump.exe" otherwise the IFilter interface will return E_NOTIMPL error code. See more at Parsing PDF Files using IFilter [squarepdf.net].

Disadvantages:

  1. Using unreliable COM interop that handles IFilter interface (and the combination of IFilter COM and Adobe PDF IFilter is especially troublesome).
  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.
  3. You have to use "filtdump.exe" file name for your application with the latest PDF IFilter implementation that comes with Acrobat Reader.

2. Parsing PDF 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 it supports extracting text from PDF as well.

Sample code:

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

// ...
 
public static string ExtractTextFromPdf(string path)
{
  using (PdfReader reader = new PdfReader(path))
  {
    StringBuilder text = new StringBuilder();

    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
    }

    return text.ToString();
  }
} 

Credit: Member 10364982

Download a sample project:

You may consider using LocationTextExtractionStrategy to get better precision.

public static string ExtractTextFromPdf(string path)
{
  ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
  
  using (PdfReader reader = new PdfReader(path))
  {
      StringBuilder text = new StringBuilder();

      for (int i = 1; i <= reader.NumberOfPages; i++)
      {
          string thePage = PdfTextExtractor.GetTextFromPage(reader, i, its);
          string[] theLines = thePage.Split('\n');
          foreach (var theLine in theLines)
          {
              text.AppendLine(theLine);
          }
      }
      return text.ToString();
  }
}  

 

Credit: Member 10140900

Disadvantages of iTextSharp:

  1. Licensing if you are not happy with AGPL license

3. Parsing PDF using 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.8.9.dll

and copying the following files the bin directory:

  • commons-logging.dll
  • fontbox-1.8.9.dll
  • IKVM.OpenJDK.Text.dll
  • IKVM.OpenJDK.Util.dll
  • IKVM.Runtime.dll

Using the PDFBox to parse PDFs is fairly easy:

using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;

// ...

private static string ExtractTextFromPdf(string path)
{
  PDDocument doc = null;
  try {
    doc = PDDocument.load(path)
    PDFTextStripper stripper = new PDFTextStripper();
    return stripper.getText(doc);
  }
  finally {
    if (doc != null) {
      doc.close();
    }
  }
}  

Download a sample project:

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.8.9.dll (4 MB)
  • commons-logging.dll (82 kB)
  • fontbox-1.8.9.dll (180 kB)
  • IKVM.OpenJDK.Text.dll (800 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.

Thanks to bobrien100 for improvements suggestions.

Disadvantages:

  1. IKVM.NET Dependencies (18 MB)
  2. Speed (especially the IKVM.NET warm-up time)

Related information

History

  • April 20, 2015 - Updated to work with the latest PDFBox release (1.8.9)
  • November 27, 2014 - Updated to work with the latest PDFBox release (1.8.7)
  • March 10, 2014 - IFilter file name limitations added, iTextSharp sample extended
  • February 27, 2014 - Samples for IFilter and iTextSharp added.
  • February 24, 2014 - Updated to work with the latest PDFBox release (1.8.4)
  • June 20, 2012 - Updated to work with the latest PDFBox release (1.7.0)

License

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

Share

About the Author

Dan Letecky
Czech Republic Czech Republic

Comments and Discussions

 
GeneralRe: Another open source useless toy Pin
abdurahman ibn hattab29-Jun-12 19:27
Memberabdurahman ibn hattab29-Jun-12 19:27 
QuestionIs license req for PDFBox Pin
Member 867094927-Feb-12 2:00
MemberMember 867094927-Feb-12 2:00 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey9-Feb-12 2:31
professionalManoj Kumar Choubey9-Feb-12 2:31 
QuestionCould not generate text using parseUsingPDFBox function Pin
Member 771558926-Dec-11 19:40
MemberMember 771558926-Dec-11 19:40 
AnswerRe: Could not generate text using parseUsingPDFBox function Pin
caodinhtuan12-Feb-12 23:28
Membercaodinhtuan12-Feb-12 23:28 
QuestionUnable to scrap text from pfd to html Pin
nileshsmarathe5-Dec-11 23:57
Membernileshsmarathe5-Dec-11 23:57 
GeneralMy vote of 1 Pin
david rahul28-Jun-11 23:45
Memberdavid rahul28-Jun-11 23:45 
AnswerRe: My vote of 1 Pin
ii_noname_ii1-Jul-12 23:59
Memberii_noname_ii1-Jul-12 23:59 
Open the sln file in notepad:
change top to:
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008

...And that's a super advanced conversion for you... lol..
GeneralRead PDF from VB.NET using PDFBox 1.5 Pin
tusharap21-Apr-11 2:49
Membertusharap21-Apr-11 2:49 
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pin
Ponzano Paolo19-May-11 20:52
MemberPonzano Paolo19-May-11 20:52 
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pin
tusharap10-Jun-11 13:51
Membertusharap10-Jun-11 13:51 
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pin
InDirk14-Jun-11 2:16
MemberInDirk14-Jun-11 2:16 
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pin
Member 771959310-Jul-11 11:33
MemberMember 771959310-Jul-11 11:33 
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pin
gigi_bujie25-Jul-11 10:41
Membergigi_bujie25-Jul-11 10:41 
GeneralPDFBox 1.6.0 .net Pin
Soren.Persian7-Sep-11 9:15
MemberSoren.Persian7-Sep-11 9:15 
GeneralRe: Read PDF from VB.NET using PDFBox 1.5 Pin
preethipoornima18-Oct-11 22:58
Memberpreethipoornima18-Oct-11 22:58 
Generalreference FontBox-0.1.0-dev.dll Pin
Kubin30-Mar-11 5:30
MemberKubin30-Mar-11 5:30 
GeneralRe: reference FontBox-0.1.0-dev.dll Pin
jschaenzle9-Aug-11 14:13
Memberjschaenzle9-Aug-11 14:13 
GeneralMy vote of 5 Pin
omkar jante14-Mar-11 22:51
Memberomkar jante14-Mar-11 22:51 
QuestionGetting "Object reference not set to an instance of an object." Pin
evergreen50113-Mar-11 19:32
Memberevergreen50113-Mar-11 19:32 
AnswerRe: Getting "Object reference not set to an instance of an object." Pin
JoseLuis1126-Apr-11 13:55
MemberJoseLuis1126-Apr-11 13:55 
AnswerRe: Getting "Object reference not set to an instance of an object." Pin
JoseLuis112-May-11 8:07
MemberJoseLuis112-May-11 8:07 
GeneralRe: Getting "Object reference not set to an instance of an object." Pin
Marcio Suenaga15-Jun-11 8:39
MemberMarcio Suenaga15-Jun-11 8:39 
Generalthank you Pin
seeed197710-Mar-11 20:16
Memberseeed197710-Mar-11 20:16 
QuestionHow to get only few field values from PDF document using the PDFBox in c# Pin
aurosish14-Feb-11 20:08
Memberaurosish14-Feb-11 20:08 

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.

Article
Posted 1 Dec 2005

Stats

1.6M views
30.4K downloads
482 bookmarked