Click here to Skip to main content
15,069,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,

i am using vb.net 2008. I want to create pdf with datagridview content. Any one help me
Posted

1 solution

if there is any ni replace it with ur datagridview name

nd these are the libraries u have to use
Imports iTextSharp
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Imports System
Imports com.lowagie.text.pdf.PdfPTable
Imports System.Windows.Forms
Imports System.Data.Sql
Imports System.Data.SqlClient


Public Sub ExportDataToPDFTable(ByVal path As String)
       Dim count As Integer
       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.COURIER, 9.0F, iTextSharp.text.Font.UNDERLINE Or iTextSharp.text.Font.BOLD, BaseColor.BLACK)
       Dim font12Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 7.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
       Dim font12Normal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 7.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

       Dim p1, p2 As New Phrase
       p2 = New Phrase(New Chunk(CompanyNm, font12BoldRed))
       p1 = New Phrase(New Chunk(reportname, font12BoldRed))
       'doc.Add(p2)
       doc.Add(p1)

       'Dim ds As New DataSet
       'Dim qry As String = "select * from advancegridsetting where head = " + head.ToString() + " and visible = 'true' "
       'Dim cmd As SqlCommand = New SqlCommand(qry, mycon)
       'Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
       'da.Fill(ds, "advancegridsetting")
       'If ds.Tables(0).Rows.Count = 0 Then
       '    MessageBox.Show("Save Advance grid setting", "Desire Softech")
       '    Exit Sub
       'End If

       Dim PdfTable As New PdfPTable(ds.Tables(0).Rows.Count)





       Dim wid As Decimal
       Dim abs As Integer
       For l As Integer = 0 To ds.Tables(0).Rows.Count - 1
           abs = abs + 1
           wid += ds.Tables(0).Rows(l).Item("width").ToString()
       Next
       'PdfTable.TotalWidth = 490.0F
       PdfTable.TotalWidth = wid + 50.0F

       Dim PdfPCell As New PdfPCell
       Try

           'For Each c As DataGridViewColumn In ni.Columns
           For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
               'If ni.Columns(i).Visible = True Then
               count = count + 1
               Dim idColumn As DataColumn = New DataColumn()
               idColumn.ColumnName = ds.Tables(0).Rows(i).Item("HeaderText")

               PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
               PdfPCell = New PdfPCell(New Phrase(New Chunk(idColumn.ToString(), font12Bold)))
               PdfTable.AddCell(PdfPCell)

           Next

           ' PdfTable.AddCell("")

           Dim increasselen As Integer
           Dim widths(count - 1) As Single
           Dim k As Integer
           For kj As Integer = 0 To ds.Tables(0).Rows.Count - 1

               If ni.Columns(kj).HeaderText.Length >= 6 Then
                   increasselen = 40
               Else

                   increasselen = 20
               End If
               k = k + 1
               widths(k - 1) = (ds.Tables(0).Rows(kj).Item("width").ToString() + increasselen)

           Next
           PdfTable.SetTotalWidth(widths)
       Catch ex As Exception
       End Try
       ' doc.Add(PdfTable)



       Dim dt As DataTable = dtFromGrid
       If dt IsNot Nothing Then
           For rows As Integer = 0 To ni.Rows.Count - 1
               For column As Integer = 0 To ds.Tables(0).Rows.Count - 1
                   Try

                       PdfPCell = New PdfPCell(New Phrase(ni.Rows(rows).Cells(ds.Tables(0).Rows(column).Item("name").ToString()).Value.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)
                   Catch ex As Exception
                       PdfPCell = New PdfPCell(New Phrase("", font12Normal))
                       PdfTable.AddCell(PdfPCell)
                   End Try


               Next

           Next
           doc.Add(PdfTable)
       End If
       doc.Close()


   End Sub
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900