Question

I am using iTextSharp to work with PDFs. I want to add signature image to Signature field without digitally signing the document (without any involvement of certificate).

Is it possible? I am able to work with digital signing but I also want to just add signature image on signature field without any use of certificates.

UPDATE:

Write now I have following code.

// Set PDF Reader and PDF Stamper
PdfReader reader = new PdfReader(sourceDocument);

// File stream where PDF will write
FileStream fout = new FileStream(destinationPath, FileMode.Create, FileAccess.ReadWrite);
PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0', null, true);

// Set PDF Appearance              
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
iTextSharp.text.Image signatureFieldImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);
appearance.SignatureGraphic = signatureFieldImage;
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC;
appearance.SetVisibleSignature(signatureFieldName);

stamper.Close();
reader.Close();
fout.Close();

But when I try to run it, it gives following error:

Signature defined. Must be closed in PdfSignatureAppearance

Was it helpful?

Solution

You are using code to digitally sign a PDF that you don't want to digitally sign ;-)

If the document is already signed, adding any extra content (such as an image) will break the signature, but if the document wasn't signed yet, you have different options.

You need to use PdfStamper the normal way, that is: not by using the CreateSignature() method, but the way it's described in chapter 6 of my book about iText. You also need to decide whether or not it's important that (1) the image is part of the signature field (in which case it will disappear when the PDF is actually signed) or (2) the image needs to be added as part of the content stream (in which case it will still be there once you sign the document).

In case of (1), please take a look at code sample 2.6 and code sample 2.7 of my book about digital signatures (see the CreateEmptyField example for the C# version of the code). In code sample 2.6, you learn how to create a PdfFormField with a custom PdfAppearance. In code sample 2.7, you learn how to add a signature field to an existing document using PdfStamper. In your case, you'd remove the existing signature field (using the removeField() method) and replace it with a new PdfFormField with a different appearance at the exact same coordinates.

In case of (2), you'll just create an Image object and add it to the PdfContentByte retrieved from the PdfStamper using the GetOverContent() method. See the examples of chapter 6 for inspiration.

In both cases, you need to know the coordinates and the page number in both cases. This information can be retrieved like this:

AcroFields form = stamper.AcroFields;
AcroFields.FieldPosition f = form.GetFieldPositions("mySigName")[0];

You'll get the page like this: f.page and a Rectangle defining the position like this: f.position.

As we don't know exactly which type of end result you require, it's hard to go into more detail. The most important error in your code is that you use the CreateSignature() method to obtain a PdfStamper object whereas you don't want to sign the document.

OTHER TIPS

This is how I add a unsigned signature field

     public void buildPDFMemoSignature()
     {   
        using (var ms = new MemoryStream())
        {
        var doc = new Document(PageSize.A4, 20f, 10f, 30f, 0f);
        {
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            doc.Open();
            try
            {
              // add stuff to your PDF

             // Signature is added here ***************

             PdfFormField field = PdfFormField.CreateSignature(writer);
  field.SetWidget(new iTextSharp.text.Rectangle(190, 730, 440, 650), PdfAnnotation.HIGHLIGHT_NONE);
                    //Rectangle(float llx, float lly, float urx, float ury) 
                    field.FieldName = "mySig";
                    field.Flags = PdfAnnotation.FLAGS_PRINT;
                    field.SetPage();
                    field.MKBorderColor = BaseColor.BLACK;
                    field.MKBackgroundColor = BaseColor.WHITE;
                    PdfAppearance tp = PdfAppearance.CreateAppearance(writer, 72, 48);
                    tp.Rectangle(0.5f, 0.5f, 71.5f, 47.5f);
                    tp.Stroke();
                    field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
                    writer.AddAnnotation(field); 
                    }
                    catch (Exception ex)
                    {  
                      //exceptions                   
                    }
                    finally
                    {
                        doc.Close();
                    }  
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top