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.

4 Responses 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

  2. BK says:

    Any idea why this doesn’t work as a scheduled task?

    • weiss92 says:

      I think there should be no reason that this sample should not work in a scheduled task. Could you please provide some more infos on the issue that you face?

      In addition I asked our support team if we have some more samples on this topic, so they can perhaps help with some additional infos.

  3. Manuel Cassan says:

    We found an alternative solution for creating password protected PDF files using Pdfsharp. You can find the related article here: https://support.coolorange.com/support/solutions/articles/22000247626-how-to-create-a-password-protected-pdf-using-pdfsharp-and-powershell

Leave a comment