File: n_cst_amyuni_pdf.sru
Size: 32086
Date: Fri, 01 Feb 2019 22:15:40 +0100
$PBExportHeader$n_cst_amyuni_pdf.sru
$PBExportComments$Amyuni PDF Converter
forward
global type n_cst_amyuni_pdf from nonvisualobject
end type
end forward

global type n_cst_amyuni_pdf from nonvisualobject autoinstantiate
end type

type prototypes
// Windows Functions

Function boolean OpenPrinter ( &
   string pPrinterName, &
   Ref ulong phPrinter, &
   long pDefault &
   ) Library "winspool.drv" Alias For "OpenPrinterW"

Function boolean ClosePrinter ( &
   long hPrinter &
   ) Library "winspool.drv"

Function boolean CryptAcquireContext ( &
   Ref ulong phProv, &
   ulong pszContainer, &
   string pszProvider, &
   ulong dwProvType, &
   ulong dwFlags &
   ) Library "advapi32.dll" Alias For "CryptAcquireContextW"

Function boolean CryptReleaseContext ( &
   ulong hProv, &
   ulong dwFlags &
   ) Library "advapi32.dll"

Function boolean CryptGenRandom ( &
   ulong hProv, &
   ulong dwLen, &
   Ref ulong pbBuffer &
   ) Library "advapi32.dll"

// Amyuni Functions

Function long DriverInit ( &
   string szPrinter &
   ) Library "cdintf600.dll" Alias For "DriverInit;Ansi"

Subroutine DriverEnd ( &
   long hPrinter &
   ) Library "cdintf600.dll"

Subroutine GetLastErrorMsg ( &
   Ref string Msg, &
   long MaxMsg &
   ) Library "cdintf600.dll" Alias For "GetLastErrorMsg;Ansi"

Function long SetDocFileProps ( &
   long hPrinter, &
   string szDocTitle, &
   long nOptions, &
   string szFileDir, &
   string szFileName &
   ) Library "cdintf600.dll" Alias For "SetDocFilePropsW"

Function long SetFileNameOptions ( &
   long hPrinter, &
   long nOptions &
   ) Library "cdintf600.dll"

Function long SetResolution ( &
   long hPrinter, &
   long nResolution &
   ) Library "cdintf600.dll"

Function long SetLicenseKey ( &
   string szCompany, &
   string szLicKey &
   ) Library "cdintf600.dll" Alias For "SetLicenseKeyW"

Function long DocOpen ( &
   ref long pedhDocument, &
   string szFileName, &
   string szPassword &
   ) Library "cdintf600.dll" Alias For "DocOpenW"

Function long DocSplit ( &
   long edhDocument, &
   string prefix &
   ) Library "cdintf600.dll" Alias For "DocSplitW"

Function long DocClose ( &
   long edhDocument &
   ) Library "cdintf600.dll"

Function long DocSave ( &
   long edhDocument, &
   string szFileName &
   ) Library "cdintf600.dll" Alias For "DocSaveW"

Function long DocSetTitle ( &
   long edhDocument, &
   string szValue &
   ) Library "cdintf600.dll" Alias For "DocSetTitleW"

Function long DocSetSubject ( &
   long edhDocument, &
   string szValue &
   ) Library "cdintf600.dll" Alias For "DocSetSubjectW"

Function long DocSetAuthor ( &
   long edhDocument, &
   string szValue &
   ) Library "cdintf600.dll" Alias For "DocSetAuthorW"

Function long DocSetKeywords ( &
   long edhDocument, &
   string szValue &
   ) Library "cdintf600.dll" Alias For "DocSetKeywordsW"

Function long Lock ( &
   long hPrinter, &
   string szDocTitle &
   ) Library "cdintf600.dll" Alias For "LockW"

Function long Unlock ( &
   long hPrinter, &
   string szDocTitle, &
   long dwTimeout &
   ) Library "cdintf600.dll" Alias For "UnlockW"

Function long SetWatermark ( &
   long hPrinter, &
   string szWatermark, &
   string szFont, &
   integer fontSize, &
   integer Orientation, &
   ulong color, &
   long xPos, &
   long yPos, &
   boolean bForeground &
   ) Library "cdintf600.dll" Alias For "SetWatermark;Ansi"

Function long ConcatenateFiles ( &
   string szFile1, &
   string szFile2, &
   string szFile3 &
   ) Library "cdintf600.dll" Alias For "ConcatenateFilesW"

end prototypes

type variables
Private:

// Options
Constant Long optNoPrompt       = 1
Constant Long optUseFileName    = 2
Constant Long optConcatenate    = 4
Constant Long optEmbedFonts     = 16
Constant Long optPrintWatermark = 64

Long il_hPrinter = 0
Long il_hDocument = 0
String is_PrinterName     = "Topwiz PDF Printer 6.0"
String is_LicensedCompany = "Amyuni PDF Converter Evaluation"
String is_ActivationCode  = "07EFCDAB0100010025AFF1809EB9441306C5739F7D452154DA83BB9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA"

// Watermark settings
String is_Watermark = "Watermark"
String is_FontName = "Arial"
Integer ii_FontSize = 72
Integer ii_Orientation = 450
Long il_Color = RGB(128, 128, 128)
Long il_xPos = 100
Long il_yPos = -100
Boolean ib_Foreground = False

Public:

String ErrorMessage
String ErrorFunction

Long Resolution = 600
Boolean Concatenate
Boolean EmbedFonts
Boolean Watermark

end variables

forward prototypes
private function string of_getlasterror ()
public function boolean of_installed ()
public subroutine of_driverend ()
public function boolean of_driverinit ()
public function boolean of_docsplit (readonly string as_filename)
public function boolean of_docopen (readonly string as_filename, readonly string as_password)
public function boolean of_docclose ()
public function boolean of_docopen (readonly string as_filename)
public function unsignedlong of_randomnbr ()
public function boolean of_docsettitle (readonly string as_value)
public function boolean of_docsave (readonly string as_filename)
public function boolean of_docsetsubject (readonly string as_value)
public function boolean of_docsetauthor (readonly string as_value)
public function boolean of_docsetkeywords (readonly string as_value)
public function boolean of_setdocfileprops (readonly string as_filename, readonly string as_doctitle)
public function boolean of_lock (readonly string as_doctitle)
public function boolean of_unlock (readonly string as_doctitle)
public function boolean of_savetopdf (readonly datastore ads_print, readonly string as_filename)
public function boolean of_savetopdf (readonly datawindow adw_print, readonly string as_filename)
public subroutine of_setwatermark (string as_watermark, string as_fontname, integer ai_fontsize, integer ai_orientation, long al_color, long al_xpos, long al_ypos, boolean ab_foreground)
public function boolean of_concatenatefiles (string as_file1, string as_file2, string as_file3)
private function boolean of_print (readonly datastore ads_print, readonly string as_doctitle)
private function boolean of_print (readonly datawindow adw_print, readonly string as_doctitle)
private function string of_getlasterror ()
public function boolean of_installed ()
public subroutine of_driverend ()
public function boolean of_driverinit ()
public function boolean of_docsplit (readonly string as_filename)
public function boolean of_docopen (readonly string as_filename, readonly string as_password)
public function boolean of_docclose ()
public function boolean of_docopen (readonly string as_filename)
public function unsignedlong of_randomnbr ()
public function boolean of_docsettitle (readonly string as_value)
public function boolean of_docsave (readonly string as_filename)
public function boolean of_docsetsubject (readonly string as_value)
public function boolean of_docsetauthor (readonly string as_value)
public function boolean of_docsetkeywords (readonly string as_value)
public function boolean of_setdocfileprops (readonly string as_filename, readonly string as_doctitle)
public function boolean of_lock (readonly string as_doctitle)
public function boolean of_unlock (readonly string as_doctitle)
public function boolean of_savetopdf (readonly datastore ads_print, readonly string as_filename)
public function boolean of_savetopdf (readonly datawindow adw_print, readonly string as_filename)
public subroutine of_setwatermark (string as_watermark, string as_fontname, integer ai_fontsize, integer ai_orientation, long al_color, long al_xpos, long al_ypos, boolean ab_foreground)
public function boolean of_concatenatefiles (string as_file1, string as_file2, string as_file3)
private function boolean of_print (readonly datastore ads_print, readonly string as_doctitle)
private function boolean of_print (readonly datawindow adw_print, readonly string as_doctitle)
end prototypes

private function string of_getlasterror ();// -----------------------------------------------------------------------------
// SCRIPT:     of_GetLastError
//
// PURPOSE:    This function returns the last error message.
//
// RETURN:     The error message
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

String ls_ErrorMsg

ls_ErrorMsg = Space(500)

GetLastErrorMsg(ls_ErrorMsg, Len(ls_ErrorMsg))

Return ls_ErrorMsg

end function

public function boolean of_installed ();// -----------------------------------------------------------------------------
// SCRIPT:     of_Installed
//
// PURPOSE:    This function determines if the printer is installed.
//
// RETURN:     True = Installed, False = Not Installed
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

ULong lul_hPrinter

If OpenPrinter(is_PrinterName, lul_hPrinter, 0) Then
   ClosePrinter(lul_hPrinter)
   Return True
End If

Return False

end function

public subroutine of_driverend ();// -----------------------------------------------------------------------------
// SCRIPT:     of_DriverEnd
//
// PURPOSE:    This function performs driver cleanup.
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

SetFileNameOptions(il_hPrinter, 0)

DriverEnd(il_hPrinter)

end subroutine

public function boolean of_driverinit ();// -----------------------------------------------------------------------------
// SCRIPT:     of_DriverInit
//
// PURPOSE:    This function performs driver setup.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ErrorMessage  = ""
ErrorFunction = ""

// initialize the printer driver
il_hPrinter = DriverInit(is_PrinterName)
If il_hPrinter = 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DriverInit"
   Return False
End If

// set the resolution level
ll_Return = SetResolution(il_hPrinter, Resolution) 
If ll_Return = 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "SetResolution"
   Return False
End If

Return True

end function

public function boolean of_docsplit (readonly string as_filename);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocSplit
//
// PURPOSE:    This function splits a document into separate files by page.
//
// ARGUMENTS:  as_filename - The base file name for the page files.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ll_Return = DocSplit(il_hDocument, as_filename)
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocSplit"
   Return False
End If

Return True

end function

public function boolean of_docopen (readonly string as_filename, readonly string as_password);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocOpen
//
// PURPOSE:    This function opens a document.
//
// ARGUMENTS:  as_filename - The name of the PDF file to be opened.
//             as_password - The password of the file (optional).
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ErrorMessage  = ""
ErrorFunction = ""

// set the license key to be used for document functions
ll_Return = SetLicenseKey(is_LicensedCompany, is_ActivationCode)
If ll_Return = 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "SetLicenseKey"
   Return False
End If

// open the document
ll_Return = DocOpen(il_hDocument, as_filename, "")
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocOpen"
   Return False
End If

Return True

end function

public function boolean of_docclose ();// -----------------------------------------------------------------------------
// SCRIPT:     of_DocClose
//
// PURPOSE:    This function closes a document.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

// close the document
ll_Return = DocClose(il_hDocument)
il_hDocument = 0
If ll_Return = 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocClose"
   Return False
End If

Return True

end function

public function boolean of_docopen (readonly string as_filename);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocOpen
//
// PURPOSE:    This function opens a document.
//
// ARGUMENTS:  as_filename - The name of the PDF file to be opened.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Return of_DocOpen(as_filename, "")

end function

public function unsignedlong of_randomnbr ();// -----------------------------------------------------------------------------
// SCRIPT:     of_RandomNbr
//
// PURPOSE:    This function returns a random number.
//
// RETURN:     ULong random number
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Constant String MS_ENH_RSA_AES_PROV    = "Microsoft Enhanced RSA and AES Cryptographic Provider"
Constant String MS_ENH_RSA_AES_PROV_XP = "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
Constant ULong PROV_RSA_AES         = 24
Constant ULong CRYPT_VERIFYCONTEXT  = 4026531840
Constant ULong CRYPT_NEWKEYSET      = 8

String CryptoProvider = MS_ENH_RSA_AES_PROV
ULong ProviderType    = PROV_RSA_AES
ULong lul_Context, lul_Random

// acquire a handle to a key container
If Not CryptAcquireContext(lul_Context, 0, &
         CryptoProvider, ProviderType, &
         CRYPT_VERIFYCONTEXT + CRYPT_NEWKEYSET) Then
   SetNull(lul_Random)
   Return lul_Random
End If

// generate random number
If Not CryptGenRandom(lul_Context, 4, lul_Random) Then
   CryptReleaseContext(lul_Context, 0)
   SetNull(lul_Random)
   Return lul_Random
End If

// close context handle
CryptReleaseContext(lul_Context, 0)

Return lul_Random

end function

public function boolean of_docsettitle (readonly string as_value);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocSetTitle
//
// PURPOSE:    This function sets the Title property in the open document.
//
// ARGUMENTS:  as_value - The value of the property.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ll_Return = DocSetTitle(il_hDocument, as_value)
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocSetTitle"
   Return False
End If

Return True

end function

public function boolean of_docsave (readonly string as_filename);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocSave
//
// PURPOSE:    This function saves an open document.
//
// ARGUMENTS:  as_filename - The name of the PDF file to be saved.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

// save the document
ll_Return = DocSave(il_hDocument, as_filename)
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocSave"
   Return False
End If

Return True

end function

public function boolean of_docsetsubject (readonly string as_value);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocSetSubject
//
// PURPOSE:    This function sets the Subject property in the open document.
//
// ARGUMENTS:  as_value - The value of the property.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ll_Return = DocSetSubject(il_hDocument, as_value)
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocSetSubject"
   Return False
End If

Return True

end function

public function boolean of_docsetauthor (readonly string as_value);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocSetAuthor
//
// PURPOSE:    This function sets the Author property in the open document.
//
// ARGUMENTS:  as_value - The value of the property.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ll_Return = DocSetAuthor(il_hDocument, as_value)
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocSetAuthor"
   Return False
End If

Return True

end function

public function boolean of_docsetkeywords (readonly string as_value);// -----------------------------------------------------------------------------
// SCRIPT:     of_DocSetKeywords
//
// PURPOSE:    This function sets the Keywords property in the open document.
//
// ARGUMENTS:  as_value - The value of the property.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ll_Return = DocSetKeywords(il_hDocument, as_value)
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "DocSetKeywords"
   Return False
End If

Return True

end function

public function boolean of_setdocfileprops (readonly string as_filename, readonly string as_doctitle);// -----------------------------------------------------------------------------
// SCRIPT:     of_SetDocFileProps
//
// PURPOSE:    This function sets various file name properties.
//
// ARGUMENTS:  as_filename - The name of the PDF file to be created.
//             as_doctitle - The document title
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return, ll_Options
String ls_filedir

// determine file name options
ll_Options = optNoPrompt + optUseFileName
If Concatenate Then
   ll_Options = ll_Options + optConcatenate
End If
If EmbedFonts Then
   ll_Options = ll_Options + optEmbedFonts
End If

// set watermark properties
If Watermark Then
   ll_Options = ll_Options + optPrintWatermark
   ll_Return = SetWaterMark(il_hPrinter, is_Watermark, &
                  is_FontName, ii_FontSize, ii_Orientation, &
                  il_Color, il_xPos, il_yPos, ib_Foreground)
   If ll_Return <> 1 Then
      ErrorMessage  = of_GetLastError()
      ErrorFunction = "SetWaterMark"
      Return False
   End If
End If

// set document file properties
ll_Return = SetDocFileProps(il_hPrinter, as_doctitle, ll_Options, ls_filedir, as_filename)
If ll_Return <> 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "SetDocFileProps"
   Return False
End If

Return True

end function

public function boolean of_lock (readonly string as_doctitle);// -----------------------------------------------------------------------------
// SCRIPT:     of_Lock
//
// PURPOSE:    This function locks the printer driver to avoid more than one
//             print at the same time in a multi-user environment.
//
// ARGUMENTS:  as_doctitle - The document title
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return, ll_retrycount

do while true
   ll_Return = Lock(il_hPrinter, as_doctitle)
   If ll_Return = 0 Then
      Return True
   Else
      ll_retrycount ++
      If ll_retrycount = 5 Then
         Exit
      Else
         Sleep(1)
      End If
   End If
loop

Return False

end function

public function boolean of_unlock (readonly string as_doctitle);// -----------------------------------------------------------------------------
// SCRIPT:     of_Lock
//
// PURPOSE:    This function unlocks the printer driver.
//
// ARGUMENTS:  as_doctitle - The document title
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ll_Return = Unlock(il_hPrinter, as_doctitle, 1000)
If ll_Return = 0 Then
   Return True
End If

Return False

end function

public function boolean of_savetopdf (readonly datastore ads_print, readonly string as_filename);// -----------------------------------------------------------------------------
// SCRIPT:     of_SaveToPDF
//
// PURPOSE:    This function saves a DataStore to a PDF file.
//
// ARGUMENTS:  ads_print   - The DataStore object being printed.
//             as_filename - The name of the PDF file to be created.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Boolean lb_Return
String ls_DocTitle

// generate a unique document title
ls_DocTitle = "PDF Print #" + String(of_RandomNbr())

// initialize the driver
If Not of_DriverInit() Then
   Return False
End If

// lock the printer
If Not of_Lock(ls_DocTitle) Then
   // release the driver
   of_DriverEnd()
   ErrorMessage  = "Failed to lock the printer driver!"
   ErrorFunction = "Lock"
   Return False
End If

// set file name options
If Not of_SetDocFileProps(as_filename, ls_DocTitle) Then
   // unlock the printer
   of_Unlock(ls_DocTitle)
   // release the driver
   of_DriverEnd()
   Return False
End If

// print the DataStore
lb_Return = of_Print(ads_print, ls_DocTitle)

// unlock the printer
of_Unlock(ls_DocTitle)

// release the driver
of_DriverEnd()

Return lb_Return

end function

public function boolean of_savetopdf (readonly datawindow adw_print, readonly string as_filename);// -----------------------------------------------------------------------------
// SCRIPT:     of_SaveToPDF
//
// PURPOSE:    This function saves a DataWindow to a PDF file.
//
// ARGUMENTS:  ads_print   - The DataWindow object being printed.
//             as_filename - The name of the PDF file to be created.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Boolean lb_Return
String ls_DocTitle

// generate a unique document title
ls_DocTitle = "PDF Print #" + String(of_RandomNbr())

// initialize the driver
If Not of_DriverInit() Then
   Return False
End If

// lock the printer
If Not of_Lock(ls_DocTitle) Then
   // release the driver
   of_DriverEnd()
   ErrorMessage  = "Failed to lock the printer driver!"
   ErrorFunction = "Lock"
   Return False
End If

// set file name options
If Not of_SetDocFileProps(as_filename, ls_DocTitle) Then
   // unlock the printer
   of_Unlock(ls_DocTitle)
   // release the driver
   of_DriverEnd()
   Return False
End If

// print the DataWindow
lb_Return = of_Print(adw_print, ls_DocTitle)

// unlock the printer
of_Unlock(ls_DocTitle)

// release the driver
of_DriverEnd()

Return lb_Return

end function

public subroutine of_setwatermark (string as_watermark, string as_fontname, integer ai_fontsize, integer ai_orientation, long al_color, long al_xpos, long al_ypos, boolean ab_foreground);// -----------------------------------------------------------------------------
// SCRIPT:     of_SetWatermark
//
// PURPOSE:    This function sets various watermark properties.
//
// ARGUMENTS:  as_Watermark   - Text to print on each page.
//             as_FontName    - Font name used to print text.
//             ai_FontSize    - Font size in 0.1 inch units.
//             ai_Orientation - Text orientation in 0.1 degree units.
//             al_Color       - RGB value for watermark color.
//             al_xPos        - Horizontal position in 0.1 inch units. This is
//                              a positive value from the left of the page.
//             al_yPos        - Vertical position in 0.1 inch units. This is
//                              a negative value from the top of the page.
//             ab_Foreground  - Indicates whether the text is in front of or
//                              behind the information being printed.
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

is_Watermark   = as_Watermark
is_FontName    = as_FontName
ii_FontSize    = ai_FontSize
ii_Orientation = ai_Orientation
il_Color       = al_Color
il_xPos        = al_xPos
il_yPos        = al_yPos
ib_Foreground  = ab_Foreground

Watermark = True

end subroutine

public function boolean of_concatenatefiles (string as_file1, string as_file2, string as_file3);// -----------------------------------------------------------------------------
// SCRIPT:     of_ConcatenateFiles
//
// PURPOSE:    This function concatenates two files into a third file.
//
// ARGUMENTS:  as_file1 - Full path of the first file.
//             as_file2 - Full path of the second file to be appended to file1.
//             as_file3 - Full path of the destination or output file.
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 08/22/2018  RolandS     Initial coding
// -----------------------------------------------------------------------------

Long ll_Return

ErrorMessage  = ""
ErrorFunction = ""

// set the license key to be used for document functions
ll_Return = SetLicenseKey(is_LicensedCompany, is_ActivationCode)
If ll_Return = 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "SetLicenseKey"
   Return False
End If

// concatenate two files into a third
ll_Return = ConcatenateFiles(as_file1, as_file2, as_file3)
If ll_Return = 0 Then
   ErrorMessage  = of_GetLastError()
   ErrorFunction = "ConcatenateFiles"
   Return False
End If

Return True

end function

private function boolean of_print (readonly datastore ads_print, readonly string as_doctitle);// -----------------------------------------------------------------------------
// SCRIPT:     of_Print
//
// PURPOSE:    This function prints a DataStore to a PDF file.
//
// ARGUMENTS:  ads_print   - The DataStore object being printed.
//             as_doctitle - The document title
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Integer li_Return
String ls_OldPrinter

// assign document name
ads_print.Modify('DataWindow.Print.DocumentName="'+ as_doctitle + '"')

// assign Amyuni as the printer
ls_OldPrinter = ads_print.Describe("DataWindow.Printer")
ads_print.Modify('DataWindow.Printer="'+ is_PrinterName + '"')

// print the DataStore
li_Return = ads_print.Print(False)

// restore the assigned printer
ads_print.Modify('DataWindow.Printer="'+ ls_OldPrinter + '"')

If li_Return = 1 Then
   Return True
End If

ErrorMessage  = "Print Failed"
ErrorFunction = "Print"

Return False

end function

private function boolean of_print (readonly datawindow adw_print, readonly string as_doctitle);// -----------------------------------------------------------------------------
// SCRIPT:     of_Print
//
// PURPOSE:    This function prints a DataWindow to a PDF file.
//
// ARGUMENTS:  ads_print   - The DataWindow object being printed.
//             as_doctitle - The document title
//
// RETURN:     True = Success, False = Error
//
// DATE        PROG/ID     DESCRIPTION OF CHANGE / REASON
// ----------  --------    -----------------------------------------------------
// 11/1/2017   RolandS     Initial coding
// -----------------------------------------------------------------------------

Integer li_Return
String ls_OldPrinter

// assign document name
adw_print.Modify('DataWindow.Print.DocumentName="'+ as_doctitle + '"')

// assign Amyuni as the printer
ls_OldPrinter = adw_print.Describe("DataWindow.Printer")
adw_print.Modify('DataWindow.Printer="'+ is_PrinterName + '"')

// print the DataStore
li_Return = adw_print.Print(False)

// restore the assigned printer
adw_print.Modify('DataWindow.Printer="'+ ls_OldPrinter + '"')

If li_Return = 1 Then
   Return True
End If

ErrorMessage  = "Print Failed"
ErrorFunction = "Print"

Return False

end function

on n_cst_amyuni_pdf.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_cst_amyuni_pdf.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on