Add signature image to PDF without digital signature using iTextSharp

I use iTextSharp to work with PDF files. I want to add a signature image in the Signature field without digitally signing the document (without participation of the certificate).

Is it possible? I can work with a digital signature, but I also want to just add a signature image to the signature field without using certificates.

UPDATE:

Write now. I have the 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 the following error:

Signature is defined. Must be closed in PdfSignatureAppearance

+4
source share
2 answers

PDF, , -)

, - (, ) , , .

PdfStamper , : CreateSignature(), , 6 iText. , , (1) ( PDF ) (2) ( , ).

(1), , 2.6 2.7 ( . CreateEmptyField #). 2.6 , PdfFormField PdfAppearance. 2.7 , PdfStamper. ( removeField()) PdfFormField .

(2) Image PdfContentByte, PdfStamper, GetOverContent(). . 6 .

. ​​ :

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

: f.page a Rectangle, : f.position.

, , . , CreateSignature() PdfStamper, .

+6

     public void buildPDFMemoSignature(string DocName, BuildableDoc docBuild)
     {   
        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();
                    }  
 }
+3

All Articles