PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 7:45 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Aug 01, 2020 1:36 pm 
Offline

Joined: Thu Jul 02, 2020 3:16 pm
Posts: 12
I could really use some help with my Watermark code.
I used the Watermark sample to get me started but I'm trying to write it in VB.Net.

I'm having issues with the variables, page, gfx, Xrush.

I'll attach a graphics image so the intellisence issues will dispaly

Please see code example below.

For idx As Integer = 0 To PDFDocument.Pages.Count

page = Document.Pages(idx)

'Get an XGraphics object for drawing beneath the existing content.
Dim gfx = XGraphics.FromPdfPage(idx, XGraphicsPdfPageOptions.Prepend)

'// Get the size (in points) of the text.
Dim Size As Integer = gfx.MeasureString(Watermark, PDFFont)

'// Define a rotation transformation at the center of the page.
gfx.TranslateTransform(page.Width / 2, page.Height / 2)
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI)
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2)

'// Create a string format.
Dim Format As New XStringFormat()
Format.Alignment = XStringAlignment.Near
Format.LineAlignment = XLineAlignment.Near

'// Create a dimmed red brush.
XBrush Brush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))

'// Draw the string.
gfx.DrawString(Watermark, PDFFont, Brush, New XPoint((page.Width - Size.Width) / 2, (page.Height - Size.Height) / 2), Format)
Next


Attachments:
2020-08-01_9-31-51.png
2020-08-01_9-31-51.png [ 75.05 KiB | Viewed 3281 times ]
Top
 Profile  
Reply with quote  
PostPosted: Sat Aug 01, 2020 3:35 pm 
Offline

Joined: Thu Jul 02, 2020 3:16 pm
Posts: 12
I've edited my code and I could use help with the variable, "page"

This is what I see now.


Attachments:
2020-08-01_11-31-27.png
2020-08-01_11-31-27.png [ 55.67 KiB | Viewed 3279 times ]
Top
 Profile  
Reply with quote  
PostPosted: Sat Aug 01, 2020 10:58 pm 
Offline

Joined: Thu Jul 02, 2020 3:16 pm
Posts: 12
I modified code to place a Warmark at a certain location but it is not working.
Any help would be greatly appreciated.

This is what I have so far:

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

Dim PDFemSize As Integer = 10
Dim PDFFont As New XFont("Times New Roman", PDFemSize, XFontStyle.BoldItalic)
Dim PDFDocument = PdfReader.Open(PDFName)
If (PDFDocument.Version < 14) Then
PDFDocument.Version = 14
End If
'Dim idx As Integer = 0
'For idx As Integer = 0 To PDFDocument.Pages.Count

Dim page As PdfPage = PDFDocument.Pages(0)

'Get an XGraphics object for drawing beneath the existing content.
'Dim xgfx = XGraphics.FromPdfPage(page)
Dim gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append)

'// Get the size (in points) of the text.
'Dim Size As Integer = gfx.MeasureString(Watermark, PDFFont)
'Dim Size1 As Integer = gfx.MeasureString("Project", PDFFont)
''// Define a rotation transformation at the center of the page.
'gfx.TranslateTransform(page.Width / 2, page.Height / 2)
'gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI)
'gfx.TranslateTransform(-page.Width / 2, -page.Height / 2)

'// Create a string format.
Dim Format As New XStringFormat()
Format.Alignment = XStringAlignment.Near
Format.LineAlignment = XLineAlignment.Near

'// Create a dimmed red brush.
Dim Brush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))

'// Draw the string.
gfx.DrawString(Watermark, PDFFont, Brush, New XPoint(100, 100), Format)
'Next
End Sub


Top
 Profile  
Reply with quote  
PostPosted: Mon Aug 03, 2020 8:38 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
JRDumont wrote:
I modified code to place a Warmark at a certain location but it is not working.
What does your code do? What is it supposed to do?

Update: Saw on StackOverflow what's wrong with the code: The changes were never saved to the file.

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 04, 2020 1:42 am 
Offline

Joined: Thu Jul 02, 2020 3:16 pm
Posts: 12
Thank you for your reply.
Yes, I forgot to place the final line to dave the file once I placed the watermark in the pdf file.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 140 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group