使用vb.net在pdf文件中添加密码

时间:2016-10-18 21:04:49

标签: vb.net visual-studio pdf passwords

我只是想问一下如何在现有的PDF文件上添加密码,我只是使用水晶报告创建了一个pdf文件,我还需要为报告添加一些安全功能。非常感谢你提前。

让我们说文件" c:\ Folder1 \ sample.pdf"已经存在。我见过类似下面的代码,但我不知道它是否有效,因为我不知道在我的引用中添加什么来使其工作

                    ' Define input and output files path.
                    Dim intputFilePath As String = Program.RootPath + "\\" + "1.pdf"
                    Dim outputFilePath As String = Program.RootPath + "\\" + "1_with_pw.pdf"
                    ' Set passwords for user and owner.
                    Dim userPassword As String = "you"
                    Dim ownerPassword As String = "me"
                    ' Create password setting.
                    Dim setting As PasswordSetting = New PasswordSetting(userPassword, ownerPassword)

                    ' Add password to plain PDF file and output a new file.
                    Dim errorCode As Integer = PDFDocument.AddPassword(intputFilePath, outputFilePath, setting)
                    If errorCode = 0 Then
                        Console.WriteLine("Success")
                    Else
                        Console.WriteLine("Failed")
                    End If

1 个答案:

答案 0 :(得分:0)

                    Dim WorkingFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
                    Dim InputFile As String = Path.Combine(WorkingFolder, "PSNOs.pdf")
                    Dim OutputFile As String = Path.Combine(WorkingFolder, "PSNOs_enc.pdf")
                    Using input As Stream = New FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read)
                        Using output As Stream = New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
                            Dim reader As New PdfReader(input)
                            PdfEncryptor.Encrypt(reader, output, True, Nothing, "secret", PdfWriter.ALLOW_SCREENREADERS)
                        End Using
                    End Using

单词" PdfReader"有错误信息,但它没有要求导入东西..