How to Start Convert PDF Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

PDF Password VB.NET Library
How to create, remove, open password protected PDF file in vb.net Windows Forms, WPF application


Help to Improve the Security of Your PDF Document by Setting Password in VB.NET Program





In this VB.NET tutorial, you learn how to create, update, remove PDF passwords in your VB.NET WinForms, WPF applications.

  • Add, apply a new password to protect PDF document
  • Change, update password
  • Remove password from a protected PDF file
  • Verify PDF password

How to manage PDF file password using VB.NET

  1. Download XDoc.PDF password manager VB.NET library
  2. Install VB library to add, remove PDF password
  3. Step by Step Tutorial










Password is a common way to be used for Document Protection. RasterEdge XDoc.PDF SDK provides some PDF security settings about password to help protect your PDF document in VB.NET project. On this page, we will illustrate how to protect PDF document via password by using simple VB.NET demo code. In general, you can do following manipulations.

  • Open password protected PDF
  • Add password to PDF
  • Change PDF original password
  • Remove password from PDF
  • Set PDF security level





Open a PDF file with password using VB.NET


This Visual Basic .NET coding example will help you open a password protected PDF file.



Dim intputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\Output.pdf"
Dim userPassword As String = "you"

' open an encrypted document
Dim doc As PDFDocument = PDFDocument.Open(intputFilePath, userPassword)

' remove the password
doc.Save(outputFilePath)




Add password to a plain PDF file using VB.NET


Following are VB.NET code examples for adding password to a plain PDF file in Visual Basic programming.



Dim intputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\1_with_pw.pdf"
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 file
Dim errorCode As Integer = PDFDocument.AddPassword(intputFilePath, outputFilePath, setting)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If


Overwrite the original file



Dim intputFilePath As String = "C:\1.pdf"
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 file
Dim errorCode As Integer = PDFDocument.AddPassword(intputFilePath, setting)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If




Add password to a plain file stream using VB.NET


Dim intputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\1_with_pw.pdf"

Dim inputStream As FileStream = File.Open(intputFilePath, FileMode.Open, FileAccess.Read)
Dim outputStream As FileStream = File.Open(outputFilePath, FileMode.Create, FileAccess.ReadWrite)
Dim userPassword As String = "you"
Dim ownerPassword As String = "me"
' create password setting
Dim setting As PasswordSetting = New PasswordSetting(userPassword, ownerPassword)
' add password to a plain file stream
Dim errorCode As Integer = PDFDocument.AddPassword(inputStream, outputStream, setting)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If




Change password for an encrypted file using VB.NET


The following VB.NET source code will update the password on a PDF file, and output to a new PDF file



Dim intputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\1_with_pw.pdf"
Dim userPassword As String = "you"
Dim newUserPassword As String = "fill"
Dim newOwnerPassword As String = "watch"
' create setting for the New password
Dim setting As PasswordSetting = New PasswordSetting(newUserPassword, newOwnerPassword)
' change password for an encrypted file
Dim errorCode As Integer = PDFDocument.ChangePassword(intputFilePath, outputFilePath, userPassword, setting)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If


Overwrite the original file



Dim intputFilePath As String = "C:\1.pdf"
Dim userPassword As String = "you"
Dim newUserPassword As String = "fill"
Dim newOwnerPassword As String = "watch"
' create setting for the New password
Dim setting As PasswordSetting = New PasswordSetting(newUserPassword, newOwnerPassword)
' change password for an encrypted file
Dim errorCode As Integer = PDFDocument.ChangePassword(intputFilePath, userPassword, setting)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If




Change password for an encrypted file stream using VB.NET


Dim intputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\1_with_pw.pdf"

Dim inputStream As FileStream = File.Open(intputFilePath, FileMode.Open, FileAccess.Read)
Dim outputStream As FileStream = File.Open(outputFilePath, FileMode.Create, FileAccess.ReadWrite)
Dim userPassword As String = "you"
Dim newUserPassword As String = "fill"
Dim newOwnerPassword As String = "watch"
' create setting for the New password
Dim setting As PasswordSetting = New PasswordSetting(newUserPassword, newOwnerPassword)
' change password for an encrypted file
Dim errorCode As Integer = PDFDocument.ChangePassword(inputStream, outputStream, userPassword, setting)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If




Remove the password for a protected PDF file in VB.NET code


These three VB.NET source code will help you to delete password for a protected PDF file.

Output to a new file



Dim intputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\Output.pdf"
Dim userPassword As String = "you"

' remove password in the input file And output to a New file
Dim errorCode As Integer = PDFDocument.RemovePassword(intputFilePath, outputFilePath, userPassword)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If


Overwrite the original file



Dim intputFilePath As String = "C:\1.pdf"
Dim userPassword As String = "you"

' remove password in the file
Dim errorCode As Integer = PDFDocument.RemovePassword(intputFilePath, userPassword)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If




Remove the password for an encrypted file stream using VB.NET


Dim intputFilePath As String = "C:\1.pdf"
Dim outputFilePath As String = "C:\Remove.pdf"

Dim inputStream As FileStream = File.Open(intputFilePath, FileMode.Open, FileAccess.Read)
Dim outputStream As FileStream = File.Open(intputFilePath, FileMode.Create, FileAccess.ReadWrite)
Dim userPassword As String = "you"

Dim errorCode As Integer = PDFDocument.RemovePassword(inputStream, outputStream, userPassword)
If errorCode = 0 Then
    Console.WriteLine("Success")
Else
    Console.WriteLine("Failed")
End If




VB.NET: How to verify if a PDF file is password protected or not


Remarks: If a PDF file does not been encrypted, it should never have any password.



Dim intputFilePath As String = "C:\1.pdf"

Dim isEncrypted As Boolean = PDFDocument.IsEncrypted(intputFilePath)
Dim hasUserPassword As Boolean = PDFDocument.HasUserPassword(intputFilePath)

Console.WriteLine("This file is encrypted: {0}", isEncrypted)
Console.WriteLine("Password is not empty: {0}", hasUserPassword)