I would like to replace the text "{signature}" with a PdfSignatureField.
I am trying to do this with PdfLoadedDocument.FindText which returns me the bounds of {signature} (here referenced by bnd)
1/ PdfLoadeDocument.Pages(0).Graphics.DrawRectangle(PdfBrushes.Green, bnd) overlays the textbox with a green rectangle correctly.
2/ Creating a PdfSignatureField and settings the bounds to bnd and adding it to the fields does NOT overlay the textbox correctly. There is a shift that equals the margins of the PDF document. I suspect that the PdfLoadeDocument.Form.Fields.Add function works with the fields offset by the margins.
3/ In PdfDocuments, there is an option for getting the margins (document.PageSettings.Margins) but this option is missing in PdfLoadedDocuments.
e.g.
If I create a document and add the signature with document.Pages(0).Graphics.DrawString("{signature}", font, PdfBrushes.Black, New Drawing.PointF(100, 200))
for testing purposes (normally I would get a file from an external party), then FindText finds it at (140,241) with margins set to 40,40
Is there a way to overlay the text correctly? Or get the margins from PdfLoadeDocument?
Example code below in VB.NET:
Dim font As PdfFont = New PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Helvetica, 10)
Dim signatureImage As New PdfBitmap("c:\convert\Signature.jpg")
'* For testing only. Create my own dowument
Dim document As New PdfDocument()
document.Pages.Add()
document.Pages(0).Graphics.DrawString("{signature}", font, PdfBrushes.Black, New Drawing.PointF(100, 200))
document.Pages(0).Graphics.DrawString("{signature}", font, PdfBrushes.Black, New Drawing.PointF(100, 400))
'MsgBox(document.PageSettings.Margins.Left & "," & document.PageSettings.Margins.Top)
document.Save("c:\convert\Output.pdf")
document.Close()
'* overlay code
Dim LoadedDocument As New PdfLoadedDocument("c:\convert\Output.pdf")
If IsNothing(LoadedDocument.Form) Then
LoadedDocument.CreateForm()
End If
Dim intIndex As Integer = 0
'LoadedDocument.Pages(0).Size.Width
Dim matchRect As New Generic.Dictionary(Of Integer, Generic.List(Of Drawing.RectangleF))
If LoadedDocument.FindText("{signature}", matchRect) Then
For Each kv As Generic.KeyValuePair(Of Integer, Generic.List(Of Drawing.RectangleF)) In matchRect
'Dim loadedpage As Syncfusion.Pdf.PdfLoadedPage = TryCast(LoadedDocument.Pages(kv.Key), Syncfusion.Pdf.PdfLoadedPage)
Dim loadedpage As PdfLoadedPage = LoadedDocument.Pages(kv.Key)
For Each r As Drawing.RectangleF In kv.Value
intIndex = intIndex + 1
'* this works:
loadedpage.Graphics.DrawRectangle(PdfBrushes.Green, r)
Dim signatureField3 As New PdfSignatureField(loadedpage, "SignatureField" & intIndex)
Dim loadedForm As PdfLoadedForm = LoadedDocument.Form
'* this not, it is shifted:
signatureField3.Bounds = New RectangleF(r.Left, r.Top, signatureImage.Width, signatureImage.Height)
LoadedDocument.Form.Fields.Add(signatureField3)
'Dim loadedField As PdfLoadedStyledField = TryCast(loadedForm.Fields(0), PdfLoadedStyledField)
'Dim bounds As RectangleF = loadedField.Bounds
'Draw rectangle on the detected bounds
'LoadedDocument.Pages(0).Graphics.DrawRectangle(PdfPens.Red, bounds)
Next
Next
End If
LoadedDocument.Save("c:\convert\Output.pdf")
LoadedDocument.Close()