Hi smile,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Clear();
DataGridViewColumn saleID = new DataGridViewTextBoxColumn();
saleID.Name = "SaleID";
saleID.HeaderText = "Id";
saleID.DataPropertyName = "SaleID";
saleID.Width = 30;
dataGridView1.Columns.Insert(0, saleID);
DataGridViewColumn invoiceNo = new DataGridViewTextBoxColumn();
invoiceNo.HeaderText = "Invoice";
invoiceNo.Name = "InvoiceNo";
invoiceNo.DataPropertyName = "InvoiceNo";
invoiceNo.Width = 60;
dataGridView1.Columns.Insert(1, invoiceNo);
DataGridViewColumn total = new DataGridViewTextBoxColumn();
total.Name = "Total";
total.HeaderText = "Total";
total.DataPropertyName = "Total";
total.Width = 40;
dataGridView1.Columns.Insert(2, total);
DataGridViewColumn paid = new DataGridViewTextBoxColumn();
paid.Name = "Paid";
paid.HeaderText = "Paid";
paid.DataPropertyName = "Paid";
paid.Width = 40;
dataGridView1.Columns.Insert(3, paid);
DataGridViewColumn due = new DataGridViewTextBoxColumn();
due.Name = "Due";
due.HeaderText = "Due";
due.DataPropertyName = "Due";
due.Width = 30;
dataGridView1.Columns.Insert(4, due);
DataTable dt = new DataTable();
dt.Columns.Add("SaleID");
dt.Columns.Add("InvoiceNo");
dt.Columns.Add("Total");
dt.Columns.Add("Paid");
dt.Columns.Add("Due");
dt.Rows.Add("1", "Inv-01", "207", "210", "-3");
dt.Rows.Add("2", "Inv-02", "104", "105", "-1");
dt.Rows.Add("3", "Inv-03", "83", "100", "-17");
dataGridView1.DataSource = null;
dataGridView1.DataSource = dt;
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = "";
buttonColumn.Width = 60;
buttonColumn.Name = "buttonColumn";
buttonColumn.Text = "Print";
buttonColumn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Insert(5, buttonColumn);
}
private void btnPDF_Click(object sender, EventArgs e)
{
string pdfpath = @"C:\Reports\";
if (!Directory.Exists(pdfpath))
{
Directory.CreateDirectory(pdfpath);
}
DataTable dt = new DataTable();
dt.Columns.Add("InvoiceNo");
dt.Columns.Add("ItemCode");
dt.Columns.Add("Qtry");
dt.Columns.Add("Price");
dt.Columns.Add("Disc");
dt.Columns.Add("Disc_Cost");
dt.Columns.Add("NewPrice");
dt.Columns.Add("VAT");
dt.Columns.Add("VAT_Cost");
dt.Columns.Add("TotalPrice");
dt.Rows.Add("Inv-01", "01", "1", "50", "10", "5", "45", "15", "6.75", "207");
dt.Rows.Add("Inv-01", "02", "1", "50", "10", "5", "45", "15", "6.75", "207");
dt.Rows.Add("Inv-01", "03", "1", "50", "10", "5", "45", "15", "6.75", "207");
dt.Rows.Add("Inv-01", "04", "1", "50", "10", "5", "45", "15", "6.75", "207");
dt.Rows.Add("Inv-02", "08", "1", "100", "10", "10", "90", "15", "13.5", "104");
dt.Rows.Add("Inv-03", "09", "1", "80", "10", "8", "72", "15", "10.8", "83 ");
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string invoiceNo = row.Cells["InvoiceNo"].Value.ToString();
string total = row.Cells["Total"].Value.ToString();
string paid = row.Cells["Paid"].Value.ToString();
string due = row.Cells["Due"].Value.ToString();
DataRow[] drs = dt.Select("InvoiceNo='" + invoiceNo + "'");
DataTable data = new DataTable();
if (drs.Length > 0)
{
data = drs.CopyToDataTable();
}
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream(pdfpath + (row.Index + 1) + ".pdf", FileMode.Create));
iTextSharp.text.Font boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
doc.Open();
PdfPTable pdfTable = null;
Paragraph paragraph = null;
pdfTable = new PdfPTable(4);
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.SetWidths(new float[] { .3f, .5f, .5f, .5f });
pdfTable.SpacingBefore = 20f;
pdfTable.DefaultCell.Border = 0;
pdfTable.AddCell(new Phrase("Inv No: ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase(invoiceNo, boldFont));
pdfTable.AddCell(new Phrase("Inv Date: ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)));
pdfTable.AddCell(new Phrase(DateTime.Today.ToString("dd.MM.yyyy"), boldFont));
doc.Add(pdfTable);
paragraph = new Paragraph(new Phrase("Item Descriptions", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
paragraph.SpacingBefore = 10f;
paragraph.Alignment = Element.ALIGN_CENTER;
doc.Add(paragraph);
pdfTable = new PdfPTable(8);
pdfTable.DefaultCell.Border = 0;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.SetWidths(new float[] { 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f, 0.8f });
pdfTable.AddCell(new Phrase("ItemCode", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("Qty", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("Price", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("Disc", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("Disc_Cost", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("NewPrice", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("VAT", FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase("VAT_Cost", FontFactory.GetFont("Calibri", 10)));
for (int i = 0; i < data.Rows.Count; i++)
{
string itemCode = data.Rows[i]["ItemCode"].ToString();
string qty = data.Rows[i]["Qtry"].ToString();
string price = data.Rows[i]["Price"].ToString();
string disc = data.Rows[i]["Disc"].ToString();
string disc_Cost = data.Rows[i]["Disc_Cost"].ToString();
string newPrice = data.Rows[i]["NewPrice"].ToString();
string vat = data.Rows[i]["VAT"].ToString();
string vat_Cost = data.Rows[i]["VAT_Cost"].ToString();
pdfTable.SpacingBefore = 20f;
pdfTable.AddCell(new Phrase(itemCode, FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(qty, FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(price, FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(disc, FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(disc_Cost, FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(newPrice, FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(vat, FontFactory.GetFont("Calibri", 10)));
pdfTable.AddCell(new Phrase(vat_Cost, FontFactory.GetFont("Calibri", 10)));
}
doc.Add(pdfTable);
paragraph = new Paragraph(new Phrase("Item Count: " + data.Rows.Count));
paragraph.SpacingBefore = 10f;
paragraph.Alignment = Element.ALIGN_LEFT;
doc.Add(paragraph);
paragraph = new Paragraph(new Phrase("Total: " + total + " Paid: " + paid + " Due: " + due));
paragraph.SpacingBefore = 10f;
paragraph.Alignment = Element.ALIGN_LEFT;
doc.Add(paragraph);
paragraph = new Paragraph(new Phrase("Thank U so much for visiting us", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)));
paragraph.SpacingBefore = 10f;
paragraph.Alignment = Element.ALIGN_CENTER;
doc.Add(paragraph);
doc.Close();
}
MessageBox.Show("Invoice Generated Successfully", "Stock Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
VB.Net
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
Private Sub BindGrid()
dataGridView1.AllowUserToAddRows = False
dataGridView1.Columns.Clear()
Dim saleID As DataGridViewColumn = New DataGridViewTextBoxColumn()
saleID.Name = "SaleID"
saleID.HeaderText = "Id"
saleID.DataPropertyName = "SaleID"
saleID.Width = 30
dataGridView1.Columns.Insert(0, saleID)
Dim invoiceNo As DataGridViewColumn = New DataGridViewTextBoxColumn()
invoiceNo.HeaderText = "Invoice"
invoiceNo.Name = "InvoiceNo"
invoiceNo.DataPropertyName = "InvoiceNo"
invoiceNo.Width = 60
dataGridView1.Columns.Insert(1, invoiceNo)
Dim total As DataGridViewColumn = New DataGridViewTextBoxColumn()
total.Name = "Total"
total.HeaderText = "Total"
total.DataPropertyName = "Total"
total.Width = 40
dataGridView1.Columns.Insert(2, total)
Dim paid As DataGridViewColumn = New DataGridViewTextBoxColumn()
paid.Name = "Paid"
paid.HeaderText = "Paid"
paid.DataPropertyName = "Paid"
paid.Width = 40
dataGridView1.Columns.Insert(3, paid)
Dim due As DataGridViewColumn = New DataGridViewTextBoxColumn()
due.Name = "Due"
due.HeaderText = "Due"
due.DataPropertyName = "Due"
due.Width = 30
dataGridView1.Columns.Insert(4, due)
Dim dt As DataTable = New DataTable()
dt.Columns.Add("SaleID")
dt.Columns.Add("InvoiceNo")
dt.Columns.Add("Total")
dt.Columns.Add("Paid")
dt.Columns.Add("Due")
dt.Rows.Add("1", "Inv-01", "207", "210", "-3")
dt.Rows.Add("2", "Inv-02", "104", "105", "-1")
dt.Rows.Add("3", "Inv-03", "83", "100", "-17")
dataGridView1.DataSource = Nothing
dataGridView1.DataSource = dt
Dim buttonColumn As DataGridViewButtonColumn = New DataGridViewButtonColumn()
buttonColumn.HeaderText = ""
buttonColumn.Width = 60
buttonColumn.Name = "buttonColumn"
buttonColumn.Text = "Print"
buttonColumn.UseColumnTextForButtonValue = True
dataGridView1.Columns.Insert(5, buttonColumn)
End Sub
Private Sub btnPDF_Click(sender As Object, e As EventArgs) Handles button1.Click
Dim pdfpath As String = "C:\Reports\"
If Not Directory.Exists(pdfpath) Then
Directory.CreateDirectory(pdfpath)
End If
Dim dt As DataTable = New DataTable()
dt.Columns.Add("InvoiceNo")
dt.Columns.Add("ItemCode")
dt.Columns.Add("Qtry")
dt.Columns.Add("Price")
dt.Columns.Add("Disc")
dt.Columns.Add("Disc_Cost")
dt.Columns.Add("NewPrice")
dt.Columns.Add("VAT")
dt.Columns.Add("VAT_Cost")
dt.Columns.Add("TotalPrice")
dt.Rows.Add("Inv-01", "01", "1", "50", "10", "5", "45", "15", "6.75", "207")
dt.Rows.Add("Inv-01", "02", "1", "50", "10", "5", "45", "15", "6.75", "207")
dt.Rows.Add("Inv-01", "03", "1", "50", "10", "5", "45", "15", "6.75", "207")
dt.Rows.Add("Inv-01", "04", "1", "50", "10", "5", "45", "15", "6.75", "207")
dt.Rows.Add("Inv-02", "08", "1", "100", "10", "10", "90", "15", "13.5", "104")
dt.Rows.Add("Inv-03", "09", "1", "80", "10", "8", "72", "15", "10.8", "83 ")
For Each row As DataGridViewRow In dataGridView1.Rows
Dim invoiceNo As String = row.Cells("InvoiceNo").Value.ToString()
Dim total As String = row.Cells("Total").Value.ToString()
Dim paid As String = row.Cells("Paid").Value.ToString()
Dim due As String = row.Cells("Due").Value.ToString()
Dim drs As DataRow() = dt.[Select]("InvoiceNo='" & invoiceNo & "'")
Dim data As DataTable = New DataTable()
If drs.Length > 0 Then
data = drs.CopyToDataTable()
End If
Dim doc As Document = New Document(PageSize.A4)
PdfWriter.GetInstance(doc, New FileStream(pdfpath & (row.Index + 1) & ".pdf", FileMode.Create))
Dim boldFont As iTextSharp.text.Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)
doc.Open()
Dim pdfTable As PdfPTable = Nothing
Dim paragraph As Paragraph = Nothing
pdfTable = New PdfPTable(4)
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.SetWidths(New Single() {0.3F, 0.5F, 0.5F, 0.5F})
pdfTable.SpacingBefore = 20.0F
pdfTable.DefaultCell.Border = 0
pdfTable.AddCell(New Phrase("Inv No: ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase(invoiceNo, boldFont))
pdfTable.AddCell(New Phrase("Inv Date: ", FontFactory.GetFont("Arial", 12, BaseColor.BLACK)))
pdfTable.AddCell(New Phrase(DateTime.Today.ToString("dd.MM.yyyy"), boldFont))
doc.Add(pdfTable)
paragraph = New Paragraph(New Phrase("Item Descriptions", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)))
paragraph.SpacingBefore = 10.0F
paragraph.Alignment = Element.ALIGN_CENTER
doc.Add(paragraph)
pdfTable = New PdfPTable(8)
pdfTable.DefaultCell.Border = 0
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.SetWidths(New Single() {0.8F, 0.8F, 0.8F, 0.8F, 0.8F, 0.8F, 0.8F, 0.8F})
pdfTable.AddCell(New Phrase("ItemCode", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("Qty", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("Price", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("Disc", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("Disc_Cost", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("NewPrice", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("VAT", FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase("VAT_Cost", FontFactory.GetFont("Calibri", 10)))
For i As Integer = 0 To data.Rows.Count - 1
Dim itemCode As String = data.Rows(i)("ItemCode").ToString()
Dim qty As String = data.Rows(i)("Qtry").ToString()
Dim price As String = data.Rows(i)("Price").ToString()
Dim disc As String = data.Rows(i)("Disc").ToString()
Dim disc_Cost As String = data.Rows(i)("Disc_Cost").ToString()
Dim newPrice As String = data.Rows(i)("NewPrice").ToString()
Dim vat As String = data.Rows(i)("VAT").ToString()
Dim vat_Cost As String = data.Rows(i)("VAT_Cost").ToString()
pdfTable.SpacingBefore = 20.0F
pdfTable.AddCell(New Phrase(itemCode, FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(qty, FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(price, FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(disc, FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(disc_Cost, FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(newPrice, FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(vat, FontFactory.GetFont("Calibri", 10)))
pdfTable.AddCell(New Phrase(vat_Cost, FontFactory.GetFont("Calibri", 10)))
Next
doc.Add(pdfTable)
paragraph = New Paragraph(New Phrase("Item Count: " & data.Rows.Count))
paragraph.SpacingBefore = 10.0F
paragraph.Alignment = Element.ALIGN_LEFT
doc.Add(paragraph)
paragraph = New Paragraph(New Phrase("Total: " & total & " Paid: " & paid & " Due: " & due))
paragraph.SpacingBefore = 10.0F
paragraph.Alignment = Element.ALIGN_LEFT
doc.Add(paragraph)
paragraph = New Paragraph(New Phrase("Thank U so much for visiting us", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)))
paragraph.SpacingBefore = 10.0F
paragraph.Alignment = Element.ALIGN_CENTER
doc.Add(paragraph)
doc.Close()
Next
MessageBox.Show("Invoice Generated Successfully", "Stock Report", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class