הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Sunday, August 2, 2020 1:45 AM
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
All replies (3)
Wednesday, August 5, 2020 8:46 AM ✅Answered
Hi JRDumont,
Thank you for posting here.
Here's an example of adding Watermark to PDF using PDFSHarp.
You can refer to the following code:
Const watermark As String = "your content"
Const emSize As Integer = 150
Const filename As String = "your pdf path"
Dim font As XFont = New XFont("Times New Roman", emSize, XFontStyle.BoldItalic)
Dim document As PdfDocument = PdfReader.Open(filename)
If document.Version < 14 Then document.Version = 14
For idx As Integer = 0 To document.Pages.Count - 1
Dim page As PdfPage = document.Pages(idx)
Dim gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend)
Dim size = gfx.MeasureString(watermark, font)
Dim width = Convert.ToDouble(page.Width)
Dim height = Convert.ToDouble(page.Height)
gfx.TranslateTransform(width / 2, height / 2)
gfx.RotateTransform(-Math.Atan(height / width) * 180 / Math.PI)
gfx.TranslateTransform(-width / 2, -height / 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((width - size.Width) / 2, (height - size.Height) / 2), format)
Next
document.Save(filename)
If you need other variations of watermarks, here's the reference you can refer to:
Hope it can help you.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Monday, August 3, 2020 2:59 AM
What's the problem? Alternatively, you can install free spire.pdf nuget package and try the following solution:
Dim pdf As New PdfDocument()
pdf.LoadFromFile("test.pdf")
Dim page As PdfPageBase = pdf.Pages(0)
Dim brush As New PdfTilingBrush(New SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3))
brush.Graphics.SetTransparency(0.3F)
brush.Graphics.Save()
brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2)
brush.Graphics.RotateTransform(-45)
Dim font As New PdfTrueTypeFont(New Font("Arial Unicode MS", 20F), True)
brush.Graphics.DrawString("Draft", font, PdfBrushes.Red, 0, 0, New PdfStringFormat(PdfTextAlignment.Center))
brush.Graphics.Restore()
brush.Graphics.SetTransparency(1)
page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.ClientSize))
pdf.SaveToFile("TextWaterMark.pdf")
Hope it helps.
Friday, August 7, 2020 12:03 AM
Thank you for your input.
I forgot to add the document.Save at the end of the function.