PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Nov 24, 2020 10:04 am

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Aug 02, 2020 3:18 pm 

Joined: Thu Jul 02, 2020 3:16 pm
Posts: 10
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
End Sub

Reply with quote  
PostPosted: Mon Aug 03, 2020 8:37 am 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 592
Location: CCAA
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.

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Bing [Bot] and 20 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group