Wednesday 21 December 2011

PDF From Datagridview Data using itextsharp font


Import  these files
Imports iTextSharp
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.Data.Odbc
Imports System.IO

Add itextsharp.dll as reference to your project.(Already posted in my Blog)

Button PDF Click Event is given below

Private Sub btnPdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPdf.Click
        SaveFileDialog1.ShowDialog()
        If SaveFileDialog1.FileName = "" Then
            MsgBox("Enter Filename to create PDF")
            Exit Sub
        Else
            ExportDataToPDFTable()
            MsgBox("PDF Created Successfully")
        End If
    End Sub


Add These Two Functions Named GetDataTable() and ExportDataToPDFTable()


GetDataTable() function is used to read Data's from datgridview and creates a DataTable.
Private Function GetDataTable() As DataTable
        Dim dataTable As New DataTable("MyDataTable")
        'Create another DataColumn Name
        Dim dataColumn_1 As New DataColumn(DataGridView1.Columns(0).HeaderText.ToString(), GetType(String))
        dataTable.Columns.Add(dataColumn_1)
        Dim dataColumn_2 As New DataColumn(DataGridView1.Columns(1).HeaderText.ToString(), GetType(String))
        dataTable.Columns.Add(dataColumn_2)
        Dim dataColumn_3 As New DataColumn(DataGridView1.Columns(2).HeaderText.ToString(), GetType(String))
        dataTable.Columns.Add(dataColumn_3)
        Dim dataColumn_4 As New DataColumn(DataGridView1.Columns(3).HeaderText.ToString(), GetType(String))
        dataTable.Columns.Add(dataColumn_4)
        Dim dataColumn_5 As New DataColumn(DataGridView1.Columns(4).HeaderText.ToString(), GetType(String))
        dataTable.Columns.Add(dataColumn_5)
        'Now Add some row to newly created dataTable
        Dim dataRow As DataRow
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            dataRow = dataTable.NewRow()
            ' Important you have create New row
            dataRow(DataGridView1.Columns(0).HeaderText.ToString()) = DataGridView1.Rows(i).Cells(0).Value.ToString()
            dataRow(DataGridView1.Columns(1).HeaderText.ToString()) = DataGridView1.Rows(i).Cells(1).Value.ToString()
            dataRow(DataGridView1.Columns(2).HeaderText.ToString()) = DataGridView1.Rows(i).Cells(2).Value.ToString()
            dataRow(DataGridView1.Columns(3).HeaderText.ToString()) = DataGridView1.Rows(i).Cells(3).Value.ToString()
            dataRow(DataGridView1.Columns(4).HeaderText.ToString()) = DataGridView1.Rows(i).Cells(4).Value.ToString()

            dataTable.Rows.Add(dataRow)
        Next
        dataTable.AcceptChanges()
        Return dataTable
    End Function

ExportDataToPDFTable() function is used to create PDF Document.
    Private Sub ExportDataToPDFTable()
        Dim paragraph As New Paragraph
        Dim doc As New Document(iTextSharp.text.PageSize.A4, 40, 40, 40, 10)
        Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(SaveFileDialog1.FileName + ".pdf", FileMode.Create))
        doc.Open()

        Dim font12BoldRed As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.UNDERLINE Or iTextSharp.text.Font.BOLDITALIC, BaseColor.RED)
        Dim font12Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
        Dim font12Normal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

        Dim p1 As New Phrase
        p1 = New Phrase(New Chunk("PDF From Datagridview Data", font12BoldRed))
        doc.Add(p1)

        'Create instance of the pdf table and set the number of column in that table
        Dim PdfTable As New PdfPTable(5)
        PdfTable.TotalWidth = 490.0F
        'fix the absolute width of the table
        PdfTable.LockedWidth = True
        'relative col widths in proportions - 1,4,1,1 and 1
        Dim widths As Single() = New Single() {1.0F, 4.0F, 1.0F, 1.0F, 1.0F}
        PdfTable.SetWidths(widths)
        PdfTable.HorizontalAlignment = 1 ' 0 --> Left, 1 --> Center, 2 --> Right
        PdfTable.SpacingBefore = 2.0F

        'pdfCell Decleration
        Dim PdfPCell As PdfPCell = Nothing

        'Assigning values to each cell as phrases
        PdfPCell = New PdfPCell(New Phrase(New Chunk("Taxcode", font12Bold)))
        'Alignment of phrase in the pdfcell
        PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
        'Add pdfcell in pdftable
        PdfTable.AddCell(PdfPCell)
        PdfPCell = New PdfPCell(New Phrase(New Chunk("Tax Name", font12Bold)))
        PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
        PdfTable.AddCell(PdfPCell)
        PdfPCell = New PdfPCell(New Phrase(New Chunk("Cess Tax", font12Bold)))
        PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
        PdfTable.AddCell(PdfPCell)
        PdfPCell = New PdfPCell(New Phrase(New Chunk("Sales Tax", font12Bold)))
        PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
        PdfTable.AddCell(PdfPCell)
        PdfPCell = New PdfPCell(New Phrase(New Chunk("Other Tax", font12Bold)))
        PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
        PdfTable.AddCell(PdfPCell)

        Dim dt As DataTable = GetDataTable()
        If dt IsNot Nothing Then
            'Now add the data from datatable to pdf table
            For rows As Integer = 0 To dt.Rows.Count - 1
                For column As Integer = 0 To dt.Columns.Count - 1
                    PdfPCell = New PdfPCell(New Phrase(dt.Rows(rows)(column).ToString(), font12Normal))
                    If column = 0 Or column = 1 Then
                        PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                    Else
                        PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
                    End If
                    PdfTable.AddCell(PdfPCell)
                Next
            Next
            'Adding pdftable to the pdfdocument
            doc.Add(PdfTable)
        End If
        doc.Close()
    End Sub
 



Monday 19 December 2011

Make PDF Using RichTextBox


For making PDF File, you need to add component file called itextsharp.dll to your project.
Itextsharp.dll can be downloaded from http://sourceforge.net/projects/itextsharp/.
Import below headers in your project
Imports System
Imports System.IO
Imports iTextSharp
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Write the below code on the click event of SavePDF Button.
Private Sub btnSavePdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePdf.Click
        ' Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
        Dim doc As New Document(iTextSharp.text.PageSize.A4, 40, 40, 40, 10)
        ' Using Savefiledialog
        SaveFileDialog1.ShowDialog()
        If SaveFileDialog1.FileName = "" Then
MsgBox("pdf not created. Enter Filename to create pdf")
Exit Sub
        End If

        Try
            Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(SaveFileDialog1.FileName + ".pdf", FileMode.Create))
            ' Open Document to write
            doc.Open()

            ' Declaring p1 as Paragraph
            Dim p1 As New Paragraph
            ' setting line spacing between paragraph to 1.
            Dim linespacing As Single = 1.0F
            ' Setting font for Paragraph to arial
            p1.Font = FontFactory.GetFont("ARIAL", 9.0F)
            ' Adding RichTextBox to Paragraph
            p1.Add(rtb.Text)
            ' Adding Paragraph to Document
            doc.Add(p1)

            ' Document Closing
            doc.Close()

        Catch docEx As DocumentException
            MessageBox.Show(docEx.Message)
            ' handle IO exception
        Catch ioEx As IOException
            ' hahndle other exception if occurs
            MessageBox.Show(ioEx.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            'Close document and writer
            doc.Close()
            MsgBox("pdf Created Successfully")
        End Try

    End Sub

Now Run the Program
When SavePDF Button is Clicked, Program will ask where to save the PDF File. Type Filename, Browse the location to save the file, and then  Press OK.

If the file is saved, you will get message as “pdf Created Successfully”.
If the file is cancelled, you will get message as “pdf not created. Enter Filename to create pdf

Saved PDF File is given below. 


Adding dll component to Project

Goto Solution Explorer, Right click on the ProjectName  and select Add Refence.
New window will appear.

 Goto tab Browse and select component file (dll,olb,ocx,exe) wants to add in your project  from your System. 
Then click OK.
Now you added a component file relevent for your project.

Check System Path Valid or Not


Imports System.IO
For this, I am using Textbox, two buttons named btnCheck and btnReset and a label for showing result.
Result will shown when check button is clicked after entering any system path in the textbox. 
If system path exist, result will be System Path Valid, otherwise System Path Not Valid.
Click event of btnCheck is Shown below
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        If txtPath.Text = "" Then
            MsgBox("Enter Any System Path to Check")
            txtPath.Focus()
            Exit Sub
        End If
        Dim di As New DirectoryInfo(txtPath.Text)
        If di.Exists = True Then
            lblResult.Text = lblResult.Text + " Valid"
        Else
            lblResult.Text = lblResult.Text + " Not Valid"
        End If
    End Sub

Click Event of btnReset is given below
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        txtPath.Text = ""
        lblResult.Text = "System Path"
    End Sub

I am using one more event, for textbox. Ie., textchange Event
When textbox content is changed, the result label set to System Path.
Private Sub txtPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPath.TextChanged
        lblResult.Text = "System Path"
    End Sub

 When btnReset click, the content in the textbox set to Null and the result label set to  “System Path”.

Copy TextBox Content To Another while Typing


For this, I am using Two Textboxes named txtpermanentadd and txtpresentadd. Screenshot given below.
To make textbox for typing  address, change multiline property of textbox to True.
Write the below code in to the TextChanged event of txtpermanantadd.

Private Sub txtPermanantadd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPermanantadd.TextChanged
        txtPresentadd.Text = txtPermanantadd.Text
    End Sub

Copy TextBox Content To Another using Checkbox


For this, I am using Two textboxes named txtpermanentadd and txtpresentadd and a checkbox named checkbox1. Screenshot given below.
To make textbox for typing  address, change multiline property of textbox to True.
Write the below code in the Checkedchanged event of Checkbox1
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            If txtPermanantadd.Text <> "" Then
                txtPresentadd.Text = txtPermanantadd.Text
            Else
                MessageBox.Show("Permanent Address field is empty")
                CheckBox1.Checked = False
                Exit Sub
            End If
        Else
            txtPresentadd.Text = ""
        End If
    End Sub
The permanentaddress is copied to presentaddress when the checkbox1 is checked, otherwise clear the content in the present address field.

Sunday 11 December 2011

Convert Number To Words (Integer Or Decimal)


For this , we need a form and a class.
It will convert 8 digit Number to its equivalent figure.
Form consist of 2 textbox and a button. One textbox for reading input, another for displaying output when Button is clicked.

My program will convert both integer and double values



Number to word Class file

Module modNumbertoWord
    Dim Character As String
    Dim num1, num2 As String
    Dim decpart, intpart, AmtStr As String
    Dim ctr As Integer
    Dim Amt As Double

    Public Class Num2Words

        Public Function split(ByVal AB As Double) As String
            Dim sb1 As New System.Text.StringBuilder
            Dim sb2 As New System.Text.StringBuilder
            Dim str As String
            AmtStr = Convert.ToString(AB)

            ctr = 0
            decpart = "0"
            intpart = "0"
            For Each chnum In AmtStr
                If chnum <> "." And ctr = 0 Then
                    sb1.Append(chnum)
                    intpart = sb1.ToString()
                ElseIf chnum = "." Then
                    ctr = 1
                Else
                    sb2.Append(chnum)
                    decpart = sb2.ToString()
                End If
            Next

            If decpart.Length() = 1 Then
                decpart = decpart + "0"
            End If

            Amt = Convert.ToInt64(intpart)
            split = NUMMM(Amt)

            If decpart = "00" Then
                split = split + " Rupees"
            Else
                str = split + " Rupees and "
                Amt = Convert.ToInt64(decpart)
                split = NUMMM(Amt)
                split = str + split + " Paise"
            End If

        End Function


        Public Function NUMMM(ByVal A As Double) As String
            On Error Resume Next
            Dim MM
            Dim IM
            MM = Microsoft.VisualBasic.Len(A)
            IM = Microsoft.VisualBasic.Left(A, Microsoft.VisualBasic.Len(A))
            If Microsoft.VisualBasic.Len(IM) = 9 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 2), " Crore ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 2), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 6), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 7), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 9), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 8 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Crore ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 2), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 5), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 6), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 8), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 7 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 2), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 5), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 7), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 6 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 6), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 5 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 5), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 4 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 2), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 3 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) <= 2 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, Microsoft.VisualBasic.Len(IM)), "")
            End If
        End Function

        Public Function conv(ByVal co As Integer, ByVal sSTR As String) As String
            On Error Resume Next
            Dim Am As Integer
            Character = ""
            'If co = 0 Then CharacteR = " Zero"
            If co = 1 Then Character = " One" & sSTR
            If co = 2 Then Character = " Two" & sSTR
            If co = 3 Then Character = " Three" & sSTR
            If co = 4 Then Character = " Four" & sSTR
            If co = 5 Then Character = " Five" & sSTR
            If co = 6 Then Character = " Six" & sSTR
            If co = 7 Then Character = " Seven" & sSTR
            If co = 8 Then Character = " Eight" & sSTR
            If co = 9 Then Character = " Nine" & sSTR
            If co = 10 Then Character = " Ten" & sSTR
            If co = 11 Then Character = " Eleven" & sSTR
            If co = 12 Then Character = " Twelve" & sSTR
            If co = 13 Then Character = " Thirteen" & sSTR
            If co = 14 Then Character = " Fourteen" & sSTR
            If co = 15 Then Character = " Fifteen" & sSTR
            If co = 16 Then Character = " Sixteen" & sSTR
            If co = 17 Then Character = " Seventeen" & sSTR
            If co = 18 Then Character = " Eighteen" & sSTR
            If co = 19 Then Character = " Nineteen" & sSTR
            If Character <> "" Then
                GoTo 10
            End If
            If Microsoft.VisualBasic.Len(CStr(co)) = 2 Then
                Am = Left(co, 1)
                If Am = 2 Then
                    Character = "Twenty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 3 Then
                    Character = "Thirty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 4 Then
                    Character = "Fourty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 5 Then
                    Character = "Fifty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 6 Then
                    Character = "Sixty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 7 Then
                    Character = "Seventy"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 8 Then
                    Character = "Eighty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 9 Then
                    Character = "Ninty"
                    Am = Right(co, 1)
                    'If Am = 1 Then Character = Character & " One" & sSTR
                    'If Am = 2 Then Character = Character & " Two" & sSTR
                    'If Am = 3 Then Character = Character & " Three" & sSTR
                    'If Am = 4 Then Character = Character & " Four" & sSTR
                    'If Am = 5 Then Character = Character & " Five" & sSTR
                    'If Am = 6 Then Character = Character & " Six" & sSTR
                    'If Am = 7 Then Character = Character & " Seven" & sSTR
                    'If Am = 8 Then Character = Character & " Eight" & sSTR
                    'If Am = 9 Then Character = Character & " Nine" & sSTR
                    nUMM(Am, sSTR)
                    GoTo 10
                End If
            End If
10:         conv = Character
        End Function

        Private Sub nUMM(ByVal AM As Integer, ByVal sSTR As String)
            If AM = 1 Then Character = Character & " One" & sSTR
            If AM = 2 Then Character = Character & " Two" & sSTR
            If AM = 3 Then Character = Character & " Three" & sSTR
            If AM = 4 Then Character = Character & " Four" & sSTR
            If AM = 5 Then Character = Character & " Five" & sSTR
            If AM = 6 Then Character = Character & " Six" & sSTR
            If AM = 7 Then Character = Character & " Seven" & sSTR
            If AM = 8 Then Character = Character & " Eight" & sSTR
            If AM = 9 Then Character = Character & " Nine" & sSTR
            If AM = 0 Then Character = Character & sSTR
        End Sub
    End Class
End Module

btnClick click event in Form

Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
        Dim CLS As New Num2Words
        txtWord.Text = ""
        'For Displaying Integer Values
        txtWord.Text = CLS.NUMMM(txtNo.Text)
        'For Displaying Double value
        'txtWord.Text = CLS.split(txtNo.Text)
        txtWord.Text = txtWord.Text + " Only"
    End Sub

 
For Displaying Integer Values Type below code
  txtWord.Text = CLS.NUMMM(txtNo.Text)

                  OR

For Displaying Double value Type Below code
  txtWord.Text = CLS.split(txtNo.Text)