Hi smile,
Refer below sample.
Namespaces
C#
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
string pdfpath = @"C:\Users\anand\Desktop\Test\\";
if (!Directory.Exists(pdfpath))
{
Directory.CreateDirectory(pdfpath);
}
string folderPath = @"C:\Users\anand\Desktop\Test\\";
FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create);
Document doc = new Document();
PdfWriter.GetInstance(doc, stream);
doc.Open();
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]{new DataColumn("SessionName",typeof(string)),
new DataColumn("Term",typeof(string)),
new DataColumn("Exam",typeof(string)),
new DataColumn("AdmissionNo",typeof(string)),
new DataColumn("Subject",typeof(string)),
new DataColumn("Max",typeof(int)),
new DataColumn("Mark",typeof(int))});
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "English", 40, 23);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Math", 40, 28);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Urdu", 40, 28);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Science", 40, 28);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Computer", 40, 28);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "English", 60, 43);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Math", 60, 28);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Urdu", 60, 28);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Science", 60, 28);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Computer", 60, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "English", 40, 33);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Math", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Urdu", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Science", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Computer", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "English", 40, 25);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Math", 40, 30);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Urdu", 40, 30);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Science", 40, 30);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Computer", 40, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "English", 40, 25);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Math", 40, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Urdu", 40, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Science", 40, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Computer", 40, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "English", 60, 25);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Math", 60, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Urdu", 60, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Science", 60, 30);
dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Computer", 60, 30);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "English", 40, 53);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Math", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Urdu", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Science", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Computer", 40, 28);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "English", 60, 25);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Math", 60, 30);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Urdu", 60, 30);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Science", 60, 30);
dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Computer", 60, 30);
DataSet dsAdmissionNo = new DataSet();
DataView viewAdmissionNo = new DataView(dt);
DataTable distinctAdmissionNoTable = viewAdmissionNo.ToTable(true, "AdmissionNo");
for (int i = 0; i < distinctAdmissionNoTable.Rows.Count; i++)
{
DataTable dtAdmissionNo = dt.Select("AdmissionNo='" + distinctAdmissionNoTable.Rows[i][0] + "'").CopyToDataTable();
DataView viewExam = new DataView(dtAdmissionNo);
DataTable distinctExamTable = viewExam.ToTable(true, "Exam");
DataTable dtFinal = new DataTable();
dtFinal.TableName = "tbl_" + distinctAdmissionNoTable.Rows[i][0];
dtFinal.Columns.Add("Subject");
for (int j = 0; j < distinctExamTable.Rows.Count; j++)
{
dtFinal.Columns.Add(distinctExamTable.Rows[j][0].ToString(), typeof(int));
}
DataView viewSubject = new DataView(dtAdmissionNo);
DataTable distinctSubject = viewSubject.ToTable(true, "Subject");
for (int k = 0; k < distinctSubject.Rows.Count; k++)
{
DataRow dr = dtFinal.NewRow();
dr["Subject"] = distinctSubject.Rows[k][0];
dtFinal.Rows.Add(dr);
}
dsAdmissionNo.Tables.Add(dtFinal);
}
for (int i = 0; i < dsAdmissionNo.Tables.Count; i++)
{
DataTable dataTable = dsAdmissionNo.Tables[i];
string admissionNo = dsAdmissionNo.Tables[i].TableName.Split('_')[1];
for (int row = 0; row < dataTable.Rows.Count; row++)
{
for (int column = 1; column < dataTable.Columns.Count; column++)
{
dataTable.Rows[row][column] = dt.Select("Subject='" + dataTable.Rows[row][0] + "' AND Exam='" + dataTable.Columns[column].ColumnName + "' AND AdmissionNo='" + admissionNo + "'")[0].ItemArray[6];
}
}
}
for (int k = 0; k < dsAdmissionNo.Tables.Count; k++)
{
PdfPTable pdfTable = new PdfPTable(dsAdmissionNo.Tables[0].Columns.Count);
DataTable admissionNoTable = dsAdmissionNo.Tables[k];
for (int l = 0; l < admissionNoTable.Columns.Count; l++)
{
pdfTable.AddCell(new Phrase(admissionNoTable.Columns[l].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
}
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
for (int l = 1; l < admissionNoTable.Columns.Count; l++)
{
string exam = admissionNoTable.Columns[l].ToString();
string admission = admissionNoTable.TableName.Split('_')[1];
int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]);
pdfTable.AddCell(new Phrase(max.ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
}
for (int j = 0; j < admissionNoTable.Rows.Count; j++)
{
for (int l = 0; l < admissionNoTable.Columns.Count; l++)
{
if (l == 0)
{
pdfTable.AddCell(new Phrase((j + 1) + "-" + admissionNoTable.Rows[j][l].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
}
else
{
pdfTable.AddCell(new Phrase(admissionNoTable.Rows[j][l].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 10)));
}
}
}
pdfTable.AddCell(new Phrase("Obtain", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
for (int l = 1; l < admissionNoTable.Columns.Count; l++)
{
int sum = Convert.ToInt32(admissionNoTable.Compute("SUM([" + admissionNoTable.Columns[l].ColumnName + "])", string.Empty));
pdfTable.AddCell(new Phrase(sum.ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
}
pdfTable.AddCell(new Phrase("Max", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
for (int l = 1; l < admissionNoTable.Columns.Count; l++)
{
string exam = admissionNoTable.Columns[l].ToString();
string admission = admissionNoTable.TableName.Split('_')[1];
int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]);
pdfTable.AddCell(new Phrase((max * admissionNoTable.Rows.Count).ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
}
pdfTable.AddCell(new Phrase("%", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
for (int l = 1; l < admissionNoTable.Columns.Count; l++)
{
string exam = admissionNoTable.Columns[l].ToString();
string admission = admissionNoTable.TableName.Split('_')[1];
int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]);
max = max * admissionNoTable.Rows.Count;
int obtained = Convert.ToInt32(admissionNoTable.Compute("SUM([" + admissionNoTable.Columns[l].ColumnName + "])", string.Empty));
pdfTable.AddCell(new Phrase((obtained * 100 / max).ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
}
pdfTable.AddCell(new Phrase("Grade", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
for (int l = 1; l < admissionNoTable.Columns.Count ; l++)
{
string grade = "";
string exam = admissionNoTable.Columns[l].ToString();
string admission = admissionNoTable.TableName.Split('_')[1];
int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]);
max = max * admissionNoTable.Rows.Count;
int obtained = Convert.ToInt32(admissionNoTable.Compute("SUM([" + admissionNoTable.Columns[l].ColumnName + "])", string.Empty));
int percentage = obtained * 100 / max;
if (percentage >= 90)
{
grade = "O";
}
else if (percentage >= 80 && percentage <= 89)
{
grade = "A+";
}
else if (percentage >= 70 && percentage <= 79)
{
grade = "A";
}
else if (percentage >= 50 && percentage <= 69)
{
grade = "B";
}
else
{
grade = "F";
}
pdfTable.AddCell(new Phrase(grade, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
}
doc.Add(pdfTable);
doc.NewPage();
}
doc.Close();
}
Screenshot