We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Find text and overlaying it with PdfSignatureField produces shifted result

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.

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(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)

        '* overlay code
        Dim LoadedDocument As New PdfLoadedDocument("c:\convert\Output.pdf")
        If IsNothing(LoadedDocument.Form) Then
        End If
        Dim intIndex As Integer = 0

        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)


                    '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)

        End If


2 Replies

HM Hugo Minoodt July 11, 2019 09:51 AM UTC

I don't know how I fixed it, but suddenly it is working now :)

SL Sowmiya Loganathan Syncfusion Team July 12, 2019 06:44 AM UTC

Hi Hugo, 

We glad to know that your issue is resolved. 

Sowmiya L 

Live Chat Icon For mobile
Up arrow icon