Hi smile,
Refer below sample.
Namespaces
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
VB.Net
Imports System.IO
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
private static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("SessionName", typeof(string)),
new DataColumn("ExamType", typeof(string)),
new DataColumn("SName", typeof(string)),
new DataColumn("FName", typeof(string)),
new DataColumn("AcademicName", typeof(string)),
new DataColumn("FPhone", typeof(string)),
new DataColumn("AdmissionNo", typeof(string)),
new DataColumn("ClassName", typeof(string)),
new DataColumn("SectionName", typeof(string)),
new DataColumn("Subject", typeof(string)),
new DataColumn("Max", typeof(int)),
new DataColumn("Obtain", typeof(int)),
new DataColumn("%", typeof(int)),
new DataColumn("Pos", typeof(int)),
new DataColumn("Grade", typeof(string)) });
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-01", "One", "A", "Urdu", 50, 24, 50, 3, "C");
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-01", "One", "A", "English", 50, 28, 55, 2, "B");
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-01", "One", "A", "Math", 50, 43, 80, 1, "A");
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-02", "One", "A", "Urdu", 50, 24, 50, 3, "C");
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-02", "One", "A", "English", 50, 28, 55, 2, "B");
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-02", "One", "A", "Math", 50, 43, 80, 1, "A");
return dt;
}
protected void tbFront_Click(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 sTable = GetData();
var grouped = from x in sTable.AsEnumerable()
group x by new { a = x["AdmissionNo"] } into g
select new
{
Value = g.Key,
ColumnValues = g
};
DataTable dtfinal = null;
foreach (var key in grouped)
{
dtfinal = sTable.Clone();
foreach (var columnValue in key.ColumnValues)
{
dtfinal.ImportRow(columnValue);
}
PdfPTable table1 = new PdfPTable(2);
table1.DefaultCell.Padding = 10f;
table1.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
table1.DefaultCell.Border = 0;
table1.HorizontalAlignment = Element.ALIGN_CENTER;
table1.TotalWidth = 500f;
table1.LockedWidth = true;
float[] widths1 = new float[] { 0.7f, 3f };
PdfPTable tableb = new PdfPTable(4);
float[] widthim = new float[] { 0.1f, 0.1f, 0.1f, 0.05f };
tableb.SetWidths(widthim);
tableb.DefaultCell.Padding = 10f;
tableb.HorizontalAlignment = Element.ALIGN_CENTER;
tableb.TotalWidth = 550f;
tableb.LockedWidth = true;
PdfPCell header = new PdfPCell(new Phrase("Student Details", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)));
header.Indent = 10;
header.HorizontalAlignment = 1;
header.Padding = 10f;
header.Colspan = 4;
tableb.AddCell(header);
PdfPTable nested = new PdfPTable(3);
float[] widthi = new float[] { 0.1f, 0.1f, 0.1f };
nested.SetWidths(widthi);
nested.DefaultCell.Padding = 10f;
nested.AddCell(new Phrase("Session: " + dtfinal.Rows[0]["SessionName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Exam: " + sTable.Rows[0]["ExamType"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Reg No. " + dtfinal.Rows[0]["AdmissionNo"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Student: " + dtfinal.Rows[0]["SName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Father: " + dtfinal.Rows[0]["FName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("AcademicYear: " + dtfinal.Rows[0]["AcademicName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Class: " + dtfinal.Rows[0]["ClassName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Section: " + dtfinal.Rows[0]["SectionName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
nested.AddCell(new Phrase("Phone No. " + dtfinal.Rows[0]["FPhone"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
doc.Add(tableb);
doc.Add(nested);
for (int j = 0; j < dtfinal.Rows.Count; j++)
{
PdfPTable table = new PdfPTable(5);
table.TotalWidth = 500f;
table.LockedWidth = true;
float[] widths = new float[] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };
table.SetWidths(widths);
table.DefaultCell.Padding = 7f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.SpacingBefore = 5f;
table.AddCell(dtfinal.Rows[j]["Subject"].ToString());
table.AddCell("A1.T1");
table.AddCell("A2.T1");
table.AddCell("A1.T2");
table.AddCell("A2.T2");
table.AddCell("Max ");
table.AddCell(dtfinal.Rows[j]["Max"].ToString());
table.AddCell("");
table.AddCell("");
table.AddCell("");
table.AddCell("Obt");
table.AddCell(dtfinal.Rows[j]["Obtain"].ToString());
table.AddCell("");
table.AddCell("");
table.AddCell("");
table.AddCell("Per");
table.AddCell(dtfinal.Rows[j]["%"].ToString());
table.AddCell("");
table.AddCell("");
table.AddCell("");
table.AddCell("Grade");
table.AddCell(dtfinal.Rows[j]["Grade"].ToString());
table.AddCell("");
table.AddCell("");
table.AddCell("");
doc.Add(table);
}
doc.NewPage();
}
doc.Close();
}
VB.Net
Private Shared Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("SessionName", GetType(String)), New DataColumn("ExamType", GetType(String)), New DataColumn("SName", GetType(String)), New DataColumn("FName", GetType(String)), New DataColumn("AcademicName", GetType(String)), New DataColumn("FPhone", GetType(String)), New DataColumn("AdmissionNo", GetType(String)), New DataColumn("ClassName", GetType(String)), New DataColumn("SectionName", GetType(String)), New DataColumn("Subject", GetType(String)), New DataColumn("Max", GetType(Integer)), New DataColumn("Obtain", GetType(Integer)), New DataColumn("%", GetType(Integer)), New DataColumn("Pos", GetType(Integer)), New DataColumn("Grade", GetType(String))})
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-01", "One", "A", "Urdu", 50, 24, 50, 3, "C")
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-01", "One", "A", "English", 50, 28, 55, 2, "B")
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-01", "One", "A", "Math", 50, 43, 80, 1, "A")
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-02", "One", "A", "Urdu", 50, 24, 50, 3, "C")
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-02", "One", "A", "English", 50, 28, 55, 2, "B")
dt.Rows.Add("2019", "Internal", "s1", "f1", "t1t1t", "654165465", "R-02", "One", "A", "Math", 50, 43, 80, 1, "A")
Return dt
End Function
Protected Sub tbFront_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pdfpath As String = "C:\Users\anand\Desktop\Test\\"
If Not Directory.Exists(pdfpath) Then
Directory.CreateDirectory(pdfpath)
End If
Dim folderPath As String = "C:\Users\anand\Desktop\Test\\"
Dim stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
Dim doc As Document = New Document()
PdfWriter.GetInstance(doc, stream)
doc.Open()
Dim sTable As DataTable = GetData()
Dim grouped = From x In sTable.AsEnumerable()
Group x By Value = x.Field(Of String)("AdmissionNo") Into g = Group
Select New With {
.Value = Value,
.ColumnValues = g}
For Each key In grouped
Dim dtfinal As DataTable = sTable.Clone()
For Each columnValue In key.ColumnValues
dtfinal.ImportRow(columnValue)
Next
dtfinal = sTable.Clone()
For Each columnValue In key.ColumnValues
dtfinal.ImportRow(columnValue)
Next
Dim table1 As PdfPTable = New PdfPTable(2)
table1.DefaultCell.Padding = 10.0F
table1.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE
table1.DefaultCell.Border = 0
table1.HorizontalAlignment = Element.ALIGN_CENTER
table1.TotalWidth = 500.0F
table1.LockedWidth = True
Dim widths1 As Single() = New Single() {0.7F, 3.0F}
Dim tableb As PdfPTable = New PdfPTable(4)
Dim widthim As Single() = New Single() {0.1F, 0.1F, 0.1F, 0.05F}
tableb.SetWidths(widthim)
tableb.DefaultCell.Padding = 10.0F
tableb.HorizontalAlignment = Element.ALIGN_CENTER
tableb.TotalWidth = 550.0F
tableb.LockedWidth = True
Dim header As PdfPCell = New PdfPCell(New Phrase("Student Details", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)))
header.Indent = 10
header.HorizontalAlignment = 1
header.Padding = 10.0F
header.Colspan = 4
tableb.AddCell(header)
Dim nested As PdfPTable = New PdfPTable(3)
Dim widthi As Single() = New Single() {0.1F, 0.1F, 0.1F}
nested.SetWidths(widthi)
nested.DefaultCell.Padding = 10.0F
nested.AddCell(New Phrase("Session: " & dtfinal.Rows(0)("SessionName"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("Exam: " & sTable.Rows(0)("ExamType"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("Reg No. " & dtfinal.Rows(0)("AdmissionNo"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("Student: " & dtfinal.Rows(0)("SName"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("Father: " & dtfinal.Rows(0)("FName"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("AcademicYear: " & dtfinal.Rows(0)("AcademicName"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("Class: " & dtfinal.Rows(0)("ClassName"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("Section: " & dtfinal.Rows(0)("SectionName"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
nested.AddCell(New Phrase("Phone No. " & dtfinal.Rows(0)("FPhone"), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
doc.Add(tableb)
doc.Add(nested)
For j As Integer = 0 To dtfinal.Rows.Count - 1
Dim table As PdfPTable = New PdfPTable(5)
table.TotalWidth = 500.0F
table.LockedWidth = True
Dim widths As Single() = New Single() {0.1F, 0.1F, 0.1F, 0.1F, 0.1F}
table.SetWidths(widths)
table.DefaultCell.Padding = 7.0F
table.HorizontalAlignment = Element.ALIGN_CENTER
table.SpacingBefore = 5.0F
table.AddCell(dtfinal.Rows(j)("Subject").ToString())
table.AddCell("A1.T1")
table.AddCell("A2.T1")
table.AddCell("A1.T2")
table.AddCell("A2.T2")
table.AddCell("Max ")
table.AddCell(dtfinal.Rows(j)("Max").ToString())
table.AddCell("")
table.AddCell("")
table.AddCell("")
table.AddCell("Obt")
table.AddCell(dtfinal.Rows(j)("Obtain").ToString())
table.AddCell("")
table.AddCell("")
table.AddCell("")
table.AddCell("Per")
table.AddCell(dtfinal.Rows(j)("%").ToString())
table.AddCell("")
table.AddCell("")
table.AddCell("")
table.AddCell("Grade")
table.AddCell(dtfinal.Rows(j)("Grade").ToString())
table.AddCell("")
table.AddCell("")
table.AddCell("")
doc.Add(table)
Next
doc.NewPage()
Next
doc.Close()
End Sub
Screenshot