Hi smile,
Refer below sample.
Namespaces
C#
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Linq;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.Text
Imports System.Linq
Code
C#
DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
try
{
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Session", typeof(string)),
new DataColumn("Exam", typeof(string)),
new DataColumn("AdmissionNo", typeof(string)),
new DataColumn("Subjects", typeof(string)),
new DataColumn("Mark", typeof(int)),
new DataColumn("Total", typeof(int)),
new DataColumn("Percentage", typeof(int)),
new DataColumn("Grade", typeof(string)),
new DataColumn("Remarks", typeof(string)),
new DataColumn("Position", typeof(string))});
dt.Rows.Add("2019", "Test-1", "R-01", "English", 33, 50, 66, "B", "Good", "1");
dt.Rows.Add("2019", "Test-1", "R-01", "Urdu", 32, 50, 64, "B", "Good", "1");
dt.Rows.Add("2019", "Test-1", "R-01", "Math", 30, 50, 60, "B", "Good", "1");
dt.Rows.Add("2019", "Test-1", "R-01", "Science", 25, 50, 50, "C", "Fair", "1");
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2");
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2");
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2");
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2");
dataGridView1.DataSource = dt;
}
catch (Exception)
{
throw;
}
}
private void Export(object sender, EventArgs e)
{
string folderPath = @"C:\Users\anand\Desktop\Test\\";
FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create);
Document pdfDoc = new Document();
StringReader sr = null;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
var grouped = from x in dt.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 = dt.Clone();
foreach (var columnValue in key.ColumnValues)
{
dtfinal.ImportRow(columnValue);
}
using (StringWriter sw = new StringWriter())
{
StringBuilder sb = new StringBuilder();
sb.Append("<div style='align:center;'><b>Generate Report</b></div><br/>");
sb.Append("<table>");
sb.Append("<tr>");
sb.Append("<td>Session: " + dtfinal.Rows[0]["Session"] + "</td>");
sb.Append("<td>Exam: " + dtfinal.Rows[0]["Exam"] + "</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td colspan='2'>AdmissionNo: " + dtfinal.Rows[0]["AdmissionNo"] + "</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("<br/>");
sb.Append("<br/>");
sb.Append("<table border = '1'>");
sb.Append("<tr>");
sb.Append("<b><th>Sr.Number</th><th>Subjects</th><th>Matk</th><th>Total</th><th>Percentage</th></b>");
sb.Append("</tr>");
for (int i = 0; i < dtfinal.Rows.Count; i++)
{
sb.Append("<tr>");
sb.Append("<td>" + (i + 1) + "</td>");
for (int j = 3; j < dtfinal.Columns.Count - 3; j++)
{
sb.Append("<td>" + dtfinal.Rows[i][j] + "</td>");
}
sb.Append("</tr>");
}
int obtainMark = Convert.ToInt32(dtfinal.Compute("sum(Mark)", ""));
int total = Convert.ToInt32(dtfinal.Compute("sum(Total)", ""));
int percentage = (obtainMark * 100) / total;
sb.Append("<tr><td colspan='2'></td>");
sb.Append("<td><b>" + obtainMark + "</b></td>");
sb.Append("<td><b>" + total + "</b></td>");
sb.Append("<td align = 'left'><b>" + percentage + "%" + "</b></td>");
sb.Append("</tr></table>");
sb.Append("<b>");
sb.Append("Obtained Marks : " + obtainMark);
sb.Append("<br/>");
sb.Append("Total Marks : " + total);
sb.Append("<br/>");
sb.Append("Grade : " + dtfinal.Rows[0]["Grade"]);
sb.Append("<br/>");
sb.Append("Position : " + dtfinal.Rows[0]["Position"]);
sb.Append("<br/>");
sb.Append("Remarks : " + dtfinal.Rows[0]["Remarks"]);
sb.Append("</b>");
sr = new StringReader(sb.ToString());
}
pdfDoc.NewPage();
htmlparser.Parse(sr);
}
pdfDoc.Close();
stream.Close();
}
VB.Net
Private dt As DataTable
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Try
dt = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Session", GetType(String)), New DataColumn("Exam", GetType(String)), New DataColumn("AdmissionNo", GetType(String)), New DataColumn("Subjects", GetType(String)), New DataColumn("Mark", GetType(Integer)), New DataColumn("Total", GetType(Integer)), New DataColumn("Percentage", GetType(Integer)), New DataColumn("Grade", GetType(String)), New DataColumn("Remarks", GetType(String)), New DataColumn("Position", GetType(String))})
dt.Rows.Add("2019", "Test-1", "R-01", "English", 33, 50, 66, "B", "Good", "1")
dt.Rows.Add("2019", "Test-1", "R-01", "Urdu", 32, 50, 64, "B", "Good", "1")
dt.Rows.Add("2019", "Test-1", "R-01", "Math", 30, 50, 60, "B", "Good", "1")
dt.Rows.Add("2019", "Test-1", "R-01", "Science", 25, 50, 50, "C", "Fair", "1")
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2")
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2")
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2")
dt.Rows.Add("2019", "Test-1", "R-02", "Science", 25, 50, 50, "C", "Fair", "2")
dataGridView1.DataSource = dt
Catch __unusedException1__ As Exception
Throw
End Try
End Sub
Private Sub Export(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim folderPath As String = "C:\Users\anand\Desktop\Test\\"
Dim stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
Dim pdfDoc As Document = New Document()
Dim sr As StringReader = Nothing
Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
Dim grouped = From x In dt.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 = dt.Clone()
For Each columnValue In key.ColumnValues
dtfinal.ImportRow(columnValue)
Next
Using sw As StringWriter = New StringWriter()
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<div style='align:center;'><b>Generate Report</b></div><br/>")
sb.Append("<table>")
sb.Append("<tr>")
sb.Append("<td>Session: " & dtfinal.Rows(0)("Session") & "</td>")
sb.Append("<td>Exam: " & dtfinal.Rows(0)("Exam") & "</td>")
sb.Append("</tr>")
sb.Append("<tr>")
sb.Append("<td colspan='2'>AdmissionNo: " & dtfinal.Rows(0)("AdmissionNo") & "</td>")
sb.Append("</tr>")
sb.Append("</table>")
sb.Append("<br/>")
sb.Append("<br/>")
sb.Append("<table border = '1'>")
sb.Append("<tr>")
sb.Append("<b><th>Sr.Number</th><th>Subjects</th><th>Matk</th><th>Total</th><th>Percentage</th></b>")
sb.Append("</tr>")
For i As Integer = 0 To dtfinal.Rows.Count - 1
sb.Append("<tr>")
sb.Append("<td>" & (i + 1) & "</td>")
For j As Integer = 3 To dtfinal.Columns.Count - 3 - 1
sb.Append("<td>" & dtfinal.Rows(i)(j) & "</td>")
Next
sb.Append("</tr>")
Next
Dim obtainMark As Integer = Convert.ToInt32(dtfinal.Compute("sum(Mark)", ""))
Dim total As Integer = Convert.ToInt32(dtfinal.Compute("sum(Total)", ""))
Dim percentage As Integer = (obtainMark * 100) / total
sb.Append("<tr><td colspan='2'></td>")
sb.Append("<td><b>" & obtainMark & "</b></td>")
sb.Append("<td><b>" & total & "</b></td>")
sb.Append("<td align = 'left'><b>" & percentage & "%" & "</b></td>")
sb.Append("</tr></table>")
sb.Append("<b>")
sb.Append("Obtained Marks : " & obtainMark)
sb.Append("<br/>")
sb.Append("Total Marks : " & total)
sb.Append("<br/>")
sb.Append("Grade : " & dtfinal.Rows(0)("Grade"))
sb.Append("<br/>")
sb.Append("Position : " & dtfinal.Rows(0)("Position"))
sb.Append("<br/>")
sb.Append("Remarks : " & dtfinal.Rows(0)("Remarks"))
sb.Append("</b>")
sr = New StringReader(sb.ToString())
End Using
pdfDoc.NewPage()
htmlparser.Parse(sr)
Next
pdfDoc.Close()
stream.Close()
End Sub
Screenshot