Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error Resume Next
        OpenFileDialog1.Filter = "PDF Files|*.pdf"
        OpenFileDialog1.Title = "Select a PDF File"
        Dim dummy As DialogResult = OpenFileDialog1.ShowDialog
        If dummy = Windows.Forms.DialogResult.OK Then
            Dim inputpath As String = OpenFileDialog1.FileName
            If System.IO.File.Exists(inputpath) = True Then
                Call SignPDF(inputpath)
            End If
        End If
        Err.Clear()
    End Sub


    Private Sub SignPDF(ByRef InputPath As String)
        Dim pdf As Aloaha_ZUGFeRD.XML
        Dim InputPDF As String = InputPath
        Dim SignedPDF1 As String = InputPath + ".1.pdf"
        Dim SignedPDF2 As String = InputPath + ".2.pdf"
        Dim CertificateSerialNumber As String = ""
        Dim LicenseKey As String = "BPMVKKEL2HJ7Y4T5NA335KBN2I"
        Dim SignatureHashType As Long = 0
        Dim SignReason As String = ""
        Dim SignContact As String = ""
        Dim SignLocation As String = ""
        Dim SignImagePath As String = ""
        Dim SignPositionLeft As Double = 0
        Dim SignPositionTop As Double = 0
        Dim SignPositionWidth As Double = 0
        Dim SignPositionHeight As Double = 0
        Dim SignPage As Long = 0
        Dim InputPDFPassword As String = ""
        Dim DoOverwrite As Boolean = False
        Dim TSAURL As String = ""
        Dim LastError As String = ""
        Dim DoCAdES As Boolean = False
        Dim IncludeCRL As Boolean = False
        Dim CertEmailAddress As String = ""

        Dim imagetext As String = ""
        Dim Files2Delete() As String = {}
        Dim StichOnTopOfEachOther As Boolean = False
        Dim Usedx509 As System.Security.Cryptography.X509Certificates.X509Certificate2 = Nothing

        Dim WriteOnImage As Boolean = False
        imagetext = imagetext + "Hello World" + vbCrLf
        imagetext = imagetext + "Hallo Welt" + vbCrLf
        imagetext = imagetext + "Wie geht es?" + vbCrLf

        pdf = New Aloaha_ZUGFeRD.XML
  
        SignImagePath = "c:\image.jpg"
        'CertificateSerialNumber = "3b a8 f3 9c 00 00 00 00 28 f6"
        LicenseKey = "BPMVKKEL2HJ7Y4T5NA335KBN2I"
        SignReason = "I have seen this document"
        SignLocation = ""
        DoOverwrite = True
        IncludeCRL = True
        DoCAdES = True
        SignPage = 1
        SignPositionWidth = 100
        SignPositionHeight = 50
        SignPositionLeft = 50
        SignPositionTop = 400

        CertificateSerialNumber = ""
        CertificateSerialNumber = "" '"3b a8 f3 9c 00 00 00 00 28 f6"
        Dim pin As String = ""      '"1234"
        SignatureHashType = 2 'for SHA 256

        Dim result1 As Boolean = False
        Dim result2 As Boolean = False

        result1 = pdf.SignPDF(InputPDF, LicenseKey, CertificateSerialNumber, _
                        SignatureHashType, SignReason, "", SignLocation, SignedPDF1, _
                        SignImagePath, SignPositionLeft, SignPositionTop, SignPositionWidth, SignPositionHeight, _
                        SignPage, InputPDFPassword, DoOverwrite, TSAURL, 30000, False, True, True, LastError, _
                        DoCAdES, IncludeCRL, pin, CertEmailAddress, imagetext, WriteOnImage, StichOnTopOfEachOther, Usedx509)

        If result1 = True Then
            MsgBox("Signed pdf is: " + SignedPDF1)
        Else
            Dim output1 As String = "1" + vbCrLf + LastError
            If Err.Number <> 0 Then
                output1 = output1 + vbCrLf + Err.Description
            End If
            MsgBox(output1)
        End If

        result2 = Aloaha_ZUGFeRD.PDF.Sign_PDF(InputPDF, LicenseKey, CertificateSerialNumber, _
                        SignatureHashType, SignReason, "", SignLocation, SignedPDF2, _
                        SignImagePath, SignPositionLeft, SignPositionTop, SignPositionWidth, SignPositionHeight, _
                        SignPage, InputPDFPassword, DoOverwrite, TSAURL, 30000, False, True, True, LastError, _
                        DoCAdES, IncludeCRL, pin, CertEmailAddress, Usedx509)


        If result2 = True Then
            MsgBox("Signed pdf is: " + SignedPDF2)
        Else
            Dim output2 As String = "2" + vbCrLf + LastError
            If Err.Number <> 0 Then
                output2 = output2 + vbCrLf + Err.Description
            End If
            MsgBox(output2)
        End If


        pdf = Nothing
    End Sub
End Class
