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
|
Public Sub SendToPDF(dgv As DataGridView, folderPath As String, titre As String, col_h_debut As Integer, col_b_debut As Integer, col_h_fin As Integer, col_b_fin As Integer)
Try
' Format de la page PDF à générer
Dim doc As Document = New Document(PageSize.A4.Rotate, 30.0F, 30.0F, 20.0F, 20.0F)
' Emplacement du fichier à créer
' Nom et droits d'accès du PDF créé
PdfWriter.GetInstance(doc, New FileStream(folderPath & titre & ".pdf", FileMode.Create))
' Police pour le titre
Dim titleFont As Font = New Font(iTextSharp.text.Font.TIMES_ROMAN, 28.0F, iTextSharp.text.Font.BOLD, Color.BLACK)
' Police pour le tableau
Dim tableFont As Font = New Font(iTextSharp.text.Font.TIMES_ROMAN, 7.0F, iTextSharp.text.Font.NORMAL, Color.BLACK)
' Police pour les entetes de colonnes
Dim headerFont As Font = New Font(iTextSharp.text.Font.TIMES_ROMAN, 7.0F, iTextSharp.text.Font.BOLD, Color.BLACK)
' Définition du tableau
Dim table As PdfPTable = New PdfPTable((col_h_fin - col_h_debut) + 1)
table.HorizontalAlignment = 1 ' 0 - gauche, 1 - centré, 2 - droite
table.SpacingBefore = 1.0F
table.WidthPercentage = 100
doc.Open()
'Ajou d'un logo
Dim logo = Path.Combine(My.Application.Info.DirectoryPath.ToString() & "\logo.png")
doc.Add(iTextSharp.text.Image.GetInstance(logo))
'Ajout d'un titre
Dim titre2 As String
titre2 = Chr(10) & titre & Chr(10)
Dim title As Phrase = New Phrase(New Chunk(titre2, titleFont))
doc.Add(title)
'Entêtes de colonnes
For i As Integer = col_h_debut To col_h_fin
Dim cell As New PdfPCell(New Phrase(New Chunk(dgv.Columns(i).HeaderText, headerFont)))
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER
cell.BackgroundColor = New iTextSharp.text.Color(204, 229, 255)
table.AddCell(cell)
Next
' Ajout des rows
For i As Integer = 0 To dgv.Rows.Count - 1
For j As Integer = col_b_debut To col_b_fin
Dim cellule As New PdfPCell(New Phrase(dgv(j, i).Value.ToString(), tableFont))
'aligmenet des cellules en fonction du contenu
cellule.VerticalAlignment = Element.ALIGN_MIDDLE
Select Case dgv.Columns(j).ValueType.ToString
Case ("System.Double")
cellule.HorizontalAlignment = Element.ALIGN_RIGHT
Case ("System.Int32")
cellule.HorizontalAlignment = Element.ALIGN_CENTER
Case ("System.Int64")
cellule.HorizontalAlignment = Element.ALIGN_CENTER
Case ("System.Int16")
cellule.HorizontalAlignment = Element.ALIGN_CENTER
Case ("System.Integer")
cellule.HorizontalAlignment = Element.ALIGN_CENTER
Case ("System.DateTime")
cellule = New PdfPCell(New Phrase(Mid(dgv(j, i).Value.ToString(), 1, 10), tableFont))
cellule.HorizontalAlignment = Element.ALIGN_CENTER
cellule.VerticalAlignment = Element.ALIGN_MIDDLE
Case ("System.String")
cellule.HorizontalAlignment = Element.ALIGN_LEFT
Case Else
cellule.HorizontalAlignment = Element.ALIGN_RIGHT
End Select
table.AddCell(cellule)
Next
Next
doc.Add(table)
doc.Close()
MessageBox.Show("Exportation des informations effectuée avec succès !" & Chr(10) & Chr(10) & "Fichier : " & titre & Chr(10) & Chr(10) & " Emplacement : " & folderPath, "Information système", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
If MsgBox("Souhaitez vous visualiser le document généré ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, "Demande de confirmation...") = MsgBoxResult.Yes Then
Dim monprocess As New Process()
Dim taille_fichier As System.IO.FileInfo
taille_fichier = My.Computer.FileSystem.GetFileInfo("C:\Users\" & Environment.UserName & "\Desktop\" & titre & ".pdf")
If taille_fichier.Length > 0 Then
monprocess.StartInfo.FileName = ("C:\Users\" & Environment.UserName & "\Desktop\" & titre & ".pdf")
monprocess.Start()
'monprocess.WaitForExit()
Else
MsgBox("Problème de lecture du fichier Pdf", MsgBoxStyle.Critical, "Erreur système")
Exit Sub
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub |
Partager