Click here to Skip to main content
11,412,159 members (62,918 online)
Click here to Skip to main content

Converting PDF to Text in C#

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


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 [].


  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)
      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) {

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.


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

Related information


  • 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)


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
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

AnswerRe: Getting "the invoked member is not supported in a dynamic assembly" exception Pinmemberflodpanter6-Aug-13 4:34 
QuestionHow to convert only range of page from pdf file Pinmembermayur.ce1-Nov-12 3:17 
AnswerRe: How to convert only range of page from pdf file Pinmembercodeproject.ir22-Jul-13 10:48 
QuestionException Pinmemberguton28-Oct-12 8:27 
QuestionCan I retain formatting? PinmemberStealthNinja00720-Aug-12 15:27 
GeneralMy vote of 4 PinmemberSreenath Kalahasti20-Aug-12 8:46 
BugPdf conversion to text is not happening PinmemberMember 86741234-Jul-12 1:34 
GeneralRe: Pdf conversion to text is not happening PinmemberDan Letecky4-Jul-12 11:43 
This seems to describe the situation quite precisely:[^]


> 1. I do get a lot of unsupported/disabled Operation info messages from
> the logger (Appendix A)
> What do they mean for me? some parts not read? Do I have to wory about
> something?

Yes, pdfbox doesn't support every operation yet. Some are seldom, some
are not that important and others will lead to an incomplete rendering
or whatever you try to do with the pdf.

> 2. Sometimes I get problems with corrupted stream (Appendix A) though
> rather seldom ..from files perfectly viewable in Acrobat Reader? I
> assume Reader ist just more error resilent and files has some bugs?

Yes, that assumption is right.
My open-source ASP.NET 2.0 controls:
DayPilot - Outlook-like calendar/scheduling control
DayPilot MonthPicker - Light-weight month picker
MenuPilot - Hover context menu

GeneralRe: Pdf conversion to text is not happening PinmemberMember 86741234-Jul-12 22:11 
QuestionError occuring in PinmemberSteve.Brown2-Jul-12 21:32 
AnswerRe: Error occuring in PinmemberDan Letecky2-Jul-12 22:30 
GeneralRe: Error occuring in PinmemberSteve.Brown2-Jul-12 22:31 
GeneralGetting empty output? [modified] Pinmemberii_noname_ii2-Jul-12 1:03 
GeneralRe: Getting empty output? PinmemberDan Letecky2-Jul-12 22:22 
GeneralRe: Getting empty output? Pinmemberii_noname_ii3-Jul-12 0:40 
QuestionAnother open source useless toy Pinmemberaquant23-May-12 23:48 
GeneralRe: Another open source useless toy [modified] Pinmemberabdurahman ibn hattab29-Jun-12 20:27 
QuestionIs license req for PDFBox PinmemberMember 867094927-Feb-12 3:00 
GeneralMy vote of 5 Pinmembermanoj kumar choubey9-Feb-12 3:31 
QuestionCould not generate text using parseUsingPDFBox function PinmemberMember 771558926-Dec-11 20:40 
AnswerRe: Could not generate text using parseUsingPDFBox function Pinmembercaodinhtuan13-Feb-12 0:28 
QuestionUnable to scrap text from pfd to html Pinmembernileshsmarathe6-Dec-11 0:57 
GeneralMy vote of 1 Pinmemberdavid rahul29-Jun-11 0:45 
AnswerRe: My vote of 1 Pinmemberii_noname_ii2-Jul-12 0:59 
GeneralRead PDF from VB.NET using PDFBox 1.5 Pinmembertusharap21-Apr-11 3:49 

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
Web03 | 2.8.150414.5 | Last Updated 20 Apr 2015
Article Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid