Step by step create PDF by using iTextSharp API

Advantages of using iTextSharp are,

  • Free available utility
  • allows us to create and manipulate PDF documents
  • Dynamic PDF document generation and/or manipulation.

Below are the steps for using the iTextSharp for devolopment,

  1. You can download the iTextSharp.dll from
  2. Open VisualStudio and then create a new ASP.NET web application.
  3. Add a reference to the iTextSharp.dll file to your project.
  4. Add the following namespaces before proceeding:

 using System;using System.IO;using System.Diagnostics;

using iTextSharp.text;

using iTextSharp.text.pdf;

In the designer, drag and drop two buttons as follows:

For the Generate PDF File button, write the following code to generate the PDF file:

protected void btnGeneratePDFFile_Click(object sender, EventArgs e)

Document doc = new  Document();
//Create PDF Table     PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(“Sample-PDF-File.pdf”), FileMode.Create));    //Open the PDF document     doc.Add(Add_Content_To_PDF(tableLayout));    // Closing the document     btnGeneratePDFFile.Enabled = false;private PdfPTable Add_Content_To_PDF(PdfPTable tableLayout)    float[] headers = { 20, 20, 30, 30 };  //Header Widths    tableLayout.WidthPercentage = 80;       //Set the PDF File witdh percentage   //Add Title to the PDF file at the top    AddCellToHeader(tableLayout, “Cricketer Name”);   AddCellToHeader(tableLayout, “Born On”);    AddCellToBody(tableLayout, “Sachin Tendulkar”);   AddCellToBody(tableLayout, “April 24, 1973”);    AddCellToBody(tableLayout, “1.75 m”);   AddCellToBody(tableLayout, “Devki Devi, Pan Singh”);   AddCellToBody(tableLayout, “Virender Sehwag”);   AddCellToBody(tableLayout, “October 20, 1978”);    AddCellToBody(tableLayout, “1.75 m”);   AddCellToBody(tableLayout, “Saroj Kohli, Prem Kohli”);   return tableLayout; private static void AddCellToHeader(PdfPTable tableLayout,string cellText)    tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.WHITE))) {HorizontalAlignment = Element.ALIGN_CENTER, Padding=5, BackgroundColor = new iTextSharp.text.Color(0, 51, 102) }); private static void AddCellToBody(PdfPTable tableLayout, string cellText)    tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER,Padding = 5, BackgroundColor = iTextSharp.text.Color.WHITE });


Don’t be confused by the code above, I am extracting the methods for re-usability purposes.

I will explain whats happening in the preceding methods.


In this method I am creating the PDF file in a specific location. Then I am calling the Add_Content_To_PDF method.


In this method I am giving the sizes of the cells as well as PDF file width and then I am adding content to the PDF file usigng the two methods AddCellToHeader and AddCellToBody.


In this mehtod I am adding a single cell to the PDF header with some style.


In this mehtod I am adding a single cell to the PDF body with some style.


// Method to add single cell to the body



// Method to add single cell to the header



AddCellToBody(tableLayout, “November 5, 1988”);

AddCellToBody(tableLayout, “Virat Kohli”);

AddCellToBody(tableLayout, “Aryavir Sehwag, Vedant Sehwag”);

AddCellToBody(tableLayout, “1.70 m”);


AddCellToBody(tableLayout, “July 7, 1981”);

AddCellToBody(tableLayout, “Mahendra Singh Dhoni”);

AddCellToBody(tableLayout, “Ramesh Tendulkar, Rajni Tendulkar”);

AddCellToBody(tableLayout, “1.65 m”);

//Add body

AddCellToHeader(tableLayout, “Parents”);

AddCellToHeader(tableLayout, “Height”);

//Add header

tableLayout.AddCell(new PdfPCell(new Phrase(“Creating PDF file using iTextsharp”, new Font(Font.HELVETICA, 13, 1, new iTextSharp.text.Color(153, 51, 0)))) { Colspan = 4, Border = 0, PaddingBottom = 20, HorizontalAlignment = Element.ALIGN_CENTER });


tableLayout.SetWidths(headers);        //Set the pdf headers



btnOpenPDFFile.Enabled = true;


//Add Content to PDF


//Create a PDF file in specific path

PdfPTable tableLayout = new PdfPTable(4);

//Create document

Now in the Open PDF button click write the following code to open the PDF that has been created:

protected void btnOpenPDFFile_Click(object sender, EventArgs e)
//Open the PDF file
btnGeneratePDFFile.Enabled = true;
btnOpenPDFFile.Enabled = false;

Save the files and run the project. Click on Generate PDF; your PDF file will be generated. Then click on the Open PDF file, you will get the created PDF.
So Simply we can create PDF by using the iTextSharp library.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s