Create PDF Report from database using C# and VB.Net in Windows Application

smile
 
on Jul 27, 2021 01:39 AM
Sample_177487.zip
542 Views

I have a two tables which contain data like that

tblItemSold

InvoiceNo

ItemCode

Qtry

Price

Disc

Disc_Cost

NewPrice

VAT

VAT_Cost

TotalPrice

Inv-01

01

1

50

10

5

45

15

6.75

207

Inv-01

02

1

50

10

5

45

15

6.75

207

Inv-01

03

1

50

10

5

45

15

6.75

207

Inv-01

04

1

50

10

5

45

15

6.75

207

Inv-02

08

1

100

10

10

90

15

13.5

104

Inv-03

09

1

80

10

8

72

15

10.8

83

tblSales

SaleID

InvoiceNo

Total

Paid

Due

1

Inv-01

207

210

-3

2

Inv-02

104

105

-1

3

Inv-03

83

100

-17

SalesHistory in Windows Form inside dataGridview is following

SaleID

InvoiceNo

Total

Paid

Due

1

Inv-01

207

210

-3

2

Inv-02

104

105

-1

3

Inv-03

83

100

-17

Now, When I click on Inv-01 or Inv-02 0r Inv-03 it should generate receipt accordingly invoice in pdf format like that

Inv No. Inv-01                                        Inv Date: 27.07.2021

                                  Item Descriptions

ItemCode

Qtry

Price

Disc

Disc_Cost

NewPrice

VAT

VAT_Cost

01

1

50

10

5

45

15

6.75

02

1

50

10

5

45

15

6.75

03

1

50

10

5

45

15

6.75

04

1

50

10

5

45

15

6.75

Item Count: 4

Total: 207               Paid: 210                                         Due: -3

                             Thank U so much for visiting us

here is my code:

 

        private void tbPrint_Click(object sender, EventArgs e)
        {
            string pdfpath = @"C:\Reports\";
            if (!Directory.Exists(pdfpath))
            {
                Directory.CreateDirectory(pdfpath);
            }
            Document doc = new Document(PageSize.A4);
            PdfWriter.GetInstance(doc, new FileStream(pdfpath + "Reciept.pdf", FileMode.Create));
            doc.Open();
            iTextSharp.text.Font boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 8);


            con = new SqlDbConnect();
            con.SqlQuery("select logo,SystemName,Address,Phone,Email FROM tblSystem");
            con.RdrEx();
            while (con.Rdr.Read())
            {

                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 };
                table1.SetWidths(widths1);
                Byte[] bytes = (Byte[])con.Rdr[0];
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bytes);
                image.ScaleAbsolute(90f, 70f);
                PdfPCell bottom1 = new PdfPCell((image));
                bottom1.Padding = 5f;
                bottom1.BackgroundColor = iTextSharp.text.Color.WHITE;
                bottom1.Border = 0;
                bottom1.HorizontalAlignment = 1;
                table1.AddCell(bottom1);
                PdfPTable nested1 = new PdfPTable(1);
                float[] width = new float[] { 0.1f };
                nested1.SetWidths(width);
                nested1.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
                nested1.DefaultCell.Padding = 7f;
                nested1.DefaultCell.HorizontalAlignment = 1;
                nested1.DefaultCell.Border = 0;
                nested1.AddCell(new Phrase(con.Rdr[1].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 15)));
                nested1.AddCell(new Phrase(con.Rdr[2].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14)));
                nested1.AddCell(new Phrase("Email: " + con.Rdr[4].ToString() + "\t Phone No: " + con.Rdr[3].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)));
                
                PdfPCell nesthousing1 = new PdfPCell(nested1);
                nesthousing1.Colspan = 2;
                nesthousing1.Padding = 0f;
                nesthousing1.Border = 0;
                table1.AddCell(nesthousing1);
                doc.Add(table1);
                Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(1.0F, 100.0F, iTextSharp.text.Color.BLACK, Element.ALIGN_LEFT, 1)));
                doc.Add(p);
            }
            con.conClose();

            con = new SqlDbConnect();
            con.SqlQuery(@"select s.InvoiceNo,s.InvoiceDate,si.ItemCode,ItemName,U_Name,si.Price,si.Pri_Dis,si.Pri_Cost,si.Quantity,si.NewPrice,s.Payable,Dis_Cost,s.G_Total,Paid,Due from tblSales as s 
                            inner join tblItemSold as si on s.InvoiceNo=si.InvoiceNo
                            inner join tblStores as st on st.ItemCode= si.ItemCode 
                            inner join tblUnit as u on st.UnitID=u.UnitID
                            where s.InvoiceNo='Recipt-000001' order by ItemName;");
            con.Cmd.Parameters.Add(new SqlParameter("@No", this.txtInv.Text.Trim()));
            paging.SelectCommand = con.Cmd;
            sBuilder = new SqlCommandBuilder(paging);
            ds = new DataSet();
            paging.Fill(ds);
            dGVPrint.DataSource = ds.Tables[0].DefaultView;
            if (ds.Tables[0].Rows.Count < 1)
            {
                MessageBox.Show("No Record Found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            /*Int64 sum = 0;
            foreach (DataGridViewRow r in this.dGVPrint.Rows)
            {
                Int64 num = Convert.ToInt64(r.Cells[4].Value);
                sum = sum + num;
            }
            string payable = sum.ToString();*/
            int rc = ds.Tables[0].Rows.Count;
            string rcc = Convert.ToString(rc);

            int i = 0;
            string No = txtInv.Text;
            string Date = null;
            string Code = null;
            string Name = null;
            string Unit = null;
            string Price = null;
            string disc = null;
            string discAmt = null;
            string Qty = null;
            string SubT = null;
            string Payable = null;
            string Gdisc = null;
            string GTot = null;
            string Totpay = null;
            string PayDue = null;
            
          
            PdfPTable tablea = new PdfPTable(2);
            tablea.TotalWidth = 500f;
            tablea.LockedWidth = true;
            tablea.DefaultCell.Padding = 3;            
            tablea.DefaultCell.Border = 0;
            tablea.HorizontalAlignment = Element.ALIGN_CENTER;
            tablea.AddCell(new Phrase("Customer Name :", FontFactory.GetFont("Calibri", 8)));
            tablea.AddCell(new Phrase("VAT No. :", FontFactory.GetFont("Calibri", 8)));
            tablea.AddCell(new Phrase("Invoice No. :\t" + No, boldFont));            
            tablea.AddCell(new Phrase("Invoice Date :", FontFactory.GetFont("Calibri", 8))); 
            tablea.AddCell(new Phrase("Payment Method :", FontFactory.GetFont("Calibri", 8)));
            tablea.AddCell(new Phrase("Address :", FontFactory.GetFont("Calibri", 8)));
            tablea.AddCell(new Phrase("Phone :", FontFactory.GetFont("Calibri", 8)));
            tablea.AddCell(new Phrase("" + DateTime.Now.ToString(), FontFactory.GetFont("Calibri", 8)));
           
            doc.Add(tablea);
            
            PdfPTable table = new PdfPTable(10);
            table.DefaultCell.Padding = 5;
            table.TotalWidth = 500f;
            table.LockedWidth = true;
            float[] widths11 = new float[] { 0.05f, 0.1f, 0.04f, 0.04f, 0.05f, 0.05f, 0.05f, 0.05f, 0.05f, 0.05f };
            table.SetWidths(widths11);
            table.HorizontalAlignment = Element.ALIGN_CENTER;
            table.AddCell(new Phrase("Item Number", boldFont));
            table.AddCell(new Phrase("Item Name", boldFont));
            table.AddCell(new Phrase("Unit", boldFont));
            table.AddCell(new Phrase("Price", boldFont));
            table.AddCell(new Phrase("Discount (%)", boldFont));
            table.AddCell(new Phrase("Discount Amount", boldFont));
            table.AddCell(new Phrase("Quantity", boldFont));
            table.AddCell(new Phrase("Sale Price", boldFont));
            table.AddCell(new Phrase("VAT Amount", boldFont));
            table.AddCell(new Phrase("Total Including VAT", boldFont));
            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                Code = ds.Tables[0].Rows[i].ItemArray[2].ToString();
                Name = ds.Tables[0].Rows[i].ItemArray[3].ToString();
                Unit = ds.Tables[0].Rows[i].ItemArray[4].ToString();
                Price = ds.Tables[0].Rows[i].ItemArray[5].ToString();
                disc = ds.Tables[0].Rows[i].ItemArray[6].ToString();
                discAmt = ds.Tables[0].Rows[i].ItemArray[7].ToString();
                Qty = ds.Tables[0].Rows[i].ItemArray[8].ToString();
                SubT = ds.Tables[0].Rows[i].ItemArray[9].ToString();
                Payable = ds.Tables[0].Rows[i].ItemArray[10].ToString();
                Gdisc = ds.Tables[0].Rows[i].ItemArray[11].ToString();
                GTot = ds.Tables[0].Rows[i].ItemArray[12].ToString();
                Totpay = ds.Tables[0].Rows[i].ItemArray[13].ToString();
                PayDue = ds.Tables[0].Rows[i].ItemArray[14].ToString();

                table.AddCell(new Phrase(Code, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase(Name, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase(Unit, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase(Price, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase(disc, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase(discAmt, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase(Qty, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase(SubT, FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase("", FontFactory.GetFont("Calibri", 8)));
                table.AddCell(new Phrase("", FontFactory.GetFont("Calibri", 8)));               
                
            }
            doc.Add(table);

            PdfPTable table11 = new PdfPTable(2);
            table11.DefaultCell.Padding = 3f;
            table11.DefaultCell.BackgroundColor = iTextSharp.text.Color.WHITE;
            table11.DefaultCell.Border = 0;
            table11.HorizontalAlignment = Element.ALIGN_CENTER;
            table11.TotalWidth = 500f;
            table11.LockedWidth = true;
            float[] widths111 = new float[] { 0.1f, 0.1f };
            table11.SetWidths(widths111);
            table11.AddCell(new Phrase("Item Count :" + rcc.ToString(), FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Total Before Disc :" + Payable.ToString(), FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Discount Amount :" + Gdisc.ToString(), FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Total After Discount :" + GTot.ToString(), FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Total VAT Amount :", FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Total Due :", FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Cash Paid Amount :" + Totpay.ToString(), FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Card Paid Amount :", FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Change Amount :" + PayDue.ToString(), FontFactory.GetFont("Calibri", 8)));
            table11.AddCell(new Phrase("Note :", FontFactory.GetFont("Calibri", 8)));
            doc.Add(table11);
            doc.Close();
            MessageBox.Show("Invoice Generated Successfully", "Stock Report", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
           
        }

How to get solution pls.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 28, 2021 01:27 AM

Hi smile,

Check this example. Now please take its reference and correct your code.

For this example i have used dynamic datatable. You need to fetch the record from database.

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 dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 5)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
            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();

            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 ");

            DataRow[] drs = dt.Select("InvoiceNo='" + invoiceNo + "'");
            DataTable data = new DataTable();
            if (drs.Length > 0)
            {
                data = drs.CopyToDataTable();
            }

            string pdfpath = @"C:\Reports\";
            if (!Directory.Exists(pdfpath))
            {
                Directory.CreateDirectory(pdfpath);
            }
            Document doc = new Document(PageSize.A4);
            PdfWriter.GetInstance(doc, new FileStream(pdfpath + "Reciept.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 dataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dataGridView1.CellContentClick
        If e.ColumnIndex = 5 Then
            Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
            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 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 ")
            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 pdfpath As String = "C:\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 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()
            MessageBox.Show("Invoice Generated Successfully", "Stock Report", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class

Generated PDF