I am trying to digitally sign every pages of any given pdf. But it only signs in first or last page. I think I have figure out that the issues is in MakeSignature.SignDetached() method. This method closes all the stream and closes the pdf for further signing.
My code:
public static void SignInForEveryPage(string input, string output, PDFEncryption pdfEnc, bool encrypt, bool passCheck, string pass) {
X509CertificateParser cp = new X509CertificateParser();
X509Certificate[] chain = { cp.ReadCertificate(CertInfo.MyCert.RawData) };
IExternalSignature externalSignature = new X509Certificate2Signature(CertInfo.MyCert, "SHA-1");
//Setup signature
if(File.Exists(output)) {
File.Delete(output);
}
PdfSignatureAppearance signatureAppearance=null;
PdfSignatureAppearance tempAppearance = null;
PdfReader reader = new PdfReader(input);
FileStream firstFileStream = new FileStream(output, FileMode.Create, FileAccess.ReadWrite);
PdfStamper pdfStamper = PdfStamper.CreateSignature(reader, firstFileStream, '\0', null, true);
for(int index = 1; index <= reader.NumberOfPages; index++) {
if(encrypt && pdfEnc != null) {
pdfEnc.Encrypt(pdfStamper);
}
if(passCheck) {
pdfStamper.SetEncryption(PdfWriter.STRENGTH128BITS, "123", "123", PdfWriter.ALLOW_COPY);
//Set password of output file
}
//Write the metadata
pdfStamper.MoreInfo = MetaData.GetMetaData();
pdfStamper.XmpMetadata = MetaData.GetStreamedMetaData();
//Set signature appearance
signatureAppearance = pdfStamper.SignatureAppearance;
signatureAppearance.Reason = ReasonText; //Reason
signatureAppearance.Contact = ContactText; //Contact
signatureAppearance.Location = LocationText; //Location
byte[] rawData = null;
var customText = "";
//Set the text shown in signature
customText += "Digitally Signed by:\n";
customText += CertInfo.CertName + "\n";
if(!string.IsNullOrEmpty(LocationText)) {
customText += "Location: ";
customText += LocationText + "\n";
}
if(!string.IsNullOrEmpty(ReasonText)) {
customText += "Reason: ";
customText += ReasonText + "\n";
}
customText += "Date: ";
customText += DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss K") + "\n";
customText = customText.TrimEnd();
//set the image shown in signature
if(ShowImage && SignaturePictureImage != null) {
using(MemoryStream memoryStream = new MemoryStream()) {
SignaturePictureImage.Save(memoryStream, ImageFormat.Bmp);
rawData = memoryStream.ToArray();
}
}
//For signature position and size
var sigX = Mm2Pt(LeftNumValue);
var sigY = Mm2Pt(BottomNumValue);
var sigW = Mm2Pt(WidthNumValue);
var sigH = Mm2Pt(HeightNumValue);
//Draw the rectangle for signature field
//pdfStamper.Reader.GetPageSize(index);
signatureAppearance.SignatureGraphic = rawData == null ? null : iTextSharp.text.Image.GetInstance(rawData);
signatureAppearance.Layer2Text = customText;
signatureAppearance.Layer4Text = ""; //if null or not set then it will show 'signature not valid'
signatureAppearance.Acro6Layers = true;
if(signatureAppearance.SignatureGraphic != null) {
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
//show image first then text in the signature
}
signatureAppearance.SetVisibleSignature(new Rectangle(sigX, sigY, sigX + sigW, sigY + sigH), index, null);
signatureAppearance.GetLayer(1);
tempAppearance = signatureAppearance;
MakeSignature.SignDetached(tempAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
}
}
I am using iTextSharp library. Is there any way of fixing this code?