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