PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Help with applying Watermark onto existing pdf file
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4162
Page 1 of 1

Author:  JRDumont [ Sat Aug 01, 2020 1:36 pm ]
Post subject:  Help with applying Watermark onto existing pdf file

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 3292 times ]

Author:  JRDumont [ Sat Aug 01, 2020 3:35 pm ]
Post subject:  Re: Help with applying Watermark onto existing pdf file

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 3290 times ]

Author:  JRDumont [ Sat Aug 01, 2020 10:58 pm ]
Post subject:  Re: Help with applying Watermark onto existing pdf file

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

Author:  TH-Soft [ Mon Aug 03, 2020 8:38 am ]
Post subject:  Re: Help with applying Watermark onto existing pdf file

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.

Author:  JRDumont [ Tue Aug 04, 2020 1:42 am ]
Post subject:  Re: Help with applying Watermark onto existing pdf file

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.

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