Add signature image on PDF without digitally signing it using iTextSharp
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.
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();
}
}