0

In a windows directory I have about 100 pdf-files, some are password protected, some are not. Is there an easy way (in the command-line or maybe with a freeware tool) to find the ones which are password protected without opening each of them in a pdf-reader?

Kodiak
  • 19
  • 1
  • 3

2 Answers2

1

Since you are on Windows, you can use the iTextSharp library to accomplish this.

First, extract itextsharp.dll, which is inside the itextsharp-dll-core archive.

Then, use the following PowerShell script:

Add-Type -Path .\itextsharp.dll

Get-ChildItem -Filter *.pdf |
ForEach-Object {
    $filename = $_.Name
    Try {
        $pdf = New-Object iTextSharp.text.pdf.PdfReader($_.FullName)
        If ($pdf.IsEncrypted()) {
            $filename
        }
    }
    Catch {
        $filename
    }
}

The output will be the name of each PDF which is secured or encrypted.

msitt
  • 1,237
  • 12
  • 27
  • The script runs fine when there are no password protected files in the directory and I get the following error if there is one: `New-Object : Exception calling ".ctor" with "1" argument(s): "Bad user password" At line:4 char:22 + $pdf = New-Object <<<< iTextSharp.text.pdf.PdfReader($_.FullName) + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand` – Kodiak Apr 01 '17 at 13:37
  • I updated the script to fix this issue. Let me know if that helps. – msitt Apr 01 '17 at 16:40
0

You could use the script of is it possible to check if pdf is password protected using ghostscript? and extend it to iterate over a bunch of files and eg. move all non-password protected files into a subdirectory.

Community
  • 1
  • 1
JMax
  • 1,134
  • 1
  • 11
  • 20