我正在使用pdf进行数字签名。我可以在PDF中添加单个签名,但是当我尝试在多个地方添加相同的签名时,我会收到无效签名问题。
我使用下面的代码在多个位置对单个PDF文件进行数字签名,但签名出现在一个地方。
在另一个地方没有显示。(签名无效)。 我在下面使用的是代码
string dttime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");
PdfReader pdfReader = null;
PdfStamper stamper = null;
try
{
pdfReader = new PdfReader(In_File_Name);
stamper = PdfStamper.CreateSignature(pdfReader, new FileStream(New_File_Name, FileMode.Create, FileAccess.Write), '\0', null, true);
//Annotation code for multiple signatures
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Reason = "";// ConfigurationManager.AppSettings["APPEARANCE_REASON"].ToString();
appearance.Location = "";// ConfigurationManager.AppSettings["APPEARANCE_LOCATION"].ToString();
appearance.SignDate = DateTime.Now.AddMinutes(3);
appearance.Image = null;
appearance.Acro6Layers = true;
//appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(540, 290, 430, 320), 1, null);
string Page_Attrb = "1(540, 290, 430, 320);2(540, 290, 430, 320)"; //In_Page_Attrb;
String[] Pageattr = Page_Attrb.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
String[] Pageattr1 = Pageattr[0].Split(new[] { "(", ")" }, StringSplitOptions.RemoveEmptyEntries);
int PageNo = int.Parse(Pageattr1[0]);//1
float lx = 0, ly = 0, hx = 0, hy = 0; string[] Attr = Pageattr1[1].Split(',');
if (Attr.Length > 3)
{
lx = float.Parse(Attr[0]);
ly = float.Parse(Attr[1]);
hx = float.Parse(Attr[2]);
hy = float.Parse(Attr[3]);
}
int csize = 8192;
PdfContentByte canvas = stamper.GetOverContent(1);
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
iTextSharp.text.Font f = new iTextSharp.text.Font(bfTimes, iTextSharp.text.Font.BOLD, 15);
canvas.SetFontAndSize(bfTimes, 6);
canvas.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
for (int page = 0; page <= 1; page++)
{
String[] Pageattr2 = Pageattr[page].Split(new[] { "(", ")" }, StringSplitOptions.RemoveEmptyEntries);
int PageNo1 = int.Parse(Pageattr2[0]);
float lx1 = 0, ly1 = 0, hx1 = 0, hy1 = 0;
string[] Attr1 = Pageattr2[1].Split(',');
if (Attr1.Length > 3)
{
lx1 = float.Parse(Attr1[0]);
ly1 = float.Parse(Attr1[1]);
hx1 = float.Parse(Attr1[2]);
hy1 = float.Parse(Attr1[3]);
}
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(lx1, ly1, hx1, hy1);
PdfAnnotation annotation = PdfAnnotation.CreateFreeText(stamper.Writer, rect, appearance.Layer2Text, canvas);
PdfAppearance tp = PdfAppearance.CreateAppearance(stamper.Writer, 444, 555);
annotation.Flags = PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_LOCKED | PdfAnnotation.FLAGS_PRINT;
PdfDate pdfdate = new PdfDate();
annotation.Title = "dsds";
annotation.Border = new PdfBorderArray(0, 0, 0, new PdfDashPattern());
annotation.Put(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
stamper.AddAnnotation(annotation, PageNo1);
}
Dictionary<PdfName, int> exclusionSizes = new Dictionary<PdfName, int>();
exclusionSizes[PdfName.CONTENTS] = csize * 2 + 2;
appearance.CryptoDictionary = (PdfDictionary)new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED)
{
Reason = appearance.Reason,
Location = appearance.Location,
Contact = appearance.Contact,
Date = new PdfDate(appearance.SignDate)
};
appearance.PreClose(exclusionSizes);
Stream s = appearance.GetRangeStream();
//byte[] by = GetStreamAsByteArray(s);
//string hashdocument = GenerateFilehash256Hex(by);
string hashdocument = genaratePDFHash(s);
string eSignReq = Esigndoc(In_OTP, In_Aadhaar_No, hashdocument);
string res = postXMLData(ConfigurationManager.AppSettings["eSignURL"].ToString(), eSignReq);
//Fetch the Esign XML response..
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(res);
XmlNode xmlNode = xmlDocument.SelectSingleNode("EsignResp");
if (xmlNode.Attributes["errCode"] != null && xmlNode.Attributes["errCode"].Value != "NA")
{
}
else
{
byte[] numArray3 = this.SignDocument(res);
byte[] bytes1 = new byte[csize];
Array.Copy((Array)numArray3, 0, (Array)bytes1, 0, numArray3.Length);
PdfDictionary update = new PdfDictionary();
update.Put(PdfName.CONTENTS, (PdfObject)new PdfString(bytes1).SetHexWriting(true));
appearance.Close(update);
//progressBar1.Value = 100;
// MessageBox.Show("eSign Operation completed successfully.");
//fileStream1.Close();
//fileStream.Close();
//lblMsg.Text = "File Signed Successfully";
//For Multiple eSign Doc Start Here ...............
//if (Pageattr.Length > 1)
//{
// for (int k = 1; k <= Pageattr.Length - 1; k++)
// {
// String[] Pageattr1 = Pageattr[k].Split(new[] { "(", ")" }, StringSplitOptions.RemoveEmptyEntries);
// PageNo = int.Parse(Pageattr1[0]);
// lx = 0; ly = 0; hx = 0; hy = 0;
// string[] Attr = Pageattr1[1].Split(',');
// if (Attr.Length > 3)
// lx = float.Parse(Attr[0]);
// ly = float.Parse(Attr[1]);
// hx = float.Parse(Attr[2]);
// hy = float.Parse(Attr[3]);
// }
// New_File_Name = PdfeSign(New_File_Name, PageNo, res, lx, ly, hx, hy, strLayer2Text);
// }
//}
//For Multiple eSign Doc End Here ...............
}
}
catch (Exception ex)
{
stamper.Close();
pdfReader.Close();
}
finally
{
stamper.Close();
pdfReader.Close();
}