Technological Guide.

How to create Pdf file in ASP.Net

Creating Pdf files in Asp.Net may not be a requirement but many of us being developers always wish to create pdf files to deliver some information instead of always going for crystal reports or others. The Microsoft .Net Framework didn’t provide any native way to work with PDF files, to serve our purpose we need to rely on third party components that are available. The best open-source free component available is iTextSharp. iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly. You can download iTextSharp dll from here.

 

Here we’ll see a simple web application to make use of iTextsharp to create pdf file with our own text. Create a web application and Add reference to the iTextShar.dll file which is extracted from the zip file downloaded from the above link. Create a folder inside your application called “PDF” to store our created pdf files. Design the Default.aspx with a multilined TextBox (to let user or we to enter our preferred text in paragraphs to appear in the pdf file) and a Button to trigger the code for pdf file creation as shown in the picture.

In code behind file we’ve to add 2 namespaces inorder to use iTextSharp Classes and Methods.

 

Code Behind File

Run the application, enter the information in multilined Textbox which must be displayed in pdf and click the button Create Pdf. An alert message will prompt you saying Pdf file has been successfully created. Now close the application and check the PDF folder inside the application which will contain a Pdf file created whcih looks as follows.

You may also like to read

 

3 Responses to “How to create Pdf file in ASP.Net”

  1. Renah says:

    Thank you Shyam for the detailed walk through…You helped to resolve my problem.

    Have a nice day :)

  2. Shyam says:

    Welcome Renah :) You too have a nice day.

  3. John Straumann says:

    Hi:

    Great example but is it possible to just open the PDF file directly? I need to run this from a hosted ASP.NET app, so I cannot write the file to teh server…

    Please reply to

    Thanks!

    John.

Leave a Reply