VBScript Requires Chilkat v11.7.0+
VBScript
Password-Protect a PDF with AES-256 Encryption
See more PDF Protection Examples
Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' This example requires the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
' The PDF used in this example is available at two_page_memo.pdf
set pdf = CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
' The passwords are taken from these two properties at the time Protection is called.
' The user password opens the PDF. The owner password opens it too, and is the one needed
' later to change or remove the protection.
pdf.UserPassword = "user-secret"
pdf.OwnerPassword = "owner-secret"
' An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
set json = CreateObject("Chilkat.JsonObject")
set jsonOut = CreateObject("Chilkat.JsonObject")
success = pdf.Protection("encrypt",json,jsonOut)
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
' Nothing has been encrypted yet. "pending" describes the security the PDF will be saved with.
outFile.WriteLine("algorithm: " & jsonOut.StringOf("pending.algorithm"))
outFile.WriteLine("PDF version: " & jsonOut.StringOf("pending.pdfVersion"))
outFile.WriteLine("password needed to open: " & jsonOut.BoolOf("pending.userPasswordRequired"))
' Writing the file applies it.
success = pdf.WriteFile("qa_output/memo_encrypted.pdf")
If (success = 0) Then
outFile.WriteLine(pdf.LastErrorText)
WScript.Quit
End If
' PreLoadCheck reports a file's encryption without loading it, and needs no password.
set checker = CreateObject("Chilkat.Pdf")
set info = CreateObject("Chilkat.JsonObject")
success = checker.PreLoadCheck("qa_output/memo_encrypted.pdf",info)
If (success = 0) Then
outFile.WriteLine(checker.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("encrypted: " & info.BoolOf("encrypted"))
outFile.WriteLine("openPasswordRequired: " & info.BoolOf("openPasswordRequired"))
' With the password, the PDF opens normally.
set reader = CreateObject("Chilkat.Pdf")
reader.UserPassword = "user-secret"
success = reader.LoadFile("qa_output/memo_encrypted.pdf")
If (success = 0) Then
outFile.WriteLine(reader.LastErrorText)
WScript.Quit
End If
outFile.WriteLine("pages: " & reader.NumPages)
' Sample output:
' algorithm: aes-256
' PDF version: 1.7
' password needed to open: True
' encrypted: True
' openPasswordRequired: True
' pages: 2
outFile.Close