Oggi vi voglio parlare di come si crea un file PDF con la libreria iTextSharp e proteggerla da Password .
Dim FileName As String = "c:\demo.pdf" 'Nome del file da creare
Dim PasswordUser As String = "mypass" 'Password User
Dim PasswordOwner As String = "1234" 'Password Admin File
'Funzione che crea un file PDF e lo protegge da Password
Crea_PDF_FileEncryptPassword(FileName, PasswordUser, PasswordOwner)
'Alternativa funzione Crea File PDF con Password
'Crea_PDF_FileEncryptPassword(FileName, PasswordUser)
'Crea un nuovo file PDF con la password
Private Sub Crea_PDF_FileEncryptPassword(ByVal PercorsoFile As String, ByVal PasswordUser As String, Optional ByVal PasswordOwner As String = Nothing)
Dim pdfWrite As PdfWriter
Dim pdfDoc As New Document()
Dim pdfMemoryStream As New IO.FileStream(PercorsoFile, IO.FileMode.Create)
pdfWrite = PdfWriter.GetInstance(pdfDoc, pdfMemoryStream)
'Crea il file con la password
pdfWrite.SetEncryption(PdfWriter.STRENGTH128BITS, PasswordUser, PasswordOwner, PdfWriter.AllowCopy)
pdfDoc.Open()
pdfDoc.Add(New Paragraph("Hello World"))
pdfDoc.NewPage()
pdfDoc.Add(New Paragraph("Hello World Again"))
pdfDoc.Close()
End Sub