Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 71.612
  • Views Posts: 542.490
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

VB.NET - MODIFICARE PASSWORD DI UN PDF CON LA LIBRERIA iTextSharp (Codice)

VB.NET - MODIFICARE PASSWORD DI UN PDF CON LA LIBRERIA iTextSharp (Codice)


 

Oggi vi voglio mettere a disposizione una funzione vi permette di modificare un file PDF e cambiare la Password del file. 
Premetto che voi dovete conoscere la Password Vecchia per poterla modificare e mettere quella nuova.

Per prima cosa dovete scaricar la libreria iTextSharp.dll dal sito ufficiale http://sourceforge.net/projects/itextsharp/ e aggiungerla nel progetto. (Progetto->Aggiungi riferimento e poi selezionate Sfoglia e il file iTextSharp.dll)

Ricordatevi di importare la libreria nel progetto :

       Imports iTextSharp.text
       Imports iTextSharp.text.pdf

Esempio di come richiamare la funzione:

Dim OpenFileName As String = "c:\demo.pdf"
   Dim OutNewFileName As String = "c:\demo_new.pdf"
Dim PasswordVecchia As String = "mypass"
Dim PasswordNuova As String = "1234"

        'Crea un nuovo file dove viene cambiata la password 
        ChangePasswordPDF(OpenFileName ,OutNewFileName  , PasswordVecchia , PasswordNuova )

 

CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET

 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

By ImaginSystems & Queen Gin   
Categoria: VB.NET
lunedì, 24 dic 2012 Ore. 16.44

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003