eqiz
eqiz

Reputation: 1591

Converting PDFsharp Unprotect from C# to VB.NET

I'm trying to convert this code from a PDFsharp sample over to VB.NET and I've used a few converters, but it keeps getting stuck on same place.

Here is the converted code:

' Get a fresh copy of the sample PDF file.
' The passwords are 'user' and 'owner' in this sample.
Dim filename As String = "HelloWorld (protected).pdf"
File.Copy(Path.Combine("../../../../PDFs/", filename), Path.Combine(Directory.GetCurrentDirectory(), filename), True)

Dim document As PdfDocument

' Opening a document will fail with an invalid password.
Try
    document = PdfReader.Open(filename, "invalid password")
Catch ex As Exception
    Debug.WriteLine(ex.Message)
End Try

' You can specifiy a delegate, which is called if the document needs a
' password. If you want to modify the document, you must provide the
' owner password.
document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify, New PdfPasswordProvider(PasswordProvider))

' Open the document with the user password.
document = PdfReader.Open(filename, "user", PdfDocumentOpenMode.[ReadOnly])

' Use the property HasOwnerPermissions to decide whether the used password
' was the user or the owner password. In both cases PDFsharp provides full
' access to the PDF document. It is up to the programmer who uses PDFsharp
' to honor the access rights. PDFsharp doesn't try to protect the document
' because this make little sence for an open source library.
Dim hasOwnerAccess As Boolean = document.SecuritySettings.HasOwnerPermissions

' Open the document with the owenr password.
document = PdfReader.Open(filename, "owner")
hasOwnerAccess = document.SecuritySettings.HasOwnerPermissions

' A document opened with the owner password is completely unprotected
' and can be modified.
Dim gfx As XGraphics = XGraphics.FromPdfPage(document.Pages(0))
gfx.DrawString("Some text...", New XFont("Times", 12), XBrushes.Firebrick, 50, 100)

' The modified document is saved without any protection applied.
Dim level As PdfDocumentSecurityLevel = document.SecuritySettings.DocumentSecurityLevel

' If you want to save it protected, you must set the DocumentSecurityLevel
' or apply new passwords.
' In the current implementation the old passwords are not automatically
' reused. See 'ProtectDocument' sample for further information.

' Save the document...
document.Save(filename)
' ...and start a viewer.
Process.Start(filename)

It keeps getting stuck here on: document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify, New PdfPasswordProvider(PasswordProvider))

With this error.. Delegate 'PdfSharp.PDF.IO.PdfPasswordProvider' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor

I've tried doing an AddressOf PdfPasswordProvider however its not working correctly. I'm just not advanced enough to figure it out.

EDIT - Code is just taking from http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=39&Itemid=50

Upvotes: 0

Views: 1177

Answers (1)

The password provider has to be a method (delegate) - a method that will be called by PDFsharp when a password is needed.

The web site only shows a code snippet.

You can find the complete source code on CodePlex:
http://pdfsharp.codeplex.com/releases/view/618773

Download the source package and you will have the complete source including the password provider method.

Upvotes: 1

Related Questions