Monday, March 4, 2013

PDF Unock by VB.NET

This is a simple code to unlock pdf file that has been protected with password: 

Imports PdfSharp
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ofd As New OpenFileDialog()
        If ofd.ShowDialog() <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If

        'import document
        Dim maindoc As PdfDocument = PdfReader.Open(ofd.OpenFile(), PdfDocumentOpenMode.Import)
        'Create the Output Document
        Dim OutputDoc As PdfDocument = New PdfDocument()

        'Copy over pages from original document
        For Each page As PdfPage In maindoc.Pages
            OutputDoc.AddPage(page)
        Next

        Dim sfd As New SaveFileDialog()
        If sfd.ShowDialog() <> Windows.Forms.DialogResult.OK Then
            maindoc.Dispose()
            OutputDoc.Dispose()
            Exit Sub
        End If

        OutputDoc.Save(sfd.OpenFile(), True)

        maindoc.Dispose()
        OutputDoc.Dispose()

        Me.Close()
    End Sub
End Class

No comments:

Post a Comment