The following example demonstrates the process of applying a watermark to a PDF. Applying watermarks is easily accomplished by setting up and mapping the element on a PDF page, and then using PDF measurements and page origin to specify its location.
The code below first defines the text and font to be used for the watermark, then computes the orientation and displacement of the text to center the watermark at the top of each page in the PDF, using Toolkit's SetHeaderMultilineText method.
NOTE: The input PDF is laid out in standard Cartesian coordinates X and Y, referenced the origin in the bottom left corner of the page. The unit of measurement is a PDF unit (1/72 inch).
The following files are required for the example (included in the download):
input.pdf - The input PDF.
output.pdf - An example of the output PDF with the watermark applied.
Watermark.vbs - The VBScript code to apply the watermark.
Click here to download this example for VBScript(includes required files & sample output):
'Multiple watermark examples needing different formatting
iMsg = Array ("A Very Very Long Text Message for the Watermark", "Draft","Department" & VBCrLf & "Memo", "Personal" & VBCrLf & "and" & VBCrLf & "Confidential")
'Set the following variable to 1, 2, or 3 to select one of the above strings as the watermark.
iExample = 2
'Specify Input/Output PDF for the watermark.
iInput = "input.pdf"
iOutput = "output.pdf"
'Specify Font, Font Size and Text for Watermark
strWatermark = iMsg(iExample)
iFont = "Times|Bold"
iFontSize = 50 'Specified in PDF Units (1/72 of an inch)
'PI = 3.1415926535897932
PI = 4 * atn(1)
'Instantiate the Toolkit Object
Set TK = CreateObject("APToolkit.Object")
'Open Output File
varReturn = TK.OpenOutputFile(iOutput)
If varReturn <> 0 Then Error("OpenOutputFile") End If
'Open Input File
varReturn = TK.OpenInputFile(iInput)
If varReturn <> 0 Then Error("OpenInputFile") End If
'Set the font, font size and the page dimensions to center the watermark
TK.SetFont iFont, iFontSize, -1
TK.GetBoundingBox iInput, 1
'Get dimensions of page and watermark
iPageHeight = TK.BBHeight
iPageWidth = TK.BBWidth
iTextWidth = TK.GetHeaderTextWidth(strWatermark) 'Get watermark length to center it
iTextHeight = iFontSize
'The origin, the bottom left corner of the page, is used to calculate the
'the angle of the watermark (the diagonal of the page)
iHypotenuse = Sqr((iPageHeight^2) + (iPageWidth^2))
A = iPageWidth / iHypotenuse
arcCosA = Atn(-A / Sqr(-A * A + 1)) + 2 * Atn(1)
iAngle = (arcCosA) * 180 / PI
TK.SetHeaderRotation(iAngle)
'Define the displacement from page origin for the rotation
x = 0
y = 0
iAdjCenter = 0
'The following is not required but provides centering adjustments to the watermark.
select case iExample
case 0 'Very Long Single line message
y = y - iTextHeight/2
case 1 'Single line message
y = y - iTextHeight/2
'iAdjCenter = iTextWidth/6
case 2 'Two line message
y = y + iTextHeight
iAdjCenter = -iTextWidth/6
case 3 'Three Line Message
y = y + (2 * iTextHeight)
iAdjCenter = -iTextWidth/4
end select
'Watermark alignment left = 0, center = 1, right = 2
iAlignText = 1
'Copy the input PDF and apply the Watermark to each page.
varReturn = TK.CopyForm(0, 0)
If varReturn <> 1 Then Error("CopyForm") End If
'Close Input/Output Files and release instance of Toolkit
TK.CloseOutputFile
TK.CloseInputFile
Set TK = Nothing
Msgbox "Success!"
' Error Handling
Sub Error(Method)
Msgbox "'" & Method & "' failed with a '" & varReturn & "'", 16, "Return Code"
Set TK = Nothing
Wscript.Quit
End Sub
The information in this article is for the use of activePDF Customers and Evaluation Users Only. No part of this document may be transmitted or reproduced without the express written consent of activePDF.