Digitally Sign a PDF File in PowerShell

#How to run the PowerShell scripts

#SignLib.dll must be placed on a folder and the path must be added on the .ps1 script
#$DllPath = 'd:\SignLib.dll'

#run the script file from command line, as below:
#powershell -executionPolicy bypass -file signpdf.ps1 "d:\test.pdf" "d:\test[signed].pdf"

if ($args.Length -eq 0)
echo "Usage: signpdf.ps1 <unsigned file> <signed file>"
$DllPath = 'd:\SignLib.dll'

#digitally sign the pdf file
$sign = new-object -typeName SignLib.Pdf.PdfSignature("")

$pfxFilePath = "d:\pfxcertificate.pfx"
$pFXFilePassword = "123456"

[SignLib.Certificates.DigitalCertificate]::LoadCertificate($pfxFilePath, $pFXFilePassword)

#Load the certificate from Microsoft Certificate Store (smart card certificates).
#$sign.DigitalSignatureCertificate = [SignLib.Certificates.DigitalCertificate]::LoadCertificate($false, "","","");

$sign.SigningReason = "I approve this document"
$sign.SigningLocation = "Europe branch"
$sign.SignaturePage = 1
$sign.SignaturePosition = [SignLib.Pdf.SignaturePosition]::TopRight

echo "Perform the digital signature..."
[System.IO.File]::WriteAllBytes($args[1], $sign.ApplyDigitalSignature())

See also: