Has anyone used Watermarks in DynaPDFMBS?
There is a sample 'Demo Text project, but as far as I can tell, that just puts ‘normal’ text at an angle on a page.
PDFs have an annotation property that allows a watermark to be added that can not (easily) be selected or amended by the viewer.
CreateAnnot and AddAnnotToPage appear to be the relevant calls, but as is normal, the DynaPDF manual just says ‘use this call if you want to do it’ , without explanation.
And the DynaPDFMBS documentation just repeats that , with a link to the DynaPDF docs that say the same.
Here is an example of an ‘example’
LBOOL pdfAddAnnotToPage(
const PPDF* IPDF, // Instance pointer
UI32 PageNum,// Page number
UI32 Handle) // Annotation handle
The function adds an annotation to a page. With exception of PopUp annotations all annotation types can be drawn on multiple pages. Watermark or Stamp annotations are typical annotations which can be placed on multiple pages
After CreateAnnotAp (integer)
And apparently EndTemplate()
dim pdf as new MyDynapdfMBS
Dim f As FolderItem = SpecialFolder.Desktop.Child("Create PDF with WatermarkAnnot.pdf")
pdf.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License
// Create a new PDF
call pdf.CreateNewPDF f
// We want to use top-down coordinates
Call pdf.SetPageCoords pdf.kpcTopDown
// Add a page
call pdf.Append
// create watermark annotation
Dim AnnotHandle As Integer = pdf.WatermarkAnnot(100, 100, 300, 200)
// it's empty initially
// so we create an annotation appearance stream
Dim x As Integer = pdf.CreateAnnotAP(AnnotHandle)
// set a color and draw rectangle
Call pdf.SetFillColor(&cFF7777)
Call pdf.Rectangle 0, 0, 300, 200, pdf.kfmFill
Call pdf.SetStrokeColor(&c000000)
Call pdf.SetFillColor(&c000000)
Call pdf.SetFont("Helvetica", pdf.kfsBold, 30)
Call pdf.WriteText(20, 20, "Hello World")
// close template
Call pdf.EndTemplate
// continue with normal streaming
// end page
Call pdf.EndPage
// Add a second page
Call pdf.Append
// let it show on second page, too
Call pdf.AddAnnotToPage(2, AnnotHandle)
// end page
Call pdf.EndPage
// Close page
call pdf.CloseFile
// Open PDF
f.Launch
That’s great, Christian.
(although ironically, the watermark here looks like content, and the DynaPDF looks like a watermark.
I know it’s the other way around)
I’d never have guessed this in a several years.
I’ll take this and experiment now.
Thanks.
Part of my test code is below if it is useful to others - some of the numbers here are hard coded (trial and error ‘magic numbers’) from a test project.
dim AnnotHandle as integer = pdf.WatermarkAnnot(0, 0, pdf.GetPageWidth,pdf.GetPageHeight * 1.4)
dim thewatermark as string = "Some Text Here"
dim fontsize as integer = pdf.getpageheight/12
dim textheight as integer
Dim x As Integer = pdf.CreateAnnotAP(AnnotHandle)
call pdf.SaveGraphicState
dim rot as double = atan(0.3) * -57.2958
call pdf.RotateCoords rot,-120,pdf.getpageheight/2 + 100
Call pdf.SetFont("Helvetica", pdf.kfsBold, fontsize)
call pdf.SetStrokeColor(&h1F62D8) 'orangey red
call pdf.SetTextDrawMode(pdf.kdmStroke) ' hollow letters
textheight = pdf.GetTextHeight (0,thewatermark) * 1.8
call pdf.Setlinewidth(textheight/250)
for z as integer = 0 to pdf.GetPageheight /textheight
Call pdf.WriteText((pdf.GetPageWidth- pdf.GetTextWidth (thewatermark))/2 , 20 + z*textheight , thewatermark)
next
call pdf.RestoreGraphicState
call pdf.SetPageCoords pdf.kpcTopDown
// close template
Call pdf.EndTemplate
call pdf.SetTextDrawMode(pdf.kdmNormal)