Knowledgebase: Developer
VB example for applying a watermark to a PDF file
Posted by Craig Lebakken on 22 July 2020 12:40 PM
Problem:
 Do you have any Visual Basic examples for applying a PDF watermark to a newly created PDF file?

Solution:
The following Visual Basic example shows how to apply a PDF watermark named "c:\temp\watermark.pdf" to a new PDF document named "c:\temp\test.pdf".

Option Explicit

Private Const REG_DWORD As Long = 4
Private Const HKEY_CURRENT_USER = &H80000001
Private Const KEY_ALL_ACCESS = &H3F

Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long

Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
Long) As Long

Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
ByVal cbData As Long) As Long



Private Sub SaveWin2PDFDword(sValueName As String, lValueSetting As Long)
Dim lRetVal As Long 'result of the SetValueExLong function
Dim hKey As Long 'handle of open key

lRetVal = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings\Dane Prairie Systems\Win2PDF", 0, _
KEY_ALL_ACCESS, hKey)

lRetVal = RegSetValueExLong(hKey, sValueName, 0&, _
REG_DWORD, lValueSetting, 4)

RegCloseKey (hKey)
End Sub



Private Sub Apply_Watermark()
'enable sending the PDF file
'replace the file options with &H800 to apply background instead of watermark

SaveWin2PDFDword "file options", &H200

'specify the existing PDF file that will be appended to the new PDF document
SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFAppendFile", "c:\temp\appendfile.pdf"

'set the file name for the new PDF document
SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFFileName", "c:\temp\test.pdf

'output some text
Printer.Print "hello world!"

'save the new PDF document and apply the watermark
Printer.EndDoc

End Sub
(0 vote(s))
Helpful
Not helpful

Comments (0)

Copyright 2000-2018 © Dane Prairie Systems LLC. All Rights Reserved. About Us | Privacy Policy | PDF Blog | Site Map