How to Read a PDF file in C#

By FoxLearn 7/18/2024 3:44:41 AM   8.64K
To read a PDF file using iTextSharp in a C# Windows Forms Application, you'll need to follow these steps.

How to read a pdf file in c#

Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "PdfReader" and then click OK

You need to install the iTextSharp library via NuGet Package Manager in your Visual Studio project by right-clicking on your project select Manage NuGet Packages -> Search itextsharp -> Install

install itextsharp

iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format, allowing you to add PDF functionality to your software projects with ease.

Drag and drop RichTextBox, Button controls form your Visual toolbox on to your form designer, then you can design your form as shown below.

read pdf in c#

Include the necessary namespace at the top of your C# file:

using iTextSharp.text.pdf;

Adding a click event handler to the Open button allows you to read a pdf file, then display data to the RichTextBox control.

// how to read a pdf file in c#
private void btnOpen_Click(object sender, EventArgs e)
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "PDF files|*.pdf", ValidateNames = true, Multiselect = false })
        if (ofd.ShowDialog() == DialogResult.OK)
                iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(ofd.FileName);
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i <= reader.NumberOfPages; i++)
                    //Read page
                    sb.Append(PdfTextExtractor.GetTextFromPage(reader, i));
                richTextBox.Text = sb.ToString();
            catch (Exception ex)
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);

Use PdfReader class to open the PDF file, then iterate through the pages and extract the text content. And don't forget to close the PDF reader after you've finished reading the content.