Click here to Skip to main content
11,492,907 members (61,686 online)
Click here to Skip to main content

Converting PDF to Text in C#

, 19 Apr 2015 CPOL 1.6M 21.6K 448
Rate this:
Please Sign up or sign in to vote.
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
My open-source event calendar/scheduling AJAX controls:

DayPilot for JavaScript/HTML5/jQuery
DayPilot for ASP.NET
DayPilot for MVC
DayPilot for Java

Comments and Discussions

 
QuestionWhat an article boss 5 out of 5 Pin
Arkadeep De24-Apr-15 9:02
professionalArkadeep De24-Apr-15 9:02 
AnswerRe: What an article boss 5 out of 5 Pin
Dan Letecky24-Apr-15 9:39
memberDan Letecky24-Apr-15 9:39 
QuestionFull trust Pin
nicolaz021-Apr-15 22:30
membernicolaz021-Apr-15 22:30 
AnswerRe: Full trust Pin
Dan Letecky24-Apr-15 9:40
memberDan Letecky24-Apr-15 9:40 
GeneralMy vote of 5 Pin
richihu21-Apr-15 1:29
memberrichihu21-Apr-15 1:29 
GeneralRe: My vote of 5 Pin
Dan Letecky24-Apr-15 9:41
memberDan Letecky24-Apr-15 9:41 
QuestionNon Unicode Characters Pin
v_farmak20-Apr-15 23:45
memberv_farmak20-Apr-15 23:45 
AnswerRe: Non Unicode Characters Pin
Dan Letecky24-Apr-15 9:48
memberDan Letecky24-Apr-15 9:48 
GeneralMy vote of 5 Pin
Mladen Borojevic28-Mar-15 4:42
memberMladen Borojevic28-Mar-15 4:42 
GeneralRe: My vote of 5 Pin
Dan Letecky10-Apr-15 21:05
memberDan Letecky10-Apr-15 21:05 
GeneralMy vote of 5 Pin
ChenShaoHsi1-Dec-14 22:46
memberChenShaoHsi1-Dec-14 22:46 
GeneralRe: My vote of 5 Pin
Dan Letecky1-Dec-14 22:58
memberDan Letecky1-Dec-14 22:58 
GeneralMy vote of 5 Pin
csharpbd27-Nov-14 4:36
membercsharpbd27-Nov-14 4:36 
GeneralRe: My vote of 5 Pin
Dan Letecky27-Nov-14 5:27
memberDan Letecky27-Nov-14 5:27 
QuestionSystem.IO exception: A required privilege is not held by the client error Pin
jackthomson25-May-14 2:32
memberjackthomson25-May-14 2:32 
QuestioniTextSharp to Text - return duplicate content - text or htmle format pdf file? Pin
Hal Xie9-Apr-14 21:33
memberHal Xie9-Apr-14 21:33 
QuestionPDF IFilter Pin
Member 101587313-Apr-14 13:39
memberMember 101587313-Apr-14 13:39 
QuestionMissing dependency error Pin
sakushi632-Apr-14 18:32
membersakushi632-Apr-14 18:32 
SuggestionFree (GNU) xpdf library to extract text Pin
Jepy11-Mar-14 6:54
memberJepy11-Mar-14 6:54 
QuestionPDF Box Special Character Pin
Member 1041359810-Mar-14 3:30
memberMember 1041359810-Mar-14 3:30 
AnswerRe: PDF Box Special Character Pin
Dan Letecky10-Mar-14 9:07
memberDan Letecky10-Mar-14 9:07 
GeneralRe: PDF Box Special Character [modified] Pin
Member 1041359819-Mar-14 1:35
memberMember 1041359819-Mar-14 1:35 
QuestioniTextSharp suggestion and problem Pin
B. Clay Shannon3-Mar-14 10:01
professionalB. Clay Shannon3-Mar-14 10:01 
AnswerRe: iTextSharp suggestion and problem Pin
Dan Letecky3-Mar-14 10:25
memberDan Letecky3-Mar-14 10:25 
GeneralRe: iTextSharp suggestion and problem Pin
B. Clay Shannon3-Mar-14 10:33
professionalB. Clay Shannon3-Mar-14 10:33 
GeneralRe: iTextSharp suggestion and problem Pin
Dan Letecky3-Mar-14 11:13
memberDan Letecky3-Mar-14 11:13 
GeneralRe: iTextSharp suggestion and problem Pin
Member 101409004-Mar-14 12:43
memberMember 101409004-Mar-14 12:43 
GeneralRe: iTextSharp suggestion and problem Pin
Dan Letecky10-Mar-14 2:23
memberDan Letecky10-Mar-14 2:23 
QuestionThe sample code Pin
bobrien10026-Feb-14 11:08
memberbobrien10026-Feb-14 11:08 
AnswerRe: The sample code Pin
Dan Letecky27-Feb-14 0:10
memberDan Letecky27-Feb-14 0:10 
QuestioniTextSharp Pin
Member 1036498225-Feb-14 23:28
memberMember 1036498225-Feb-14 23:28 
AnswerRe: iTextSharp Pin
Dan Letecky27-Feb-14 0:10
memberDan Letecky27-Feb-14 0:10 
AnswerRe: iTextSharp Pin
B. Clay Shannon3-Mar-14 10:02
professionalB. Clay Shannon3-Mar-14 10:02 
GeneralRe: iTextSharp Pin
Member 103649823-Mar-14 23:46
memberMember 103649823-Mar-14 23:46 
QuestionMy Vote of 4 Pin
TheFigmo14-Nov-13 6:17
memberTheFigmo14-Nov-13 6:17 
AnswerRe: My Vote of 4 Pin
Dan Letecky1-Dec-13 22:24
memberDan Letecky1-Dec-13 22:24 
QuestionThanks Pin
_WinBase_14-Nov-13 3:26
member_WinBase_14-Nov-13 3:26 
AnswerRe: Thanks Pin
Dan Letecky1-Dec-13 22:24
memberDan Letecky1-Dec-13 22:24 
Questionyou can also try this one! Pin
jacaboo22-Aug-13 17:27
memberjacaboo22-Aug-13 17:27 
GeneralMy vote of 5 Pin
fredatcodeproject13-Aug-13 23:09
memberfredatcodeproject13-Aug-13 23:09 
GeneralRe: My vote of 5 Pin
Dan Letecky1-Dec-13 22:23
memberDan Letecky1-Dec-13 22:23 
Questionshare my method Pin
danny rough8-May-13 18:03
memberdanny rough8-May-13 18:03 
GeneralRe: share my method Pin
codeproject.ir26-Jul-13 11:09
membercodeproject.ir26-Jul-13 11:09 
QuestionGreek Character Pin
Weslley29-Apr-13 23:38
memberWeslley29-Apr-13 23:38 
QuestionNeed some info Pin
Tridip Bhattacharjee26-Mar-13 5:56
memberTridip Bhattacharjee26-Mar-13 5:56 
AnswerRe: Need some info Pin
Dan Letecky26-Mar-13 10:14
memberDan Letecky26-Mar-13 10:14 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun15-Mar-13 0:38
memberHumayun Kabir Mamun15-Mar-13 0:38 
GeneralRe: My vote of 5 Pin
Dan Letecky26-Mar-13 10:15
memberDan Letecky26-Mar-13 10:15 
Questionretain format? Pin
Kalpana Volety11-Jan-13 8:22
memberKalpana Volety11-Jan-13 8:22 
SuggestionPdf to text conversion in c# Pin
HighCommand18-Dec-12 9:22
memberHighCommand18-Dec-12 9:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 20 Apr 2015
Article Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid