1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| Private Sub Bt_calcul_Click(sender As System.Object, e As System.EventArgs) Handles Bt_calcul.Click
If Form4_Ticket.Dtgdv_ticket.ColumnCount = 0 Then
MsgBox("Veuillez choisir une entité et lancer une recherche svp")
Exit Sub
End If
'Récupération des données du Dgv dans une table.
Dim dt As DataTable = New DataTable
For Each col As DataGridViewColumn In Form4_Ticket.Dtgdv_ticket.Columns
dt.Columns.Add(col.HeaderText)
Next
For Each row As DataGridViewRow In Form4_Ticket.Dtgdv_ticket.Rows
Dim dRow As DataRow = dt.NewRow
For Each cell As DataGridViewCell In row.Cells
dRow(cell.ColumnIndex) = cell.Value
Next
dt.Rows.Add(dRow)
Next
'Agencement des colonnes pour le visuel voulu.
dt.Columns(8).SetOrdinal(1)
'Récupération des données organisées pour le PDF.
Dim pdfTable As New PdfPTable(dt.Columns.Count)
pdfTable.DefaultCell.Padding = 3
pdfTable.HorizontalAlignment = Element.ALIGN_CENTER
pdfTable.DefaultCell.NoWrap = True
'Formatage des données.
For Each column As DataColumn In dt.Columns
Dim cell As New PdfPCell(New Phrase(column.Caption))
cell.BackgroundColor = New iTextSharp.text.BaseColor(196, 228, 229)
pdfTable.AddCell(cell)
Next
Dim j As Integer = 0
For Each row As DataRow In dt.Rows
Dim cellPDF As New PdfPCell
If Form4_Ticket.Dtgdv_ticket.Rows(j).DefaultCellStyle.BackColor = Color.LightGray Then
cellPDF.BackgroundColor = New iTextSharp.text.BaseColor(234, 246, 208)
End If
If Form4_Ticket.Dtgdv_ticket.Rows(j).DefaultCellStyle.BackColor = Color.LightCyan Then
cellPDF.BackgroundColor = New iTextSharp.text.BaseColor(255, 251, 207)
End If
j = j + 1
For i As Integer = 0 To dt.Columns.Count - 1
Dim cell As String = row(i).ToString
Select Case i
Case 5
Dim dat As Date
If Date.TryParse(cell, dat) Then
cell = dat.ToShortDateString
End If
cellPDF.Phrase = New Phrase(cell)
cellPDF.Column.Alignment = Element.ALIGN_RIGHT
cellPDF.PaddingRight = 10
Case 4, 8
Dim dbl As Double
If Double.TryParse(cell, dbl) Then
cell = dbl.ToString("0")
End If
cellPDF.Phrase = New Phrase(cell)
cellPDF.Column.Alignment = Element.ALIGN_RIGHT
cellPDF.PaddingRight = 10
Case Else
cellPDF.Phrase = New Phrase(cell)
End Select
pdfTable.AddCell(cellPDF)
Next
Next
pdfTable.SetWidths(New Integer() {7, 6, 7, 25, 6, 8, 6, 25, 6})
Dim folderPath As String = "C:\Users\degecom\Desktop\PDF TEST"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As New FileStream(folderPath & "\test.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
AxAcroPDF1.src = "C:\Users\degecom\Desktop\PDF TEST\test.pdf"
End Sub |
Partager