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() Public Function BurnElementAnnotsToPDF(pSourceBuffer As Byte(), elements As List(Of Signature)) As Byte()
' Add background ' Add background
Using doc As Pdf(Of MemoryStream, MemoryStream) = Pdf.FromMemory(pSourceBuffer) Using doc As Pdf(Of MemoryStream, MemoryStream) = Pdf.FromMemory(pSourceBuffer)
Return doc.Background(elements).ExportStream().ToArray() pSourceBuffer = doc.Background(elements).ExportStream().ToArray()
End Using
For Each element In elements Dim oResult As GdPictureStatus
Using oSourceStream As New MemoryStream(pSourceBuffer)
Dim x = ToInches(element.Left) ' Open PDF
Dim y = ToInches(element.Top) oResult = Manager.InitFromStream(oSourceStream)
If oResult <> GdPictureStatus.OK Then
For Each annot In element.Annotations Throw New BurnAnnotationException($"Could not open document for burning: [{oResult}]")
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 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 End Function
Public Function BurnInstantJSONAnnotsToPDF(pSourceBuffer As Byte(), pInstantJSONList As List(Of String)) As Byte() Public Function BurnInstantJSONAnnotsToPDF(pSourceBuffer As Byte(), pInstantJSONList As List(Of String)) As Byte()
@ -192,11 +213,10 @@ Namespace Jobs.FinalizeDocument
End Sub 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) 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 { Dim yOffset As New Dictionary(Of String, Integer) From {
{"position", 1}, {"position", 1},
{"date", 2}, {"date", 2},
{"city", 3}, {"city", 3}
} }
Manager.SelectPage(page + 1) Manager.SelectPage(page + 1)