How to create a password protected PDF using pdfSharp and Powershell

The Pdfsharp library allows you to do many different operations on PDF files. As it is a .NET library you can use it within Powershell. In the attached cO.Password_Protect.zip you will find a psm1 file and the PdfSharp-gdi.dll with version 1.5.4000.

The library contains functionality to create a new PDF with password protection.

 

This sample code shows how to use it:

Set-PdfPassword -SourceFile "C:\TEMP\test.pdf" -DestinationFile "C:\TEMP\protected_test.pdf" -Password "SECRET" -PdfSharpPath 'C:\ProgramData\coolOrange\powerJobs\Modules\PdfSharp-gdi.dll' -Force -RemoveSourceFiles

In the module you can further customize the permissions on the PDF file by settings these properties to $true or $false:

  • PermitAccessibilityExtractContent: Permits content extraction for accessibility
  • PermitAnnotations: Permits commenting the document
  • PermitAssembleDocument: Permits to insert, rotate or delete pages and create bookmarks or thumbnail images
  • PermitExtractContent: Permits copying content or extraction
  • PermitFormsFill: Permits filling of form fields
  • PermitFullQualityPrint: Permits to print in high quality, insert, rotate or delete pages and create bookmarks
  • PermitModifyDocument: Permits modifying the document
  • PermitPrint: Permits printing the document, should be used in conjunction with PermitFullQualityPrint

See Also

Downloads