#IgnoreWarnings: 12
Sub Class_Globals
    Private mCallBack As Object            'ignore
    Private mEvent As String            'ignore
    Private mFileName As String        
    Private pdfObj As BANanoObject        'ignore
    Private arrayBuffer As Object
    Private BANAno As BANano            'ignore
    Private PDFLib As BANanoObject
    Private pdfDoc As BANanoObject
    Private pages As List
    Public PageCount As Int
End Sub
'initialize the pdf-lib with the final file name
Public Sub Initialize(Module As Object, event As String, fileName As String)
    mCallBack = Module
    mEvent = event
    mFileName = fileName
    PDFLib.Initialize("PDFLib")
    PageCount = 0
End Sub
'open an existing pdf template
Sub OpenTemplate(tmpFile As String)
    arrayBuffer = BANAno.Await(BANAno.GetFileAsArrayBuffer(tmpFile, Null))
    pdfDoc = BANAno.Await(PDFLib.GetField("PDFDocument").RunMethod("load", arrayBuffer))
    pages = pdfDoc.RunMethod("getPages", Null)
    PageCount = pages.size
End Sub
Sub SetTitle(s As String)
    pdfDoc.RunMethod("setTitle", Array(s))
End Sub
Sub SetAuthor(s As String)
    pdfDoc.RunMethod("setAuthor", Array(s))
End Sub
Sub SetSubject(s As String)
    pdfDoc.RunMethod("setSubject", Array(s))
End Sub
'add a signature of a png format
Sub AddSignaturePng(pg As BANanoObject, signURL As String, x As Int, y As Int, width As Int, height As Int)
    Dim sigBuffer As Object = BANAno.Await(BANAno.GetFileAsArrayBuffer(signURL, Null))
    Dim pngIMage As Object = BANAno.Await(pdfDoc.RunMethod("embedPng", sigBuffer))
    
    Dim opt As Map = CreateMap()
    opt.Put("x", x)
    opt.Put("y", y)
    opt.Put("width", width)
    opt.Put("height", height)
    '
    pg.RunMethod("drawImage", Array(pngIMage, opt))
End Sub
Sub GetPage(pgNum As Int) As BANanoObject
    Dim pg As BANanoObject = pages.Get(pgNum)
    Return pg
End Sub
Sub DrawText(pg As BANanoObject, txt As String, xPos As Int, yPos As Int, fSize As Int)
    Dim opt As Map = CreateMap()
    opt.Put("x", xPos)
    opt.Put("y", yPos)
    opt.Put("size", fSize)
    '
    pg.RunMethod("drawText", Array(txt, opt))
End Sub
Sub AddSignatureJpeg(pg As BANanoObject, signURL As String, x As Int, y As Int, width As Int, height As Int)
    Dim sigBuffer As Object = BANAno.Await(BANAno.GetFileAsArrayBuffer(signURL, Null))
    Dim pngIMage As Object = BANAno.Await(pdfDoc.RunMethod("embedJpg", sigBuffer))
    
    Dim opt As Map = CreateMap()
    opt.Put("x", x)
    opt.Put("y", y)
    opt.Put("width", width)
    opt.Put("height", height)
    '
    pg.RunMethod("drawImage", Array(pngIMage, opt))
End Sub
Sub EmbedBase64Jpeg(pg As BANanoObject, base64Jpeg As String, x As Int, y As Int, width As Int, height As Int)
    Dim pngIMage As Object = BANAno.Await(pdfDoc.RunMethod("embedJpg", base64Jpeg))
    
    Dim opt As Map = CreateMap()
    opt.Put("x", x)
    opt.Put("y", y)
    opt.Put("width", width)
    opt.Put("height", height)
    '
    pg.RunMethod("drawImage", Array(pngIMage, opt))
End Sub
'save the pdf as a FileObject
Sub SaveToFileObject As BANanoObject
    Dim pdfBytes As Object = BANAno.Await(pdfDoc.RunMethod("save", Null))
    Dim fc As List
    fc.Initialize
    fc.Add(pdfBytes)
    Dim blob As BANanoObject
    blob.Initialize2("Blob",Array(fc, CreateMap("type": "application/pdf")))
    ' make a new File object
    Dim f As BANanoObject
    f.Initialize2("File",Array(Array(blob), mFileName, CreateMap("type": blob.getfield("type"))))
    Return f
End Sub
'download the PDF
Sub Download
    Dim pdfBytes As Object = BANAno.Await(pdfDoc.RunMethod("save", Null))
    Dim fc As List
    fc.Initialize
    fc.Add(pdfBytes)
    Dim blob As BANanoObject
    blob.Initialize2("Blob",Array(fc, CreateMap("type": "application/pdf")))
    BANAno.RunJavascriptMethod("saveAs",Array(blob,mFileName))
End Sub