PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Watermark text justification
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4163
Page 1 of 1

Author:  JRDumont [ Sun Aug 02, 2020 3:18 pm ]
Post subject:  Watermark text justification

I am placing a Watermark on PDF files after they are generated.
I would like the user to have the ability to locate the Watermark on the PDF. For example, Top Left, Top Right, Center etc.

Is it possible to change the justification of the Watermark? Currently, it's defaulting to Top Left.
Based on the location of the Watermark I would like to change the justification of the Watermark accordingly.

Here's my code:

Sub PDFWatermark(ByRef PDFName As String, ByRef PDFProject As String, PDFQty As String)

Dim Watermark As String
Watermark = "Project: " + descAssyProject.Value + " - Project Qty: " + activeCompQty
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 pHeight As Double = page.Height
pHeight = pHeight - (emSize * 1.5)
Dim pWidth As Double = page.Width
pWidth = pWidth / 2

Using gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend)
Dim size = gfx.MeasureString(Watermark, font)
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(50, pHeight), format)
End Using
Next
document.Save(PDFName)
End Sub

Author:  TH-Soft [ Mon Aug 03, 2020 8:37 am ]
Post subject:  Re: Watermark text justification

Hi!
JRDumont wrote:
Is it possible to change the justification of the Watermark?
You can invoke DrawString with a destination rectangle instead of a destination point. Then the various alignments make sense and you just have to change the flags of your "format" variable.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/