question

OSD-4642 avatar image
0 Votes"
OSD-4642 asked OSD-4642 edited

DataGridView to PDF / using iTextSharp /VB.Net Forms Application

Hi,

I am trying to save tabular data in PDF format and following this example:
Export-Windows-Forms-DataGridView-to-PDF-using-iTextSharp-C-and-VBNet.aspx

I am getting following error message when exporting data as PDF and according to error message, following line is culprit: Line 45 in complete code list.

 pdfTable.AddCell(cell.Value.ToString())

93182-image.png

Here comes the full code:, what might I am doing wrong?

 Imports System.IO
 Imports System.Data
 Imports System.Reflection
 Imports iTextSharp.text
 Imports iTextSharp.text.pdf
 Public Class Form1
     Public Sub New()
         InitializeComponent()
         Me.BindDataGridView()
     End Sub
    
     Private Sub BindDataGridView()
         Dim dt As New DataTable()
         dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)),
                                            New DataColumn("Name", GetType(String)),
                                            New DataColumn("Country", GetType(String))})
         dt.Rows.Add(1, "John Hammond", "United States")
         dt.Rows.Add(2, "Mudassar Khan", "India")
         dt.Rows.Add(3, "Suzanne Mathews", "France")
         dt.Rows.Add(4, "Robert Schidner", "Russia")
         Me.DataGridView1.DataSource = dt
     End Sub
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
     End Sub
    
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         'Creating iTextSharp Table from the DataTable data
         Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
         pdfTable.DefaultCell.Padding = 3
         pdfTable.WidthPercentage = 30
         pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
         pdfTable.DefaultCell.BorderWidth = 1
    
         'Adding Header row
         For Each column As DataGridViewColumn In DataGridView1.Columns
             Dim cell As New PdfPCell(New Phrase(column.HeaderText))
             'cell.BackgroundColor = New iTextSharp.text.(240, 240, 240)
             pdfTable.AddCell(cell)
         Next
    
         'Adding DataRow
         For Each row As DataGridViewRow In DataGridView1.Rows
             For Each cell As DataGridViewCell In row.Cells
                 pdfTable.AddCell(cell.Value.ToString())
             Next
         Next
    
         'Exporting to PDF
         Dim folderPath As String = "C:\PDFs\"
         If Not Directory.Exists(folderPath) Then
             Directory.CreateDirectory(folderPath)
         End If
         Using stream As New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
             Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
             PdfWriter.GetInstance(pdfDoc, stream)
             pdfDoc.Open()
             pdfDoc.Add(pdfTable)
             pdfDoc.Close()
             stream.Close()
         End Using
     End Sub
    
     Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    
     End Sub
 End Class




dotnet-visual-basic
image.png (31.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered OSD-4642 commented

Try adding an If:

 'Adding DataRow
 For Each row As DataGridViewRow In DataGridView1.Rows
    If row.IsNewRow Then Continue For
    . . .
 Next



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Perfect, thanks Viorel, worked fine.

0 Votes 0 ·
OSD-4642 avatar image
0 Votes"
OSD-4642 answered OSD-4642 edited

@Viorel-1

Hello again,

Now everything was working fine until I check the application on PC other than it was created and I am having issues that Could not load file or assembly "itextsharp," Version=5.5.13.2

I have installed itextsharp with following command on PC where I am running Visual Studio, and it is not practical for me to install it on all devices, any thoughts how I can integrate it to the solution itself?

Install-Package iTextSharp

And Copy Local is already True.
94758-image.png



image.png (85 B)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.