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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Bouton qui va récuperer le chemin du fichier ZIP'
Dim res As DialogResult
res = OpenFileDialog1.ShowDialog()
If res = Windows.Forms.DialogResult.OK Then
txtPath.Text = OpenFileDialog1.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Bouton qui va lancer la procédure pour completer les fichier PDF'
Dim GeneralName As String 'GeneralName corespond au numéro de série du dossier'
GeneralName = System.IO.Path.GetFileName(Replace(txtPath.Text, ".zip", ""))
ExtractPath = Replace(txtPath.Text, ".zip", "\ ") 'Chemin d extraction, extrait dans un dossier ou le nom est le numéro de série'
ExtractArchive(txtPath.Text, ExtractPath)
'Nettoyage de 2 fichiers extrait'
System.IO.File.Delete(ExtractPath & "notice.pdf")
System.IO.File.Delete(ExtractPath & System.IO.Path.GetFileName(Replace(txtPath.Text, ".zip", "") & "_description.zip"))
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(ExtractPath)
Dim aryFi As IO.FileInfo() = di.GetFiles("*.pdf")
Dim fi As IO.FileInfo
Dim Entreprise As String
Dim NomFinal As String
'Pour tous les fichiers présent dans le dossier d extraction et qui doivent etre remplie'
For Each fi In aryFi
'Validation du fichier'
If fi.Name <> System.IO.Path.GetFileName(Replace(txtPath.Text, ".zip", "")) & "_resume.pdf" And fi.Name <> System.IO.Path.GetFileName(Replace(txtPath.Text, ".zip", "")) & "_emprise.pdf" Then
'Recuperation du nom de l entreprise'
Entreprise = GetEntreprise(fi.FullName)
'Formatage du nom du futur fichier'
NomFinal = Replace(fi.FullName, fi.Name, "")
NomFinal = GeneralName & "_" & Entreprise & ".pdf"
'Appel de la procédure qui complete le fichier PDF'
FillForm(fi.FullName, NomFinal)
End If
Next
End Sub
Private Function GetEntreprise(ByVal Fichier As String)
Dim Ent As String
Dim pdfTemplate As String = Fichier
Dim pdfReader As New PdfReader(pdfTemplate)
'La fermeture du reader a été ajouté, si cette ligne n est pas présente, la ligne suivante refuse de s executer avec le message d erreur suivant :
"Le processus ne peut pas accéder au fichier, car il est en cours d utilisation par un autre processus."'
pdfReader.Close()
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
Fichier, FileMode.Open))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
Ent = pdfFormFields.GetField("Exploitant")
Try
pdfStamper.FormFlattening = True
pdfStamper.Reader.Close()
'Les deux lignes suivantes refuse de s executer avec le message d erreur :
"Impossible d accéder à un fichier fermé."'
pdfStamper.Dispose()
pdfStamper.close()
Catch ex As Exception
End Try
pdfReader.Close()
Return Ent
End Function
Private Sub FillForm(ByVal PDFPath As String, ByVal NewPDFPath As String)
'Ligne qui plante systématiquement avec le message d erreur :
"Le processus ne peut pas accéder au fichier, car il est en cours d utilisation par un autre processus."'
Dim pdfReader As New PdfReader(PDFPath)
Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
System.IO.Path.GetFileName(NewPDFPath), FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
pdfFormFields.SetField("N° affaire executant", txtNumAff.Text)
[...]
pdfFormFields.SetField("NombrePJ_DICT", txtNbPJ.Text)
pdfStamper.FormFlattening = True
pdfStamper.Close()
pdfReader.Close()
My.Computer.FileSystem.CopyFile(PDFPath, NewPDFPath)
My.Computer.FileSystem.DeleteFile(PDFPath)
End Sub
Public Sub ExtractArchive(ByVal zipFilename As String, ByVal ExtractDir As String)
'Procédure d extraction'
Dim Redo As Integer = 1
Dim MyZipInputStream As ZipInputStream
Dim MyFileStream As FileStream
MyZipInputStream = New ZipInputStream(New FileStream(zipFilename, FileMode.Open, FileAccess.Read))
Dim MyZipEntry As ZipEntry = MyZipInputStream.GetNextEntry
Directory.CreateDirectory(ExtractDir)
While Not MyZipEntry Is Nothing
If (MyZipEntry.IsDirectory) Then
Directory.CreateDirectory(ExtractDir & "\" & MyZipEntry.Name)
Else
If Not Directory.Exists(ExtractDir & "\" & _
Path.GetDirectoryName(MyZipEntry.Name)) Then
Directory.CreateDirectory(ExtractDir & "\" & _
Path.GetDirectoryName(MyZipEntry.Name))
End If
MyFileStream = New FileStream(ExtractDir & "\" & _
MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write)
Dim count As Integer
Dim buffer(4096) As Byte
count = MyZipInputStream.Read(buffer, 0, 4096)
While count > 0
MyFileStream.Write(buffer, 0, count)
count = MyZipInputStream.Read(buffer, 0, 4096)
End While
MyFileStream.Close()
End If
Try
MyZipEntry = MyZipInputStream.GetNextEntry
Catch ex As Exception
MyZipEntry = Nothing
End Try
End While
If Not (MyZipInputStream Is Nothing) Then MyZipInputStream.Close()
If Not (MyFileStream Is Nothing) Then MyFileStream.Close()
End Sub |
Partager