Torna al Thread

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click Dim FileName As String = "c:\prova.pdf" Crea_PDF_File(FileName) Dim TempFileName As String = "c:\temp_demo.pdf" Dim PasswordVecchia As String = "mypass" Dim PasswordNuova As String = "1234" 'Verifica se il file esiste If System.IO.File.Exists(FileName) Then 'Verifica se il file è protetto da password If IsPasswordProtected(FileName) Then 'Verifica prima se la password è corretta If IsPasswordProtectedCorrect(FileName, PasswordVecchia) Then 'Modifica la password al file ChangePasswordPDF(FileName, TempFileName, PasswordVecchia, PasswordNuova) Else 'restituisce il messaggio d'errore che la password è sbagliata MsgBox("Errore la Password inserita non è corretta", vbCritical, "ATTENZIONE!!") End If Else 'Il file non è protetto da password CreatePDFPassword(FileName, TempFileName, PasswordNuova) End If Else MsgBox("File non esistente", vbCritical, "ATTENZIONE!!") End If End Sub ''' <summary> ''' Cambia password al file PDF ''' </summary> ''' <param name="sourcePdfPath">Percorso del file sorgente PDF (ex. c:\demo.pdf)</param> ''' <param name="outputPdfPath">Percorso dove salvare il nuovo file PDF (ex. c:\test.pdf)</param> ''' <param name="PasswordNew">Password Nuova</param> ''' <remarks></remarks> Private Sub CreatePDFPassword(ByVal sourcePdfPath As String, ByVal outputPdfPath As String, ByVal PasswordNew As String) Try Dim pageCount As Integer = 0 Dim currentPage As Integer = 0 Dim pdfDoc As iTextSharp.text.Document = Nothing Dim writer As iTextSharp.text.pdf.PdfCopy = Nothing Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing Dim currentPDF As Integer = 0 Dim pdfReader As New PdfReader(sourcePdfPath) pdfDoc = New iTextSharp.text.Document(pdfReader.GetPageSizeWithRotation(1)) writer = New iTextSharp.text.pdf.PdfCopy(pdfDoc, _ New IO.FileStream(outputPdfPath, _ IO.FileMode.OpenOrCreate, _ IO.FileAccess.Write)) writer.SetEncryption(PdfWriter.STRENGTH128BITS, PasswordNew, PasswordNew, PdfWriter.AllowCopy) pageCount = pdfReader.NumberOfPages pdfDoc.Open() While currentPage < pageCount currentPage += 1 pdfDoc.SetPageSize(pdfReader.GetPageSizeWithRotation(currentPage)) pdfDoc.NewPage() page = writer.GetImportedPage(pdfReader, currentPage) writer.AddPage(page) End While pdfDoc.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub '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 ''' <summary> ''' Cambia password al file PDF ''' </summary> ''' <param name="sourcePdfPath">Percorso del file sorgente PDF (ex. c:\demo.pdf)</param> ''' <param name="outputPdfPath">Percorso dove salvare il nuovo file PDF (ex. c:\test.pdf)</param> ''' <param name="password">Password Attuale</param> ''' <param name="PasswordNew">Password Nuova</param> ''' <remarks></remarks> Private Sub ChangePasswordPDF(ByVal sourcePdfPath As String, ByVal outputPdfPath As String, ByVal password As String, ByVal PasswordNew As String) Try Dim pageCount As Integer = 0 Dim currentPage As Integer = 0 Dim pdfDoc As iTextSharp.text.Document = Nothing Dim writer As iTextSharp.text.pdf.PdfCopy = Nothing Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing Dim currentPDF As Integer = 0 Dim pdfReader As New PdfReader(sourcePdfPath, New System.Text.ASCIIEncoding().GetBytes(password)) pdfDoc = New iTextSharp.text.Document(pdfReader.GetPageSizeWithRotation(1)) writer = New iTextSharp.text.pdf.PdfCopy(pdfDoc, _ New IO.FileStream(outputPdfPath, _ IO.FileMode.OpenOrCreate, _ IO.FileAccess.Write)) writer.SetEncryption(PdfWriter.STRENGTH128BITS, PasswordNew, PasswordNew, PdfWriter.AllowCopy) pageCount = pdfReader.NumberOfPages pdfDoc.Open() While currentPage < pageCount currentPage += 1 pdfDoc.SetPageSize(pdfReader.GetPageSizeWithRotation(currentPage)) pdfDoc.NewPage() page = writer.GetImportedPage(pdfReader, currentPage) writer.AddPage(page) End While pdfDoc.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub 'Verifica se il file è protetto da password Private Function IsPasswordProtected(ByVal pdfFullname As String) As Boolean Try Dim pdfReader As New PdfReader(pdfFullname) Return False Catch generatedExceptionName As BadPasswordException Return True End Try End Function 'Verifica se la password è corretta per aprire il file Private Function IsPasswordProtectedCorrect(ByVal pdfFullname As String, ByVal Passwords As String) As Boolean Try Dim pdfReader As New PdfReader(pdfFullname, New System.Text.ASCIIEncoding().GetBytes(Passwords)) Return True Catch generatedExceptionName As BadPasswordException Return False End Try End Function
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5