Search code examples
itextitextpdf

Appearance defined by PDFAppearance class dont apply to Signature Field


i am working through the Paper: Digital Signatures for PDF documents. I am working with the .Net Dlls of ITEXT and i wrote this Code similar to example 2.6 of that paper:

Private Sub Method()
    'step   1:  Create a Document
    Dim document = New Document()

    'step   2:  Create a PdfWriter
    Dim file = "C:\Export\ITEXT.PDF"
    Dim FileOutputStream As System.IO.FileStream = System.IO.File.Open(file, System.IO.FileMode.Create)
    Dim writer = PdfWriter.GetInstance(document, FileOutputStream)

    'step   3: Open the Document
    document.Open()

    'step   4: Add content
    document.Add(New Paragraph("leeres Signaturfeld"))

    Dim signatureFormField = PdfFormField.CreateSignature(writer)

    signatureFormField.FieldName = "FieldName"
    signatureFormField.SetPage()
    signatureFormField.SetWidget(New Rectangle(150, 750, 250, 800), PdfAnnotation.HIGHLIGHT_NONE)
    signatureFormField.SetFieldFlags(PdfAnnotation.FLAGS_PRINT)

    writer.AddAnnotation(signatureFormField)

    Dim tp = PdfAppearance.CreateAppearance(writer, 72, 48)
    tp.SetColorStroke(BaseColor.GRAY)
    tp.SetColorFill(BaseColor.LIGHT_GRAY)
    tp.Rectangle(0.5F, 0.5F, 71.5F, 20.5F)
    tp.FillStroke()
    tp.SetColorFill(BaseColor.BLUE)
    ColumnText.ShowTextAligned(tp, Element.ALIGN_CENTER, New Phrase("SIGN HERE"), 36, 24, 25)
    signatureFormField.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp)

    'step   5: Close the Document
    document.Close()
End Sub

But the Appearance defined by the PDFAppearance class dont apply to my empty Signature field, i just get a empty field without text. What did i wrong?


Solution

  • I have tried to reproduce your issue; as I feel more at home with C# than VB, I translated it line-by-line, so there should not be any difference in behavior.

    So I was quite surprised when after opening your file in Adobe Acrobat Reader DC, I could clearly see the appearance:

    ITEXT.PDF in Adobe Acrobat Reader DC

    So I opened it in my ancient Adobe Acrobat 9.5 to inspect the file, but what did I see:

    ITEXT.PDF in Adobe Acrobat 9.5

    Oops...

    But probably it has to do with the ribbon above. And indeed, after releasing the mark-fields button I got:

    ITEXT.PDF in Adobe Acrobat 9.5, mark-fields button released

    So I assume your issue may depend on the PDF viewer you use and the state it is in. This actually corresponds to the specification which states that individual annotation handlers may ignore this entry and provide their own appearances.


    One remark, though: I would propose you do

    writer.AddAnnotation(signatureFormField)
    

    after

    signatureFormField.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp)
    

    i.e. add the annotation only when it is completely finished.

    Otherwise the signature field may already have been written to file before you finished creating the appearance