Click here to Skip to main content
13,043,092 members (53,656 online)
Rate this:
Please Sign up or sign in to vote.
See more:

i am using 2008. I want to create pdf with datagridview content. Any one help me
Posted 4-Oct-12 0:48am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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))
        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))
        '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()
        'PdfTable.TotalWidth = 490.0F
        PdfTable.TotalWidth = wid + 50.0F
        Dim PdfPCell As New PdfPCell
            '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("")
            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
                    increasselen = 20
                End If
                k = k + 1
                widths(k - 1) = (ds.Tables(0).Rows(kj).Item("width").ToString() + increasselen)
        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
                        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
                            PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
                        End If
                    Catch ex As Exception
                        PdfPCell = New PdfPCell(New Phrase("", font12Normal))
                    End Try

        End If

    End Sub

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100