Issue

A watermark should be added to a PDF file.

Solution

To add a watermark to a PDF file the commandlet 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 $localPDFfileLocation -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 $localPDFfileLocation -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

$hidePDF = $false #change this setting to $true or $false for showing/hiding the PDF
$workingDirectory = "C:\Temp\$($file._Name)"
$localPDFfileLocation = "$workingDirectory\$($file._Name)"

#Watermark
$Color = "Red"
$Font = "Arial"
$FontSize = 26
$HorizontalAlignment = "Right" #Left, Center or Right
$VerticalAlignment = "Top" #Top, Middle or Bottom
$Opacity = 50 #Between 0 and 100
$OffsetX = -2
$OffsetY = 2
$Angle = 0
#endregion


Add-Log "Starting job 'Sample.AddWatermarkToPDF' for file '$($file._Name)' ..."

if( @("pdf") -notcontains $file._Extension ) {
    Add-Log "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) 
{
  Add-Log "Following file is read only: '$($file._Name)'."
    Set-ItemProperty $file.LocalPath -name IsReadOnly -value $false
    Add-Log "Set read-only flag to false for $($file._Name)."
}

try
{
  Add-WaterMark -Path $localPDFfileLocation -WaterMark "sampletext" -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
Add-Log "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