Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to read PDF file using iTextSharp in ASP.NET

Author: Rachel Nicole
Download Source Code :

The purpose of this article is to explain how we can read a PDF file in ASP.NET using iTextSharp.

iText is a open source library for creating and manipulating PDF files. iTextSharp is the ASP.NET version of iText. We can use it in ASP.NET to create and manipulate PDF files with ease. It is a fast way of handling PDF files so I am a good fan of iTextSharp. I will write a series of articles on PDF and iTextSharp and show you that how we can manipulate PDF files using iTextSharp. This article is the first of this series and here I will read PDF file using iTextSharp both in C# and VB.NET.

First you need to download iTextSharp, unzip it and include it in your project.
  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual Basic or Visual C#  > ASP.NET Empty Web Site
  4. Add New Item > Web Form
  5. Add Reference > Browse > iTextSharp.dll
  6. Add following controls in Web Form

    Please select a PDF file to upload and read:
    <br />
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Read PDF" onclick="Button1_Click" />
    <br />
    <asp:Label ID="Label1" runat="server">asp:Label>

  7. Open code behind file and include following namespac

    using iTextSharp.text.pdf;
    using iTextSharp.text.pdf.parser;
    Imports iTextSharp.text.pdf
    Imports iTextSharp.text.pdf.parser
  8. Write below code Button click event

    protected void Button1_Click(object sender, EventArgs e)
        if (FileUpload1.HasFile)
            string file = FileUpload1.FileName;
            StringBuilder pdfText = new StringBuilder();
            PdfReader reader = new PdfReader(Server.MapPath(file));
            for (int i = 1; i <= reader.NumberOfPages; i++)
                ITextExtractionStrategy objExtractStrategy = new SimpleTextExtractionStrategy();
                string strLineText = PdfTextExtractor.GetTextFromPage(reader, i, objExtractStrategy);
                strLineText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strLineText)));
            Label1.Text = pdfText.ToString();
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        If FileUpload1.HasFile Then
            Dim file As String = FileUpload1.FileName
            Dim pdfText As New StringBuilder()
            Dim reader As New PdfReader(Server.MapPath(file))
            For i As Integer = 1 To reader.NumberOfPages
                Dim objExtractStrategy As ITextExtractionStrategy = New SimpleTextExtractionStrategy()
                Dim strLineText As String = PdfTextExtractor.GetTextFromPage(reader, i, objExtractStrategy)
                strLineText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.[Default], Encoding.UTF8, Encoding.[Default].GetBytes(strLineText)))
            Label1.Text = pdfText.ToString()
        End If
    End Sub

  9. View web site in browser, upload PDF file and click button to see text of PDF file
Add Article Comment:
Name :
Email Address :
Comments :
<< HTTP Handlers and HTTP Modules in ASP.NET

Disclaimer - Privacy
© 2002-2017