1
Reply

digital signature at multiple position in PDF file

ranjith shetty

ranjith shetty

Jun 7 2017 5:01 AM
433

I am using below code to sign the single PDF file at multiple location but sign appear at one place .

Below is the code

string dttime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");
PdfReader pdfReader = null;
PdfStamper stamper = null;

try
{
//FileStream fileStream = new FileStream(sTempPath + f_n + "_" + DT + ".pdf", FileMode.Open, FileAccess.Read);
//FileStream fileStream1 = new FileStream(Server.MapPath("~/fs/") + f_n + "_Signed_" + DT + ".pdf", FileMode.Create, FileAccess.Write);
// FileStream fileStream = new FileStream(In_File_Name, FileMode.Open, FileAccess.Read);
//FileStream fileStream1 = new FileStream(sTempPath + f_n + "_Signed_" + DT + a1[1].ToString(), FileMode.Create, FileAccess.Write);
// FileStream fileStream1 = new FileStream(New_File_Name, FileMode.Create, FileAccess.Write);
//Session["SignedFileName"] = f_n + "_Signed" + DT + ".pdf";

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]);
}

// appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(540, 290, 430, 320), PageNo, null);
// appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(lx, ly, hx, hy), 2, null);

int csize = 8192;

//if (ConfigurationManager.AppSettings["Layer2Text"].ToString().ToUpper() == "YES")
//{
// if (In_key1.ToString().Trim() != "")
// {
// strLayer2Text = "Digitally signed by: " + In_key1 + "\nDate: " + DateTime.Now.AddMinutes(3) + "\nReason: " + ConfigurationManager.AppSettings["APPEARANCE_REASON"].ToString() + "\nLocation: " + ConfigurationManager.AppSettings["APPEARANCE_LOCATION"].ToString();
// appearance.Layer2Text = strLayer2Text;
// }
// else
// {
// strLayer2Text = "Digitally signed by: SONAWANE LTD customer" + "\nDate: " + DateTime.Now.AddMinutes(3) + "\nReason: " + ConfigurationManager.AppSettings["APPEARANCE_REASON"].ToString() + "\nLocation: " + ConfigurationManager.AppSettings["APPEARANCE_LOCATION"].ToString();
// appearance.Layer2Text = strLayer2Text;
// appearance.Layer2Font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 8);
// }
//}

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();
}


Answers (1)