The script signPdfFile.ps1 signs a PDF file from Windows PowerShell 5.1 with the .NET Framework build of the library. The certificate is a PFX file or a certificate of the Windows store found by its thumbprint (a smart card or a USB token), without any window.
How to run it
powershell -ExecutionPolicy Bypass -File signPdfFile.ps1 test.pdf signedFile.pdf
powershell -ExecutionPolicy Bypass -File signPdfFile.ps1 test.pdf -Pades -Position BottomLeft -Reason "Approved" -Location "Bucharest"
powershell -ExecutionPolicy Bypass -File signPdfFile.ps1 test.pdf -LastPage -ImageFile signature.jpg
powershell -ExecutionPolicy Bypass -File signPdfFile.ps1 test.pdf -Thumbprint "FAC4D3EF766A59BB068DD90877267EBC56B4232A" -SmartCardPin 1234
Options: -Reason, -Location, -Page, -LastPage, -Position, -Invisible, -ImageFile, -Pades, -HashAlgorithm, -TimestampUrl, -PfxFile, -PfxPassword, -Thumbprint, -LocalMachine, -SmartCardPin. Without -Pades the signature is the classic PKCS#7 signature that every PDF reader recognizes.
The signing code of the script
The functions Initialize-SignLib, Get-SigningCertificate and Set-SmartCardPin are in SignLibCommon.ps1, next to the script.
Initialize-SignLib
if (-not $OutputFile) { $OutputFile = Get-DefaultOutputFile $InputFile }
$sign = New-Object SignLib.Pdf.PdfSignature (Get-SerialNumber $SerialNumber)
$sign.LoadPdfDocument([System.IO.File]::ReadAllBytes((Get-FullPath $InputFile)))
$sign.DigitalSignatureCertificate = Get-SigningCertificate -PfxFile $PfxFile -PfxPassword $PfxPassword -Thumbprint $Thumbprint -LocalMachine:$LocalMachine
$sign.HashAlgorithm = Get-HashAlgorithm $HashAlgorithm
$sign.SigningReason = $Reason
if ($Location) { $sign.SigningLocation = $Location }
if ($Pades) { $sign.SignatureStandard = [SignLib.Pdf.PdfSignatureStandard]::Pades }
# The appearance of the signature.
if ($Invisible)
{
$sign.VisibleSignature = $false
}
else
{
if ($LastPage) { $sign.SignaturePage = [int]::MaxValue } else { $sign.SignaturePage = $Page }
$sign.SignaturePosition = [System.Enum]::Parse([SignLib.Pdf.SignaturePosition], $Position)
if ($ImageFile)
{
$sign.SignatureImage = [System.IO.File]::ReadAllBytes((Get-FullPath $ImageFile))
$sign.SignatureImageType = [SignLib.Pdf.SignatureImageType]::ImageAndText
}
}
if ($TimestampUrl) { $sign.TimeStamping.ServerUrl = [Uri]$TimestampUrl }
Write-Host "Perform the digital signature..."
Set-SmartCardPin $SmartCardPin
[System.IO.File]::WriteAllBytes((Get-FullPath $OutputFile), $sign.ApplyDigitalSignature())
Write-Host "Signed file: $OutputFile"
The minimum PowerShell code to sign a PDF
$dir = "C:\SignLib" # the folder with SignLib.dll and iTextSharp-4.1.6.dll (.NET Framework build)
[Reflection.Assembly]::LoadFrom("$dir\iTextSharp-4.1.6.dll") | Out-Null
[Reflection.Assembly]::LoadFrom("$dir\SignLib.dll") | Out-Null
$sign = New-Object SignLib.Pdf.PdfSignature("your serial number")
$sign.LoadPdfDocument([IO.File]::ReadAllBytes("C:\docs\test.pdf"))
$sign.DigitalSignatureCertificate = [SignLib.Certificates.DigitalCertificate]::LoadCertificate("C:\docs\certificate.pfx", "123456")
$sign.SigningReason = "I approve this document"
[IO.File]::WriteAllBytes("C:\docs\signed.pdf", $sign.ApplyDigitalSignature())
Run the full sample. The script signPdfFile.ps1 is in the folder PowerShell Scripts of the library download, with SignLib.dll, a test certificate and test documents. It runs in Windows PowerShell 5.1. Without a serial number the library runs in demo mode. Download the library and the samples · User manual
Frequently asked questions
Does the script run in PowerShell 7?
No. It uses the .NET Framework build of SignLib.dll and it runs in Windows PowerShell 5.1 (powershell.exe).
How do I give the serial number and the PIN without writing them in the script?
With the environment variables SIGNLIB_SERIAL and SIGNLIB_PIN, which all the scripts read.