In this article, I am going to explain you how to generate pdf file using iTextSharp in C# and asp.net. First, you need to download iTextSharp dll from the internet. Click on the link to download https://github.com/itext/itextsharp.
Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.
In code-behind file:
Add below nampespaces.
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
Creating a PDF file with a single paragraph:
Below is the steps to create pdf file.
- Create a FileStream object
- Create a Document object
- Create a PdfWriter object to write the Document to the specified FileStream
- Opening the document
- Create Paragraph object
- Adding paragraph object to document.
- Closing the document
FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
Document doc = new Document();
PdfWriter.GetInstance(doc, fs);
doc.Open();
Paragraph para = new Paragraph("Welcome to asparticles.com. This is first paragraph.");
doc.Add(para);
doc.Close();
Complete code snippet is given below.
public void CreateDocument()
{
// delete the file if already exists
if (File.Exists(Server.MapPath("Example.pdf")))
{
File.Delete(Server.MapPath("Example.pdf"));
}
// create filestream object
FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
// create document object
Document doc = new Document();
// create PdfWriter instance which will write at file filestream
PdfWriter.GetInstance(doc, fs);
// opening the dociment
doc.Open();
// creating paragraph object
Paragraph para = new Paragraph("Welcome to asparticles.com. This is first paragraph.");
// adding pargraph to document
doc.Add(para);
// closing the document
doc.Close();
}
Setting page size and background color of pdf document:
public void SetPageSize()
{
if (File.Exists(Server.MapPath("Example.pdf")))
{
File.Delete(Server.MapPath("Example.pdf"));
}
FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
// setting page page size and background color
Rectangle rec = new Rectangle(PageSize.A4);
rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);
//OR
//Rectangle rec = new Rectangle(720, 720); // for page size
//rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);
Document doc = new Document(rec);
PdfWriter.GetInstance(doc, fs);
doc.Open();
Paragraph para = new Paragraph("Welcome to asparticles.com. Pdf with pagesize A4. Different kind of pagesize is supported");
doc.Add(para);
doc.Close();
}
Setting paragraph alignment:
Paragraph para = new Paragraph("Welcome to asparticles.com. This is first paragraph. This is paragraph alignment example. Thare are varius alignment supported.");
para.Alignment = Element.ALIGN_RIGHT;
Adding a PdfPTable and a PdfPCell to PdfPTable:
public void PdfTable1()
{
if (File.Exists(Server.MapPath("Example.pdf")))
{
File.Delete(Server.MapPath("Example.pdf"));
}
FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
Rectangle rec = new Rectangle(PageSize.A4);
rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);
Document doc = new Document(rec);
PdfWriter.GetInstance(doc, fs);
PdfPTable table = new PdfPTable(4); // creating a table with 4 columns
// float[] width = { 20, 20, 30, 30 }; //Header Widths
//table.SetWidths(width); //Setting the pdf headers
table.WidthPercentage = 80; //Setting the PDF File width percentage
//creating a cell and show as table title
PdfPCell cell = new PdfPCell();
cell.Colspan = 4;
cell.Border = 1;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Phrase = new Phrase("Adding cell to table & display as table title", new Font(Font.FontFamily.COURIER, 18, 1, new BaseColor(System.Drawing.Color.Blue)));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
cell.PaddingBottom = 10;
table.AddCell(cell); // adding cell to table
doc.Open();
doc.Add(table); // adding table to document
doc.Close();
}
Adding a table and multiple cells to table:
public void PdfTable2()
{
if (File.Exists(Server.MapPath("Example.pdf")))
{
File.Delete(Server.MapPath("Example.pdf"));
}
FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
Rectangle rec = new Rectangle(PageSize.A4);
rec.BackgroundColor = new BaseColor(System.Drawing.Color.WhiteSmoke);
Document doc = new Document(rec);
PdfWriter.GetInstance(doc, fs);
PdfPTable table = new PdfPTable(4); // creating a table with 4 columns
// float[] width = { 20, 20, 30, 30 }; //setting Header Widths
//table.SetWidths(width); //Setting the pdf headers
table.WidthPercentage = 80; //Setting the PDF File width percentage
//creating 1st cell and show as table title
PdfPCell cell = new PdfPCell();
cell.Colspan = 4;
cell.Border = 1;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Phrase = new Phrase("Adding cell to table & display as table title", new Font(Font.FontFamily.COURIER, 18, 1, new BaseColor(System.Drawing.Color.Blue)));
cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
cell.PaddingBottom = 10;
// adding 1st cell to table
table.AddCell(cell);
// creating and adding 2nd cell using constructor and display as a students marks header
table.AddCell(new PdfPCell(new Phrase("Student Marks", new Font(Font.FontFamily.COURIER, 15, 2, BaseColor.WHITE))) { HorizontalAlignment = Element.ALIGN_CENTER, PaddingBottom = 10, BackgroundColor = BaseColor.RED, Colspan = 4 });
// creating and adding 3rd,4th,5th,6th cell using constructor and display as a students name and subkects
table.AddCell(new PdfPCell(new Phrase("Student Name", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });
table.AddCell(new PdfPCell(new Phrase("Physics", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });
table.AddCell(new PdfPCell(new Phrase("Chemistry", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });
table.AddCell(new PdfPCell(new Phrase("Maths", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });
// creating and adding remaining cells using constructor and display as a students marks
table.AddCell(new PdfPCell(new Phrase("John", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("75", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("80", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("85", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("Mary", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("80", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("85", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("75", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("Rahul", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("90", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("75", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
table.AddCell(new PdfPCell(new Phrase("85", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
doc.Open();
doc.Add(table); // adding table to document
doc.Close();
}
Below is the pdf generated by last program.