Files
EnvelopeGenerator/EnvelopeGenerator.BBTests/frmFinalizePDF.vb
TekH c523153654 Add "Full Finalize Test" button for PDF finalization debug
Added a new "Full Finalize Test" button to frmFinalizePDF, along with its event handler. The handler loads annotation data, document bytes, and related entities, logs detailed information about the envelope and signatures, and tests the PDF annotation burning process. The result is saved to the desktop and opened automatically. Also added necessary repository/entity imports for EF Core access. This feature aids in debugging and verifying the full PDF finalization workflow.
2026-03-10 02:16:58 +01:00

211 lines
10 KiB
VB.net

Imports System.IO
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports EnvelopeGenerator.CommonServices.Jobs
Imports EnvelopeGenerator.CommonServices.Jobs.FinalizeDocument
Imports GdPicture14
Imports Newtonsoft.Json.Linq
Imports EnvelopeGenerator.Infrastructure
Imports Microsoft.EntityFrameworkCore
Imports DigitalData.Core.Abstractions
Imports DigitalData.Core.Abstraction.Application.Repository
Imports EnvelopeGenerator.Domain.Entities
Public Class frmFinalizePDF
Private Const CONNECTIONSTRING = "Server=sDD-VMP04-SQL17\DD_DEVELOP01;Database=DD_ECM;User Id=sa;Password=+bk8oAbbQP1AzoHtvZUbd+Mbok2f8Fl4miEx1qssJ5yEaEWoQJ9prg4L14fURpPnqi1WMNs9fE4=;" + "Encrypt=True;TrustServerCertificate=True;"
Private Database As MSSQLServer
Private LogConfig As LogConfig
Private Viewer As GdViewer
Private Manager As AnnotationManager
Private PDFBurner As FinalizeDocument.PDFBurner
Private pGDPictureLicenseKey As String = "kG1Qf9PwmqgR8aDmIW2zI_ebj48RzqAJegRxcystEmkbTGQqfkNBdFOXIb6C_A00Ra8zZkrHdfjqzOPXK7kgkF2YDhvrqKfqh4WDug2vOt0qO31IommzkANSuLjZ4zmraoubyEVd25rE3veQ2h_j7tGIoH_LyIHmy24GaXsxdG0yCzIBMdiLbMMMDwcPY-809KeZ83Grv76OVhFvcbBWyYc251vou1N-kGg5_ZlHDgfWoY85gTLRxafjD3KS_i9ARW4BMiy36y8n7UP2jN8kGRnW_04ubpFtfjJqvtsrP_J9D0x7bqV8xtVtT5JI6dpKsVTiMgDCrIcoFSo5gCC1fw9oUopX4TDCkBQttO4-WHBlOeq9dG5Yb0otonVmJKaQA2tP6sMR-lZDs3ql_WI9t91yPWgpssrJUxSHDd27_LMTH_owJIqkF3NOJd9mYQuAv22oNKFYbH8e41pVKb8cT33Y9CgcQ_sy6YDA5PTuIRi67mjKge_nD9rd0IN213Ir9M_EFWqg9e4haWzIdHXQUo0md70kVhPX4UIH_BKJnxEEnFfoFRNMh77bB0N4jkcBEHPl-ghOERv8dOztf4vCnNpzzWvcLD2cqWIm6THy8XGGq9h4hp8aEreRleSMwv9QQAC7mjLwhQ1rBYkpUHlpTjhTLnMwHknl6HH0Z6zzmsgkRKVyfquv94Pd7QbQfZrRka0ss_48pf9p8hAywEn81Q=="
Private ReadOnly _pdfBurnerParams As New PDFBurnerParams()
Private Sub frmFinalizePDF_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LogConfig = New LogConfig(LogConfig.PathType.CustomPath, Application.StartupPath)
Dim dCnnStr As String = MSSQLServer.DecryptConnectionString(CONNECTIONSTRING)
Database = New MSSQLServer(LogConfig, dCnnStr)
#Disable Warning BC40000 ' Type or member is obsolete
Factory.Shared _
.BehaveOnPostBuild(PostBuildBehavior.Ignore) _
.AddEnvelopeGeneratorInfrastructureServices(
Sub(opt)
opt.AddDbTriggerParams(
Sub(triggers)
triggers("Envelope") = New List(Of String) From {"TBSIG_ENVELOPE_AFT_INS"}
triggers("History") = New List(Of String) From {"TBSIG_ENVELOPE_HISTORY_AFT_INS"}
triggers("EmailOut") = New List(Of String) From {"TBEMLP_EMAIL_OUT_AFT_INS", "TBEMLP_EMAIL_OUT_AFT_UPD"}
triggers("EnvelopeReceiverReadOnly") = New List(Of String) From {"TBSIG_ENVELOPE_RECEIVER_READ_ONLY_UPD"}
triggers("Receiver") = New List(Of String)() ' no tigger
triggers("EmailTemplate") = New List(Of String) From {"TBSIG_EMAIL_TEMPLATE_AFT_UPD"}
End Sub)
opt.AddDbContext(
Sub(options)
options.UseSqlServer(dCnnStr) _
.EnableSensitiveDataLogging() _
.EnableDetailedErrors()
End Sub)
End Sub)
#Enable Warning BC40000 ' Type or member is obsolete
PDFBurner = New PDFBurner(LogConfig, pGDPictureLicenseKey, _pdfBurnerParams)
Viewer = New GdViewer()
Manager = New AnnotationManager()
Dim oLicense = New LicenseManager()
oLicense.RegisterKEY(pGDPictureLicenseKey)
End Sub
Private Function LoadAnnotationDataForReceiver() As String
Dim oSql = $"SELECT VALUE FROM [TBSIG_DOCUMENT_STATUS] WHERE ENVELOPE_ID = {txtEnvelope.Text} AND RECEIVER_ID = {txtReceiver.Text}"
Return Database.GetScalarValue(oSql)
End Function
Private Function LoadAnnotationDataForEnvelope() As DataTable
Dim oSql = $"SELECT VALUE FROM [TBSIG_DOCUMENT_STATUS] WHERE ENVELOPE_ID = {txtEnvelope.Text}"
Return Database.GetDatatable(oSql)
End Function
Private Function LoadEnvelopeDocument() As String
Dim oSql = $"SELECT FILEPATH FROM [TBSIG_ENVELOPE_DOCUMENT] WHERE ENVELOPE_ID = {txtEnvelope.Text}"
Return Database.GetScalarValue(oSql)
End Function
Private Function ReadEnvelope(ByVal pEnvID As Integer) As Byte()
Dim strSql As String = "Select [BYTE_DATA] from [TBSIG_ENVELOPE_DOCUMENT] WHERE ENVELOPE_ID = " & pEnvID
Dim obyteDB = Database.GetScalarValue(strSql)
If Not IsDBNull(obyteDB) Then
Dim fileData As Byte() = DirectCast(Database.GetScalarValue(strSql), Byte())
If fileData IsNot Nothing Then
Return fileData
End If
End If
Throw New InvalidOperationException($"Byte data is null. Envelope ID: {pEnvID}")
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oTable = LoadAnnotationDataForEnvelope()
Dim oJsonList = oTable.Rows.
Cast(Of DataRow).
Select(Function(r As DataRow) r.Item("VALUE").ToString()).
ToList()
Dim envelopeId As Integer = CInt(txtEnvelope.Text)
Dim oBuffer As Byte() = ReadEnvelope(envelopeId)
Dim oNewBuffer = PDFBurner.BurnAnnotsToPDF(oBuffer, oJsonList, envelopeId)
Dim desktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim oNewPath = Path.Combine(desktopPath, $"E{txtEnvelope.Text}R{txtReceiver.Text}.burned.pdf")
File.WriteAllBytes(oNewPath, oNewBuffer)
Process.Start(oNewPath)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim oTable = LoadAnnotationDataForEnvelope()
Dim oJsonList = oTable.Rows.
Cast(Of DataRow).
Select(Function(r As DataRow) r.Item("VALUE").ToString()).
Select(Function(s As String) JObject.Parse(s)).
ToList()
Dim oJObject1 = oJsonList.First()
Dim oJObject2 = oJsonList.ElementAt(1)
oJObject1.Merge(oJObject2)
txtResult.Text = oJObject1.ToString()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
Dim envelopeId As Integer = CInt(txtEnvelope.Text)
Dim log As New System.Text.StringBuilder()
' 1. Load annotation JSON data (same as Service)
Dim oTable = LoadAnnotationDataForEnvelope()
Dim oJsonList = oTable.Rows.
Cast(Of DataRow).
Select(Function(r As DataRow) r.Item("VALUE").ToString()).
ToList()
log.AppendLine($"Annotation JSON count: {oJsonList.Count}")
' 2. Load document bytes (same as Service)
Dim oBuffer As Byte() = ReadEnvelope(envelopeId)
log.AppendLine($"Document bytes: {oBuffer.Length}")
' 3. Check what BurnAnnotsToPDF will do internally
Using scope = Factory.Shared.ScopeFactory.CreateScope()
Dim envRepo = scope.ServiceProvider.Repository(Of Envelope)()
Dim envelope = envRepo.Where(Function(env) env.Id = envelopeId).FirstOrDefault()
If envelope Is Nothing Then
log.AppendLine("ERROR: Envelope not found in EF Core!")
txtResult.Text = log.ToString()
Return
End If
log.AppendLine($"Envelope found: Id={envelope.Id}, EnvelopeTypeId={envelope.EnvelopeTypeId}")
log.AppendLine($"ReadOnly (IsReadAndConfirm): {envelope.ReadOnly}")
If envelope.ReadOnly Then
log.AppendLine(">>> EARLY RETURN: ReadOnly=True, original PDF returned without burning")
txtResult.Text = log.ToString()
Return
End If
Dim sigRepo = scope.ServiceProvider.Repository(Of Signature)()
Dim elements = sigRepo _
.Where(Function(sig) sig.Document.EnvelopeId = envelopeId) _
.Include(Function(sig) sig.Annotations) _
.ToList()
log.AppendLine($"Elements (Signature) count: {elements.Count}")
If elements.Any() Then
log.AppendLine(">>> PATH: BurnElementAnnotsToPDF (new element-based path)")
For Each elem In elements
Dim annotCount = If(elem.Annotations IsNot Nothing, elem.Annotations.Count(), 0)
log.AppendLine($" Element Id={elem.Id}, Page={elem.Page}, X={elem.X}, Y={elem.Y}, W={elem.Width}, H={elem.Height}, Annotations={annotCount}")
If elem.Annotations IsNot Nothing Then
For Each annot In elem.Annotations
log.AppendLine($" Annot: Name={annot.Name}, Type={annot.Type}, X={annot.X}, Y={annot.Y}, W={annot.Width}, H={annot.Height}")
Next
End If
Next
Else
log.AppendLine(">>> PATH: BurnInstantJSONAnnotsToPDF (old JSON-based path)")
End If
End Using
' 4. Actually call BurnAnnotsToPDF (same as Service)
log.AppendLine("")
log.AppendLine("Calling BurnAnnotsToPDF...")
Dim oNewBuffer = PDFBurner.BurnAnnotsToPDF(oBuffer, oJsonList, envelopeId)
log.AppendLine($"Result bytes: {oNewBuffer.Length}")
log.AppendLine($"Same as input: {oBuffer.Length = oNewBuffer.Length AndAlso oBuffer.SequenceEqual(oNewBuffer)}")
' 5. Write output
Dim desktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim oNewPath = Path.Combine(desktopPath, $"E{txtEnvelope.Text}_FullTest.burned.pdf")
File.WriteAllBytes(oNewPath, oNewBuffer)
log.AppendLine($"Output: {oNewPath}")
txtResult.Text = log.ToString()
Process.Start(oNewPath)
Catch ex As Exception
txtResult.Text = $"ERROR: {ex.Message}{vbCrLf}{vbCrLf}{ex.ToString()}"
End Try
End Sub
End Class