Password protected PDF

A customer asked us if it is possible to create a password protected PDF with powerJobs. The answer is YES, it is!

Here is the PowerShell code for protecting a PDF file with a password:

[System.Reflection.Assembly]::LoadFrom("itextsharp.dll")

function PSUsing {
 param (
 [System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
 [ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
 )
 
 Try {
 &$scriptBlock
 }
 Finally {
 if ($inputObject.psbase -eq $null) {
 $inputObject.Dispose()
 } else {
 $inputObject.psbase.Dispose()
 }
 }
}

$file = New-Object System.IO.FileInfo "C:\Temp\test.pdf"
$fileWithPassword = New-Object System.IO.FileInfo "C:\Temp\test_password.pdf"
$password = "secret"
PSUsing ( $fileStreamIn = $file.OpenRead() ) { 
 PSUsing ( $fileStreamOut = New-Object System.IO.FileStream($fileWithPassword.FullName,[System.IO.FileMode]::Create,[System.IO.FileAccess]::Write,[System.IO.FileShare]::None) ) { 
 PSUsing ( $reader = New-Object iTextSharp.text.pdf.PdfReader $fileStreamIn ) {
 [iTextSharp.text.pdf.PdfEncryptor]::Encrypt($reader, $fileStreamOut, $true, $password, $password, [iTextSharp.text.pdf.PdfWriter]::ALLOW_PRINTING)
 }
 }
}

You can see, we are using iTextSharp for doing this. Please download the library from here and extract the content of itextsharp-dll-core.zip to the folder where you will place your powerShell script later.

The file that you specify for $file variable will be stored with password-protection to the file $fileWithPassword.

Just run the script and open the new file with any PDF viewer and you should get this:

04-12-2014 10-03-34

Bam! That’s all. Have fun!

About weiss92

Software Developer
This entry was posted in powerJobs, PowerShell, Uncategorized, Vault API. Bookmark the permalink.

One Response to Password protected PDF

  1. Jf says:

    Usefull code, thanks

    Could you please tell me how can I pass more than one permission and how to pass the encryption option.

    I have try to many ways and none of then work. For permissions it look like I have to use an ORing string but I could not fin the way in powershell…

    my code (based in yours) Works perfectly, but if I add another permission (see below) the protected file has 0 permisions and I get an error when I I try to change to another encryption type…

    $uPassword = “Trying”
    $oPassword = “”
    $eOption = “ALLOW_PRINTING | ALLOW_EDIT”
    $eLevel = “ENCRYPTION_AES128″
    $orgnFile = “D:\Trial.pdf”
    $destFile = “D:\Trial(protected).pdf”
    $tempFile = “C:\Users\Me\AppData\Local\Temp\encrypted.pdf”

    if (Test-Path $tempFile) {Remove-Item $tempFile}
    if (Test-Path $destFile) {Remove-Item $destFile}

    $file = New-Object System.IO.FileInfo $orgnFile

    $fileProtected = New-Object System.IO.FileInfo $tempFile

    $fileStreamIn = $file.OpenRead()

    $fileStreamOut = New-Object System.IO.FileStream($fileProtected.FullName,[System.IO.FileMode]::Create,[System.IO.FileAccess]::Write,[System.IO.FileShare]::None)

    $reader = New-Object iTextSharp.text.pdf.PdfReader $fileStreamIn

    [iTextSharp.text.pdf.PdfEncryptor]::Encrypt($reader, $fileStreamOut, $true, $uPassword, $oPassword, [iTextSharp.text.pdf.PdfWriter]::$eOption)

    Move-Item -Path $tempFile -Destination $destFile

    Thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s