【发布时间】:2016-02-04 18:52:33
【问题描述】:
我正在尝试将此代码从 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))
出现此错误.. Delegate 'PdfSharp.PDF.IO.PdfPasswordProvider' 需要一个 'AddressOf' 表达式或 lambda 表达式作为其构造函数的唯一参数
我试过做一个 AddressOf PdfPasswordProvider 但是它不能正常工作。我只是不够先进,无法弄清楚。
编辑 - 代码只是取自 http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=39&Itemid=50
【问题讨论】:
-
你没有展示
PasswordProvider的实现。没有 MCVE 可玩,我不会花时间解决您的问题。 -
我所拥有的只是它来自的页面:pdfsharp.com/PDFsharp/…
-
尝试将 AddressOf 放在括号内。很难用您提供的内容来判断,但我认为错误出在 PasswordProvider 参数中,而不是在“New PdfPasswordProvider”声明中。
-
如果您点击了您找到的页面底部的链接,您会找到带有 PasswordProvider 代码的当前页面:pdfsharp.net/wiki/UnprotectDocument-sample.ashx