Hi alhamd,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void ExportPDf_Click(object sender, EventArgs e)
{
DataTable dtPurchase = GetData("SELECT * FROM tblPurchase");
DataTable dtPayTransaction = GetData("SELECT * FROM tblPayTransaction");
object[] dr = dtPurchase.AsEnumerable().Select(x => x["CustomerACC"]).Distinct().ToArray();
string pdfpath = Server.MapPath("~/Reports/");
if (!Directory.Exists(pdfpath))
{
Directory.CreateDirectory(pdfpath);
}
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "Reciept.pdf", FileMode.Create));
Font boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
doc.Open();
PdfPTable pdfTable = null;
Paragraph paragraph = null;
foreach (var item in dr)
{
//doc.NewPage();
paragraph = new Paragraph(new Phrase("Ledger Report", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)));
paragraph.SpacingBefore = 10f;
paragraph.Alignment = Element.ALIGN_LEFT;
doc.Add(paragraph);
pdfTable = new PdfPTable(4);
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.SetWidths(new float[] { .3f, .5f, .5f, .5f });
pdfTable.SpacingBefore = 10f;
pdfTable.DefaultCell.Border = 0;
pdfTable.AddCell(new Phrase("Account: ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase(item.ToString(), boldFont));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)));
doc.Add(pdfTable);
paragraph = new Paragraph(new Phrase("Product Details", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)));
paragraph.Alignment = Element.ALIGN_LEFT;
doc.Add(paragraph);
pdfTable = new PdfPTable(5);
pdfTable.SpacingBefore = 10f;
pdfTable.DefaultCell.Border = 0;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.SetWidths(new float[] { 0.5f, 2f, 1.5f, 1.5f, 1.5f });
pdfTable.AddCell(new Phrase("S.I", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase("Product", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase("Qty", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase("Net Bal", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase("P_Date", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)));
DataRow[] drProduct = dtPurchase.Select("CustomerACC='" + item.ToString() + "'");
decimal total = 0;
for (int i = 0; i < drProduct.Length; i++)
{
pdfTable.AddCell(new Phrase((i + 1).ToString(), FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(drProduct[i]["Product"].ToString(), FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(drProduct[i]["Qty"].ToString(), FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(drProduct[i]["NetBal"].ToString(), FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(drProduct[i]["P_Date"].ToString(), FontFactory.GetFont("Calibri", 10)));
total = total + Convert.ToDecimal(drProduct[i]["NetBal"]);
}
doc.Add(pdfTable);
pdfTable = new PdfPTable(5);
pdfTable.DefaultCell.Border = 0;
pdfTable.DefaultCell.PaddingBottom = 10f;
pdfTable.SpacingBefore = 10f;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.SetWidths(new float[] { .5f, 5.5f, 1.5f, 1.5f, 1.5f });
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("Total", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(total.ToString(), FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
int netBal = Convert.ToInt32(dtPayTransaction.Compute("SUM(Paid)", "CustormerACC = '" + item.ToString() + "'"));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("Paid in [Cash Account]", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(netBal.ToString(), FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
int remBal = Convert.ToInt32(dtPayTransaction.Compute("SUM(RemBal)", "CustormerACC = '" + item.ToString() + "'"));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("Rem Balance", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(remBal.ToString(), FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont("Calibri", 10)));
doc.Add(pdfTable);
}
doc.Close();
}
VB.Net
Private Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub ExportPDf_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim dtPurchase As DataTable = GetData("SELECT * FROM tblPurchase")
Dim dtPayTransaction As DataTable = GetData("SELECT * FROM tblPayTransaction")
Dim dr As Object() = dtPurchase.AsEnumerable().Select(Function(x) x("CustomerACC")).Distinct().ToArray()
Dim pdfpath As String = Server.MapPath("~/Reports/")
If Not Directory.Exists(pdfpath) Then
Directory.CreateDirectory(pdfpath)
End If
Dim doc As Document = New Document(PageSize.A4)
PdfWriter.GetInstance(doc, New FileStream(pdfpath & "Reciept.pdf", FileMode.Create))
Dim boldFont As Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)
doc.Open()
Dim pdfTable As PdfPTable = Nothing
Dim paragraph As Paragraph = Nothing
For Each item In dr
'doc.NewPage()
paragraph = New Paragraph(New Phrase("Ledger Report", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)))
paragraph.SpacingBefore = 10.0F
paragraph.Alignment = Element.ALIGN_LEFT
doc.Add(paragraph)
pdfTable = New PdfPTable(4)
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.SetWidths(New Single() {0.3F, 0.5F, 0.5F, 0.5F})
pdfTable.SpacingBefore = 10.0F
pdfTable.DefaultCell.Border = 0
pdfTable.AddCell(New Phrase("Account: ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase(item.ToString(), boldFont))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)))
doc.Add(pdfTable)
paragraph = New Paragraph(New Phrase("Product Details", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)))
paragraph.Alignment = Element.ALIGN_LEFT
doc.Add(paragraph)
pdfTable = New PdfPTable(5)
pdfTable.SpacingBefore = 10.0F
pdfTable.DefaultCell.Border = 0
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.SetWidths(New Single() {0.5F, 2.0F, 1.5F, 1.5F, 1.5F})
pdfTable.AddCell(New Phrase("S.I", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase("Product", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase("Qty", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase("Net Bal", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase("P_Date", FontFactory.GetFont("Calibri", 10, Font.BOLD, BaseColor.BLACK)))
Dim drProduct As DataRow() = dtPurchase.[Select]("CustomerACC='" & item.ToString() & "'")
Dim total As Decimal = 0
For i As Integer = 0 To drProduct.Length - 1
pdfTable.AddCell(New Phrase((i + 1).ToString(), FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(drProduct(i)("Product").ToString(), FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(drProduct(i)("Qty").ToString(), FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(drProduct(i)("NetBal").ToString(), FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(drProduct(i)("P_Date").ToString(), FontFactory.GetFont("Calibri", 10)))
total = total + Convert.ToDecimal(drProduct(i)("NetBal"))
Next
doc.Add(pdfTable)
pdfTable = New PdfPTable(5)
pdfTable.DefaultCell.Border = 0
pdfTable.DefaultCell.PaddingBottom = 10.0F
pdfTable.SpacingBefore = 10.0F
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.SetWidths(New Single() {0.5F, 5.5F, 1.5F, 1.5F, 1.5F})
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("Total", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(total.ToString(), FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
Dim netBal As Integer = Convert.ToInt32(dtPayTransaction.Compute("SUM(Paid)", "CustormerACC = '" & item.ToString() & "'"))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("Paid in [Cash Account]", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(netBal.ToString(), FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
Dim remBal As Integer = Convert.ToInt32(dtPayTransaction.Compute("SUM(RemBal)", "CustormerACC = '" & item.ToString() & "'"))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("Rem Balance", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(remBal.ToString(), FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(" ", FontFactory.GetFont("Calibri", 10)))
doc.Add(pdfTable)
Next
doc.Close()
End Sub
Screenshot