Generate PDF File at Runtime in ASP.Net

Introduction

This article describes how to generate a PDF file at runtime in ASP.NET. For generating the PDF file, you need to use a PDF generator library.

Note: I will use the "iTextSharp.dll" as a PDF generator library. You can download it from:

  1. Links

    http://sourceforge.net/projects/itextsharp
    https://www.nuget.org/packages/iTextSharp/
     
  2. You can download the attached file of this article.

To explain this article, I will do the procedure like:

  1. Add a reference of the downloaded "iTextSharp.dll" to the Project/Website.
  2. Write some code in the ".cs" file to generate the PDF file with some text for the button Click event.

The following are the details of the preceding procedure.

Step 1

  1. Create a new empty Website named "PDF_Generation".

    Creating Empty Web Site
     
  2. Right-click on the website and click on "Add Reference".

    Adding Reference
     
  3. Use the following sub steps in the "Reference Manager".
     
    1. Click on the Browse tab on the left side
    2. Click on the Browse button at the bottom
    3. Select the "iTextSharp.dll" from the system
    4. Click on the Add button

    Adding DLL Reference

Finally it will look like:

Reference Manager

Now click on the "OK" button and see the "Solution Explorer" where the "iTextSharp.dll" reference has been added to the "Bin" folder.

Solution Explorer

Step 2

  1. Add a new Page named GenerateFile.aspx.

    Adding Web Form
     
  2. Add a Button with Onclick event (to generate the PDF) on the page.
    1. <asp:Button ID="btnGenerate" runat="server" Text="Generate PDF" OnClick="btnGenerate_Click" /> 
    Web Form Coding
     
  3. Add the following 2 namespaces to the top of the ".cs" file:
    1. using iTextSharp.text;  
    2. using iTextSharp.text.pdf;   
  4. Write the code to generate the PDF file on click event of the button: 

    1. protected void btnGenerate_Click(object sender, EventArgs e)  
    2.     {  
    3.         try  
    4.         {  
    5.             Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);  
    6.             PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);  
    7.             pdfDoc.Open();  
    8.             Paragraph Text = new Paragraph("This is test file");  
    9.             pdfDoc.Add(Text);  
    10.             pdfWriter.CloseStream = false;  
    11.             pdfDoc.Close();  
    12.             Response.Buffer = true;  
    13.             Response.ContentType = "application/pdf";  
    14.             Response.AddHeader("content-disposition""attachment;filename=Example.pdf");  
    15.             Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    16.             Response.Write(pdfDoc);  
    17.             Response.End();  
    18.         }  
    19.         catch (Exception ex)  
    20.         { Response.Write(ex.Message); }  
    21.     }  

     

    Coding Page

Note: You can provide any name of the generated file and any text that you want to print in the PDF file. For example here I provided "filename=Example.pdf" and the text as "This is test file".

Step 3

  1. Run the page that will be like:

    Running Web Site 
  2. Click on the "Generate PDF" button and save the "Example.pdf" file.

    Perform Operation

Result: Open the PDF file and see your text in the PDF file.

Test PDF File

Up Next
    Ebook Download
    View all
    Learn
    View all