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