Add Watermark to PDF using PDFSHarp

  • Thread starter Thread starter JRDumont
  • Start date Start date
J

JRDumont

Guest
I could use some help trying to add a Watermark to PDF files.

Here is the function I converted from C#.

Sub PDFWatermark(ByRef PDFName As String, ByRef PDFProject As String, PDFQty As String)
Dim Watermark As String
Watermark = "Project: " + PDFProject + " - Project Qty: " + PDFQty
Dim emSize As Integer = 50

Dim font = New XFont("Times New Roman", emSize, XFontStyle.BoldItalic)
Dim document = PdfReader.Open(PDFName)
If document.Version < 14 Then document.Version = 14

For idx = 0 To document.Pages.Count - 1
Dim page = document.Pages(idx)

Dim gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend)
Dim size = gfx.MeasureString(Watermark, font)
gfx.TranslateTransform(1000, 1000)
gfx.RotateTransform(-Math.Atan(2000 / 2000) * 180 / Math.PI)
gfx.TranslateTransform(-2000 / 2, -2000 / 2)
Dim format = New XStringFormat()
format.Alignment = XStringAlignment.Near
format.LineAlignment = XLineAlignment.Near
Dim brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
gfx.DrawString(Watermark, font, brush, New XPoint(500, 500), format)
Next
End Sub

Continue reading...
 
Back
Top