fix: ensure annotations are properly burned after background addition in PDFBurner

This commit is contained in:
tekh 2025-10-28 10:43:19 +01:00
parent 75e47d10e3
commit 7041a4694a

View File

@ -51,25 +51,46 @@ Namespace Jobs.FinalizeDocument
Public Function BurnElementAnnotsToPDF(pSourceBuffer As Byte(), elements As List(Of Signature)) As Byte()
' Add background
Using doc As Pdf(Of MemoryStream, MemoryStream) = Pdf.FromMemory(pSourceBuffer)
Return doc.Background(elements).ExportStream().ToArray()
End Using
pSourceBuffer = doc.Background(elements).ExportStream().ToArray()
For Each element In elements
Dim x = ToInches(element.Left)
Dim y = ToInches(element.Top)
For Each annot In element.Annotations
If annot.Type = AnnotationType.FormField Then
AddFormFieldValue(annot.Name, x, y, 100, 180, element.Page, annot.Value)
ElseIf annot.Type = AnnotationType.FormField Then
AddImageAnnotation(x, y, 100, 180, element.Page, annot.Value)
ElseIf annot.Type = AnnotationType.Ink Then
AddInkAnnotation(element.Page, annot.Value)
Dim oResult As GdPictureStatus
Using oSourceStream As New MemoryStream(pSourceBuffer)
' Open PDF
oResult = Manager.InitFromStream(oSourceStream)
If oResult <> GdPictureStatus.OK Then
Throw New BurnAnnotationException($"Could not open document for burning: [{oResult}]")
End If
Next
Next
'Add annotations
For Each element In elements
Dim x = ToInches(element.Left)
Dim y = ToInches(element.Top)
For Each annot In element.Annotations
If annot.Type = AnnotationType.FormField Then
AddFormFieldValue(annot.Name, x, y, 100, 180, element.Page, annot.Value)
ElseIf annot.Type = AnnotationType.FormField Then
AddImageAnnotation(x, y, 100, 180, element.Page, annot.Value)
ElseIf annot.Type = AnnotationType.Ink Then
AddInkAnnotation(element.Page, annot.Value)
End If
Next
Next
'Save PDF
Using oNewStream As New MemoryStream()
oResult = Manager.SaveDocumentToPDF(oNewStream)
If oResult <> GdPictureStatus.OK Then
Throw New BurnAnnotationException($"Could not save document to stream: [{oResult}]")
End If
Manager.Close()
Return oNewStream.ToArray()
End Using
End Using
End Using
End Function
Public Function BurnInstantJSONAnnotsToPDF(pSourceBuffer As Byte(), pInstantJSONList As List(Of String)) As Byte()
@ -192,11 +213,10 @@ Namespace Jobs.FinalizeDocument
End Sub
Private Sub AddFormFieldValue(name As String, x As Double, y As Double, width As Double, height As Double, page As Integer, value As String)
Dim yOffset As New Dictionary(Of String, Integer) From {
{"position", 1},
{"date", 2},
{"city", 3},
{"city", 3}
}
Manager.SelectPage(page + 1)