Add Watermark to a pdf

Hi,

Below is the code i am using to add watermark to a pdf file.

Dim pdf As PdfDocument = New PdfDocument()
pdf.LoadFromFile(“C:\Users\sreej\Documents\AI_RPA Production Authorization Form CCOSTR_Family.pdf”)

’ Initialize font with the appropriate font file path

Dim trueTypeFontPath As String =“C:\Windows\Fonts\arial.ttf”

Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(trueTypeFontPath, 12)

Dim text As String = “CONFIDENTIAL”

Dim textSize As SizeF = font.MeasureString(text)

Dim offset1 As Single = CType((textSize.Width * System.Math.Sqrt(2) / 4), Single)
Dim offset2 As Single = CType((textSize.Height * System.Math.Sqrt(2) / 4), Single)

Dim page As PdfPageBase

For Each page In pdf.Pages
If page.Canvas IsNot Nothing Then
console.WriteLine(“rcbrjcbj”)
Else
Console.WriteLine("Canvas is null for page ")
End If

Dim alpha As Single = 0.8 ' Transparency value


' Set transparency for both pen and brush
Dim penColor As Color = Color.FromArgb(CInt(alpha * 255), Color.Black)
Dim brushColor As Color = Color.FromArgb(CInt(alpha * 255), Color.DarkGray)

' Create pen and brush with transparent colors
Dim pen As New PdfPen(penColor)
Dim brush As New PdfSolidBrush(brushColor)



Dim offsetX As Single =CType(page.Size.Width / 2 - offset1 - offset2, Single)
Dim offsetY As Single = CType(page.Size.Height / 2 + offset1 - offset2, Single)

console.WriteLine("hello")
Console.WriteLine("OffsetX: " & offsetX & ", OffsetY: " & offsetY)
page.Canvas.TranslateTransform(offsetX, offsetY)
console.WriteLine("hello")
page.Canvas.RotateTransform(-45)
page.Canvas.DrawString(text, font, brush, 0, 0)

Next

pdf.SaveToFile(“C:\Users\sreej\Downloads\Output.pdf”)

but the page.canvas is showing null.
Can anybody help me solve this.

Regards
Sreejith S S

For this option, instead going through the script approach , you can try with activities

Leverage Built-in Activities:

  • Utilize the “Add Text Stamp” activity for adding text watermarks to PDFs.
  • Employ the “Image Stamp” activity for adding logo watermarks to PDFs.

Could you please tell these activities comes under which package.

Hi @sreejith.ss

For Add text Stamp you can use below marketplace activity " Itext" it only support on windows compatibility. refer the below thread.

For Image Stamp you can use this activity “UiPathTeam.PDF.Extensions.Activities” it also support on windows compatibility.

Hope it helps!!!