To add a watermark to a PDF file the Cmdlet Add-WaterMark contained in the attached module can be used.
Module installation:
- Download the attached ZIP file containing the module
- Depending on your security policies you may have to unblock (How to unblock files) the archive after downloading it
- Extract all files and folders from the archive
- Copy the extracted files and folders to the powerJob's module directory. The default path is "C:\ProgramData\coolOrange\powerJobs\Modules"
Examples
Adding a text watermark to a PDF file:
This example adds the word "sampletext" as a watermark to a locally saved PDF file.
try{
Add-WaterMark -Path "C:\Temp\myPDF.pdf" -WaterMark "sampletext"
} catch [System.Exception] {
throw($error[0])
}
Adding the current state of a file as a watermark to the PDF file:
This example adds the lifecycle state of the file (assuming the file takes part in a lifecycle) as a watermark to the PDF file.
try{
if ($file._State -ne $null) {
Add-WaterMark -Path "C:\Temp\myPDF.pdf" -WaterMark "$($file._State)"
}
}catch [System.Exception] {
throw($error[0])
}
The following sample job adds a customized watermark to a PDF file:
The settings where the PDF files should be saved as well as the watermark settings can be found in the "settings" region of the job
#region Settings
#change this setting to $true or $false for showing/hiding the PDF
$hidePDF = $false
$workingDirectory = "C:\Temp\$($file._Name)"
#Watermark
$waterMark = "sample text"
$Color = "Red"
$Font = "Arial"
$FontSize = 26
#Left, Center or Right
$HorizontalAlignment = "Right"
#Top, Middle or Bottom
$VerticalAlignment = "Top"
#Between 0 and 100
$Opacity = 50
$OffsetX = -2
$OffsetY = 2
$Angle = 0
#endregion
Write-Host "Starting job 'Sample.AddWatermarkToPDF' for file '$($file._Name)' ..."
if( @("pdf") -notcontains $file._Extension ) {
Write-Host "Files with extension: '$($file._Extension)' are not supported"
return
}
$downloadedFiles = Save-VaultFile -File $file._FullPath -DownloadDirectory $workingDirectory -ExcludeChildren:$false -ExcludeLibraryContents:$false
$file = $downloadedFiles | select -First 1
if((Get-ChildItem $file.LocalPath).IsReadonly) {
Write-Host "Following file is read only: '$($file._Name)'."
Set-ItemProperty $file.LocalPath -name IsReadOnly -value $false
Write-Host "Set read-only flag to false for $($file._Name)."
}
try{
Add-WaterMark -Path $file.LocalPath -WaterMark $waterMark -Angle $Angle -HorizontalAlignment $HorizontalAlignment -VerticalAlignment $VerticalAlignment -Color $Color -Opacity $Opacity -Font $Font -FontSize $FontSize -OffSetX $OffsetX -OffSetY $OffsetY
} catch [System.Exception]{
throw($error[0])
}
#Clean-Up $workingDirectory
Write-Host "Job 'Sample.AddWatermarkToPDF' completed"
- Define the download location and settings for the watermark
- Check if the current file is a PDF file
- Download the file from Vault and set the 'isReadOnly' property to false if it is set
- Add the watermark to the downloaded PDF file
Remarks
- The -Path parameter for the Add-WaterMark cmdlet has to be an absolute path to the PDF file.
- The attached module uses the 1.50beta version of PDFsharp (Project homepage)
- This was tested with version 1.50beta of PDFsharp (attached)