将 PDFsharp Unprotect 从 C# 转换为 VB.NET

Converting PDFsharp Unprotect from C# to VB.NET

我正在尝试将此代码从 PDFsharp 示例转换为 VB.NET,我使用了一些转换器,但它一直卡在同一个地方。

这里是转换后的代码:

' 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)

它一直卡在这里:document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify, New PdfPasswordProvider(PasswordProvider))

出现此错误。委托 'PdfSharp.PDF.IO.PdfPasswordProvider' 需要 'AddressOf' 表达式或 lambda 表达式作为其构造函数的唯一参数

我试过使用 AddressOf PdfPasswordProvider,但它无法正常工作。我只是不够先进,无法弄清楚。

编辑 - 代码刚刚取自 http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=39&Itemid=50

密码提供程序必须是一个方法(委托)- PDFsharp 在需要密码时将调用的方法。

该网站只显示了一个代码片段。

您可以在 CodePlex 上找到完整的源代码:
http://pdfsharp.codeplex.com/releases/view/618773

下载源码包,您将获得包括密码提供程序方法在内的完整源码。