iTextSharp
iTextSharp is a library that allows you to create and manipulate PDF documents.
It enables developers looking to enhance web applications and other applications with dynamic PDF document generation and/or manipulation.
Before proceeding you should have the iTextSharp library file.
You can download the iTextSharp.dll from here.
- Open VisualStudio and then create a new ASP.NET web application.
- Add a reference to the iTextSharp.dll file to your project.
- Add the following namespaces before proceeding:
- using System;
- using iTextSharp.text.pdf;
- using System.IO;
- using iTextSharp.text;
- using System.Diagnostics;
- 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) {
- //Create document
- Document doc = new Document();
- //Create PDF Table
- PdfPTable tableLayout = new PdfPTable(4);
- //Create a PDF file in specific path
- PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("Sample-PDF-File.pdf"), FileMode.Create));
- //Open the PDF document
- doc.Open();
- //Add Content to PDF
- doc.Add(Add_Content_To_PDF(tableLayout));
- // Closing the document
- doc.Close();
- btnOpenPDFFile.Enabled = true;
- btnGeneratePDFFile.Enabled = false;
- }
- private PdfPTable Add_Content_To_PDF(PdfPTable tableLayout) {
- float[] headers = {
- 20,
- 20,
- 30,
- 30
- }; //Header Widths
- tableLayout.SetWidths(headers); //Set the pdf headers
- tableLayout.WidthPercentage = 80; //Set the PDF File witdh percentage
- //Add Title to the PDF file at the top
- 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
- });
- //Add header
- AddCellToHeader(tableLayout, "Cricketer Name");
- AddCellToHeader(tableLayout, "Height");
- AddCellToHeader(tableLayout, "Born On");
- AddCellToHeader(tableLayout, "Parents");
- //Add body
- AddCellToBody(tableLayout, "Sachin Tendulkar");
- AddCellToBody(tableLayout, "1.65 m");
- AddCellToBody(tableLayout, "April 24, 1973");
- AddCellToBody(tableLayout, "Ramesh Tendulkar, Rajni Tendulkar");
- AddCellToBody(tableLayout, "Mahendra Singh Dhoni");
- AddCellToBody(tableLayout, "1.75 m");
- AddCellToBody(tableLayout, "July 7, 1981");
- AddCellToBody(tableLayout, "Devki Devi, Pan Singh");
- AddCellToBody(tableLayout, "Virender Sehwag");
- AddCellToBody(tableLayout, "1.70 m");
- AddCellToBody(tableLayout, "October 20, 1978");
- AddCellToBody(tableLayout, "Aryavir Sehwag, Vedant Sehwag");
- AddCellToBody(tableLayout, "Virat Kohli");
- AddCellToBody(tableLayout, "1.75 m");
- AddCellToBody(tableLayout, "November 5, 1988");
- AddCellToBody(tableLayout, "Saroj Kohli, Prem Kohli");
- return tableLayout;
- }
- // Method to add single cell to the header
- 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)
- });
- }
- // Method to add single cell to the body
- 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 reusability purposes.
I will explain whats happening in the preceding methods.
btnGeneratePDFFile_Click
In this method I am creating the PDF file in a specific location. Then I am calling the Add_Content_To_PDF method.
Add_Content_To_PDF
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.
AddCellToHeader
In this mehtod I am adding a single cell to the PDF header with some style.
AddCellToBody
In this mehtod I am adding a single cell to the PDF body with some style.
- 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
- Process.Start(Server.MapPath("Sample-PDF-File.pdf"));
- 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.
- If your code is not working then I have added a project to this article you can download and test.
So in this way we can create a PDF simply by using the iTextSharp library.

waseem waliPosted Jan 23, 2024, 12:49 PM
How to add picture
vimlesh kumarPosted Jul 7, 2020, 10:21 AM
Hi Did you worked for Kantar???
Ritu GargPosted Jun 5, 2019, 9:35 AM
Thanks so much , i was struggling since long tinme on this. Your blog helped me alot
Osmith SmithPosted Jan 2, 2019, 12:55 PM
Howto Create a PDF file in specific path in c # in my C drive directory thank u
mostak shaikhPosted Oct 4, 2018, 1:37 AM
How to colspan and rowspan ?
Shankar ReshmimathPosted Sep 30, 2018, 6:25 AM
I need help in creating password for PDF files stored in specific location and also need to rename those PDF files
Shankar ReshmimathPosted Sep 30, 2018, 6:24 AM
Hi Srinubabu,
ayten kurnazPosted May 17, 2018, 10:11 AM
I tried your code but It gave me error message about Server.MapPath.
Patrick MuofheePosted Jul 10, 2017, 5:14 AM
Hi Srinubabu How can i split the pdf depending on page Numbers?
Former memberPosted Apr 29, 2014, 11:03 PM
You can create simple and complex pdf file using C# with Aspose.Pdf for .NET Library, its not free but offers free trial of one month and you can get the idea of creating your own application from their code samples also: http://www.aspose.com/.net/pdf-component.aspx
Srinubabu RavillaPosted Jun 18, 2013, 2:30 AM
Yes it is Free / Open Source PDF Library. No, i have tested without images.
Mahesh ChandPosted Jun 18, 2013, 2:10 AM
Useful. Is iTextSharp free? Did you try embedding an image in PDF?