refactor(PDFBurner): replace fieldName with egName and simplify form field handling

- Removed formFieldIndex tracking and inline dictionary
- Introduced EGName class with centralized index mapping
- Updated AddFormFieldValue to resolve Y-offset using EGName.Index
- Replaced fieldName property with egName for annotations
- Changed helper methods from Function(Void) to Sub for clarity
This commit is contained in:
tekh 2025-10-10 15:00:04 +02:00
parent ce7ca39c39
commit 79c26eb5b5

View File

@ -69,36 +69,17 @@ Namespace Jobs.FinalizeDocument
End Using
End Function
Public Shared ReadOnly FormFieldIndex As ImmutableDictionary(Of String, Integer) =
New Dictionary(Of String, Integer) From {
{Nothing, 0},
{"signature", 0},
{"date", 1},
{"date_label", 2},
{"city", 3},
{"city_label", 4},
{"position", 5},
{"position_label", 6}
}.ToImmutableDictionary()
Private Sub AddInstantJSONAnnotationToPDF(pInstantJSON As String)
Dim oAnnotationData = JsonConvert.DeserializeObject(Of AnnotationData)(pInstantJSON)
oAnnotationData.annotations.Reverse()
Dim yPosOfSigAnnot = oAnnotationData.annotations.ElementAt(2).bbox.ElementAt(1) - 71.84002685546875 + 7
Dim isSeal = True 'First element is signature seal
Dim formFieldIndex = 0
For Each oAnnotation In oAnnotationData.annotations
Logger.Debug("Adding AnnotationID: " + oAnnotation.id)
Select Case oAnnotation.type
Case ANNOTATION_TYPE_IMAGE
If (isSeal) Then
oAnnotation.bbox.Item(1) = yPosOfSigAnnot
End If
AddImageAnnotation(oAnnotation, oAnnotationData.attachments)
Exit Select
@ -110,19 +91,15 @@ Namespace Jobs.FinalizeDocument
'Add form field values
Dim formFieldValue = oAnnotationData.formFieldValues.FirstOrDefault(Function(fv) fv.name = oAnnotation.id)
If formFieldValue IsNot Nothing AndAlso Not _pdfBurnerParams.IgnoredLabels.Contains(formFieldValue.value) Then
AddFormFieldValue(oAnnotation, formFieldValue, formFieldIndex)
formFieldIndex += 1
AddFormFieldValue(oAnnotation, formFieldValue)
End If
Exit Select
End Select
isSeal = False
Next
End Sub
Private Function AddImageAnnotation(pAnnotation As Annotation, pAttachments As Dictionary(Of String, Attachment)) As Void
Private Sub AddImageAnnotation(pAnnotation As Annotation, pAttachments As Dictionary(Of String, Attachment))
Dim oAttachment = pAttachments.Where(Function(a) a.Key = pAnnotation.imageAttachmentId).
SingleOrDefault()
@ -136,9 +113,9 @@ Namespace Jobs.FinalizeDocument
Manager.SelectPage(pAnnotation.pageIndex + 1)
Manager.AddEmbeddedImageAnnotFromBase64(oAttachment.Value.binary, oX, oY, oWidth, oHeight)
End Function
End Sub
Private Function AddInkAnnotation(pAnnotation As Annotation) As Void
Private Sub AddInkAnnotation(pAnnotation As Annotation)
Dim oSegments = pAnnotation.lines.points
Dim oColor = ColorTranslator.FromHtml(pAnnotation.strokeColor)
Manager.SelectPage(pAnnotation.pageIndex + 1)
@ -150,14 +127,16 @@ Namespace Jobs.FinalizeDocument
Manager.AddFreeHandAnnot(oColor, oPoints)
Next
End Function
End Sub
Private Sub AddFormFieldValue(pAnnotation As Annotation, formFieldValue As FormFieldValue)
Dim ffIndex As Integer = EGName.Index(pAnnotation.egName)
Private Function AddFormFieldValue(pAnnotation As Annotation, formFieldValue As FormFieldValue, index As Integer) As Void
' Convert pixels to Inches
Dim oBounds = pAnnotation.bbox.Select(AddressOf ToInches).ToList()
Dim oX = oBounds.Item(0)
Dim oY = oBounds.Item(1) + _pdfBurnerParams.YOffset * index + _pdfBurnerParams.TopMargin
Dim oY = oBounds.Item(1) + _pdfBurnerParams.YOffset * ffIndex + _pdfBurnerParams.TopMargin
Dim oWidth = oBounds.Item(2)
Dim oHeight = oBounds.Item(3)
@ -170,7 +149,7 @@ Namespace Jobs.FinalizeDocument
ant.FontSize = _pdfBurnerParams.FontSize
ant.FontStyle = _pdfBurnerParams.FontStyle
Manager.SaveAnnotationsToPage()
End Function
End Sub
Private Function ToPointF(pPoints As List(Of Single)) As PointF
Dim oPoints = pPoints.Select(AddressOf ToInches).ToList()
@ -220,17 +199,17 @@ Namespace Jobs.FinalizeDocument
Public index As Integer = Nothing
Public fieldName As String = Nothing
Public egName As String = PDFBurner.EGName.NoName
Public hasStructuredID As Boolean = False
Public ReadOnly Property isLabel As Boolean
Get
If String.IsNullOrEmpty(fieldName) Then
If String.IsNullOrEmpty(egName) Then
Return False
End If
Dim parts As String() = fieldName.Split("_"c)
Dim parts As String() = egName.Split("_"c)
Return parts.Length > 1 AndAlso parts(1) = "label"
End Get
End Property
@ -265,7 +244,7 @@ Namespace Jobs.FinalizeDocument
Throw New BurnAnnotationException($"The index of annotation is not integer. Id: {_id}")
End If
fieldName = parts(3)
egName = parts(3)
hasStructuredID = True
End Set
@ -286,6 +265,21 @@ Namespace Jobs.FinalizeDocument
Public Property strokeColor As String
End Class
Public Class EGName
Public Shared ReadOnly NoName As String = Guid.NewGuid().ToString()
Public Shared ReadOnly Seal As String = "signature"
Public Shared ReadOnly Index As ImmutableDictionary(Of String, Integer) =
New Dictionary(Of String, Integer) From {
{NoName, 0},
{Seal, 0},
{"position", 1},
{"city", 2},
{"date", 3}
}.ToImmutableDictionary()
End Class
Friend Class Lines
Public Property points As List(Of List(Of List(Of Single)))
End Class