While developing web/console applications in .NET we may need to read pdf file data using C#, so in this article, I am going to provide you a working code sample and step by step intructions to create console app which reads PDF file data and show it in console app, using iTextSharp.

Step 1: Create a new console app in your Visual Studio, by navigating to File-> New -> Project -> Select "Console APP(C#)" from the right pane(you can search it also on top righ search bar) -> Give a name & click OK

Step 2: As we will be using iTextsharp to readh PDF file in C#, let's install iTextSharp in our Console app using Nuget package manager console.

Navigate to Tools->Nuget Package manager -> Select "Manage Nuget packages for solution..."

itext-sharp-pdf-read-csharp-min.png

Step 3: Now, here is our main code which read each line of pdf file using iTextSharp and convert it into string

 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();
            }
        }

So, the complete code in C# for the console app will be as below

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Text;
namespace ReadPDFUsingCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var ExtractedPDFToString 
            = ExtractTextFromPdf(@"C:\Users\CT\Desktop\pdf-sample.pdf");
            Console.WriteLine(ExtractedPDFToString);

        }
        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();
            }
        }

    }
}

Where "C:\Users\CT\Desktop\pdf-sample.pdf" is the location of the sample pdf which we have used, here is the sample pdf screenshot

read-pdf-file-in-csharp-example-min.png

Here is the output of the console app

how-to-read-pdf-file-in-csharp-min.png

That's it, we are done, you can download code sample.