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;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.Text
Code
C#
DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Phone", typeof(string)),
new DataColumn("Month", typeof(string)),
new DataColumn("IssueDate", typeof(string)),
new DataColumn("DueDate", typeof(string)),
new DataColumn("AdmissionNo", typeof(string)),
new DataColumn("SName", typeof(string)),
new DataColumn("FName", typeof(string)),
new DataColumn("ClassName", typeof(string)),
new DataColumn("SectionName", typeof(string)),
new DataColumn("AdmissionFees", typeof(int)),
new DataColumn("ExamFees", typeof(int)),
new DataColumn("LibraryFees", typeof(int)),
new DataColumn("MonthlyFees", typeof(int)),
});
dt.Rows.Add("12345", "July", "01/02/2019", "03/03/2019", "R-00001", "ABC", "SKY", "Eight", "A", 1000, 500, 1000, 2500);
dt.Rows.Add("867968", "August", "02/05/2019", "03/06/2019", "R-00002", "ABC", "SKY", "Eight", "A", 1000, 500, 1000, 2500);
dataGridView1.DataSource = dt;
}
private void Export(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] { new DataColumn("Head", typeof(string)), new DataColumn("Amount", typeof(int)) });
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();
for (int i = 0; i < dt.Rows.Count; i++)
{
dt1.Rows.Clear();
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>");
sb.Append("AdmissionNo: " + dt.Rows[i]["AdmissionNo"]);
sb.Append("</td>");
sb.Append("<td>");
sb.Append("Class: " + dt.Rows[i]["ClassName"]);
sb.Append("</td>");
sb.Append("<td>");
sb.Append("Section: " + dt.Rows[i]["SectionName"]);
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td>");
sb.Append("SName: " + dt.Rows[i]["SName"]);
sb.Append("</td>");
sb.Append("<td>");
sb.Append("FName: " + dt.Rows[i]["FName"]);
sb.Append("</td>");
sb.Append("<td>");
sb.Append("Phone: " + dt.Rows[i]["Phone"]);
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td>");
sb.Append("Month: " + dt.Rows[i]["Month"]);
sb.Append("</td>");
sb.Append("<td>");
sb.Append("IssueDate: " + dt.Rows[i]["IssueDate"]);
sb.Append("</td>");
sb.Append("<td>");
sb.Append("DueDate: " + dt.Rows[i]["DueDate"]);
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("</table>");
sb.Append("</br>");
sb.Append("<table border = '1'>");
sb.Append("<tr>");
sb.Append("<b><th>Head</th><th>Amount</th></b>");
sb.Append("</tr>");
for (int j = 9; j < dt.Columns.Count; j++)
{
dt1.Rows.Add(dt.Columns[j].ColumnName, dt.Rows[i][j]);
sb.Append("<tr>");
sb.Append("<td>");
sb.Append(dt.Columns[j].ColumnName);
sb.Append("</td>");
sb.Append("<td>");
sb.Append(dt.Rows[i][j]);
sb.Append("</td>");
sb.Append("</tr>");
}
int sum = Convert.ToInt32(dt1.Compute("sum(Amount)", ""));
sb.Append("</table>");
sb.Append("<b>");
sb.Append("Payable " + sum);
sb.Append("</b>");
sr = new StringReader(sb.ToString());
pdfDoc.NewPage();
htmlparser.Parse(sr);
}
}
pdfDoc.Close();
stream.Close();
}
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
dt = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Phone", GetType(String)), New DataColumn("Month", GetType(String)), New DataColumn("IssueDate", GetType(String)), New DataColumn("DueDate", GetType(String)), New DataColumn("AdmissionNo", GetType(String)), New DataColumn("SName", GetType(String)), New DataColumn("FName", GetType(String)), New DataColumn("ClassName", GetType(String)), New DataColumn("SectionName", GetType(String)), New DataColumn("AdmissionFees", GetType(Integer)), New DataColumn("ExamFees", GetType(Integer)), New DataColumn("LibraryFees", GetType(Integer)), New DataColumn("MonthlyFees", GetType(Integer))})
dt.Rows.Add("12345", "July", "01/02/2019", "03/03/2019", "R-00001", "ABC", "SKY", "Eight", "A", 1000, 500, 1000, 2500)
dt.Rows.Add("867968", "August", "02/05/2019", "03/06/2019", "R-00002", "ABC", "SKY", "Eight", "A", 1000, 500, 1000, 2500)
dataGridView1.DataSource = dt
End Sub
Private Sub Export(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn() {New DataColumn("Head", GetType(String)), New DataColumn("Amount", GetType(Integer))})
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()
For i As Integer = 0 To dt.Rows.Count - 1
dt1.Rows.Clear()
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>")
sb.Append("AdmissionNo: " & dt.Rows(i)("AdmissionNo"))
sb.Append("</td>")
sb.Append("<td>")
sb.Append("Class: " & dt.Rows(i)("ClassName"))
sb.Append("</td>")
sb.Append("<td>")
sb.Append("Section: " & dt.Rows(i)("SectionName"))
sb.Append("</td>")
sb.Append("</tr>")
sb.Append("<tr>")
sb.Append("<td>")
sb.Append("SName: " & dt.Rows(i)("SName"))
sb.Append("</td>")
sb.Append("<td>")
sb.Append("FName: " & dt.Rows(i)("FName"))
sb.Append("</td>")
sb.Append("<td>")
sb.Append("Phone: " & dt.Rows(i)("Phone"))
sb.Append("</td>")
sb.Append("</tr>")
sb.Append("<tr>")
sb.Append("<td>")
sb.Append("Month: " & dt.Rows(i)("Month"))
sb.Append("</td>")
sb.Append("<td>")
sb.Append("IssueDate: " & dt.Rows(i)("IssueDate"))
sb.Append("</td>")
sb.Append("<td>")
sb.Append("DueDate: " & dt.Rows(i)("DueDate"))
sb.Append("</td>")
sb.Append("</tr>")
sb.Append("</table>")
sb.Append("</br>")
sb.Append("<table border = '1'>")
sb.Append("<tr>")
sb.Append("<b><th>Head</th><th>Amount</th></b>")
sb.Append("</tr>")
For j As Integer = 9 To dt.Columns.Count - 1
dt1.Rows.Add(dt.Columns(j).ColumnName, dt.Rows(i)(j))
sb.Append("<tr>")
sb.Append("<td>")
sb.Append(dt.Columns(j).ColumnName)
sb.Append("</td>")
sb.Append("<td>")
sb.Append(dt.Rows(i)(j))
sb.Append("</td>")
sb.Append("</tr>")
Next
Dim sum As Integer = Convert.ToInt32(dt1.Compute("sum(Amount)", ""))
sb.Append("</table>")
sb.Append("<b>")
sb.Append("Payable " & sum)
sb.Append("</b>")
sr = New StringReader(sb.ToString())
pdfDoc.NewPage()
htmlparser.Parse(sr)
End Using
Next
pdfDoc.Close()
stream.Close()
End Sub
Screenshot