/** * Signs a document with a PAdES-LTV Timestamp. The document is closed at the end. * @param sap the signature appearance * @param tsa the timestamp generator * @param signatureName the signature name or null to have a name generated * automatically * @throws Exception */ public static void Timestamp(PdfSignatureAppearance sap, ITSAClient tsa, String signatureName) { int contentEstimated = tsa.GetTokenSizeEstimate(); sap.SetVisibleSignature(new Rectangle(0,0,0,0), 1, signatureName); PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ETSI_RFC3161); dic.Put(PdfName.TYPE, PdfName.DOCTIMESTAMP); sap.CryptoDictionary = dic; Dictionary<PdfName,int> exc = new Dictionary<PdfName,int>(); exc[PdfName.CONTENTS] = contentEstimated * 2 + 2; sap.PreClose(exc); Stream data = sap.RangeStream; IDigest messageDigest = DigestUtilities.GetDigest(tsa.GetDigestAlgorithm()); byte[] buf = new byte[4096]; int n; while ((n = data.Read(buf, 0, buf.Length)) > 0) { messageDigest.BlockUpdate(buf, 0, n); } byte[] tsImprint = new byte[messageDigest.GetDigestSize()]; messageDigest.DoFinal(tsImprint, 0); byte[] tsToken = tsa.GetTimeStampToken(tsImprint); if (contentEstimated + 2 < tsToken.Length) throw new Exception("Not enough space"); byte[] paddedSig = new byte[contentEstimated]; System.Array.Copy(tsToken, 0, paddedSig, 0, tsToken.Length); PdfDictionary dic2 = new PdfDictionary(); dic2.Put(PdfName.CONTENTS, new PdfString(paddedSig).SetHexWriting(true)); sap.Close(dic2); }
public string AddSignature(string PathSource, string PathTarget, string CertPath, string CertPass, int lx = 100, int ly = 100, int ux = 250, int uy = 150, int page = 1, bool Visible = true) { try { Org.BouncyCastle.Crypto.AsymmetricKeyParameter Akp = null; Org.BouncyCastle.X509.X509Certificate[] Chain = null; string alias = null; Org.BouncyCastle.Pkcs.Pkcs12Store pk12; pk12 = new Org.BouncyCastle.Pkcs.Pkcs12Store(new System.IO.FileStream(CertPath, System.IO.FileMode.Open, System.IO.FileAccess.Read), CertPass.ToCharArray()); IEnumerable aliases = pk12.Aliases; foreach (string aliasTemp in aliases) { alias = aliasTemp; if (pk12.IsKeyEntry(alias)) { break; } } Akp = pk12.GetKey(alias).Key; Org.BouncyCastle.Pkcs.X509CertificateEntry[] ce = pk12.GetCertificateChain(alias); Chain = new Org.BouncyCastle.X509.X509Certificate[ce.Length]; for (int k = 0; k < ce.Length; ++k) { Chain[k] = ce[k].Certificate; } iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(PathSource); iTextSharp.text.pdf.PdfStamper st = iTextSharp.text.pdf.PdfStamper.CreateSignature(reader, new System.IO.FileStream(PathTarget, System.IO.FileMode.Create, System.IO.FileAccess.Write), '\0', null, true); iTextSharp.text.pdf.PdfSignatureAppearance sap = st.SignatureAppearance; if (Visible == true) { page = (page <1 || page> reader.NumberOfPages) ? 1 : page; sap.SetVisibleSignature(new iTextSharp.text.Rectangle(lx, ly, ux, uy), page, null); } sap.CertificationLevel = iTextSharp.text.pdf.PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED; // digital signature - http://itextpdf.com/examples/iia.php?id=222 IExternalSignature es = new PrivateKeySignature(Akp, "SHA-256"); // "BC" MakeSignature.SignDetached(sap, es, new X509Certificate[] { pk12.GetCertificate(alias).Certificate }, null, null, null, 0, CryptoStandard.CMS); st.Close(); return(""); } catch (Exception e) { return(e.Message); } }
public static void SetAppearance(YapsConfig config, PdfSignatureAppearance sap) { var appearance = config.Appearance ?? new SignatureAppearance(); sap.Reason = appearance.Reason; sap.Contact = appearance.Contact; sap.Location = appearance.Location; sap.SignDate = DateTime.Now; sap.Acro6Layers = true; if (!config.Visible || !appearance.ValidateRect()) return; //iTextSharp.text.Rectangle rect = st.Reader.GetPageSize(sigAP.Page); var xi = appearance.X + appearance.Width; var yi = appearance.Y + appearance.Height; var rect = new iTextSharp.text.Rectangle(appearance.X, appearance.Y, xi, yi); //sap.Image = sigAP.RawData == null ? null : iTextSharp.text.Image.GetInstance(sigAP.RawData); if (!string.IsNullOrEmpty(appearance.CustomText)) sap.Layer2Text = appearance.CustomText; //sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 300, 200), 1, "Signature"); sap.SetVisibleSignature(rect, appearance.Page, "Signature"); }
private static void SetSigPosition(PdfSignatureAppearance sigAppearance, int oldSigCount) { //Note: original formula from QuangNgV, ll = lower left, ur = upper right, coordinates are calculated relative from the lower left of the pdf page float llx = (100 + 20) * (oldSigCount % 5), lly = (25 + 20) * (oldSigCount / 5), urx = llx + 100, ury = lly + 25; sigAppearance.SetVisibleSignature(new Rectangle(llx, lly, urx, ury), 1, null); }
private void addVisibleSignature(PdfSignatureAppearance signAppearance, PdfStamper stamper) { if (SignatureData.VisibleSignature == null) return; signAppearance.Image = string.IsNullOrEmpty(SignatureData.VisibleSignature.ImagePath) ? null : Image.GetInstance(SignatureData.VisibleSignature.ImagePath); signAppearance.Layer2Text = SignatureData.VisibleSignature.CustomText; if (SignatureData.VisibleSignature.RunDirection == null) SignatureData.VisibleSignature.RunDirection = PdfRunDirection.LeftToRight; signAppearance.RunDirection = (int)SignatureData.VisibleSignature.RunDirection; signAppearance.Layer2Font = SignatureData.VisibleSignature.Font.Fonts[0]; var pageNumber = SignatureData.VisibleSignature.UseLastPageToShowSignature ? stamper.Reader.NumberOfPages : SignatureData.VisibleSignature.PageNumberToShowSignature; signAppearance.SetVisibleSignature(SignatureData.VisibleSignature.Position, pageNumber, null); }
/// <summary> /// Assina o arquivo PDF /// </summary> /// <param name="filePath">Caminho do arquivo</param> /// <param name="certificate">Certificado</param> /// <param name="reason">Motivo da assinatura</param> internal static void sign(string filePath, X509Certificate2 certificate, string reason = null) { try { // make the certificate chain IList <BCX.X509Certificate> chain = getCertChain(certificate); // open the original file TS.PdfReader reader = new TS.PdfReader(filePath); string newFilePath = filePath.Substring(0, filePath.Length - 4) + "_signed.pdf"; // create a new file FileStream fout = new FileStream(newFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); // create the "stamp" on the file TS.PdfStamper stamper = TS.PdfStamper.CreateSignature(reader, fout, '\0', null, true); TS.PdfSignatureAppearance appearance = stamper.SignatureAppearance; appearance.Reason = reason; appearance.Location = getLocation(certificate.Subject); int i = 1; int xdiff = 0; while (true) { string fieldName = "Assinatura" + i.ToString();; try { appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(20 + xdiff, 10, 170 + xdiff, 60), 1, fieldName); TSS.X509Certificate2Signature es = new TSS.X509Certificate2Signature(certificate, "SHA-1"); TSS.MakeSignature.SignDetached(appearance, es, chain, null, null, null, 0, TSS.CryptoStandard.CMS); break; } catch (Exception ex) { if (ex.Message != "The field " + fieldName + " already exists.") { throw ex; } else { i++; xdiff += 180; } } } // close the files reader.Close(); fout.Close(); // delete the tmp file e move the new to the right name System.IO.File.Delete(filePath); System.IO.File.Move(newFilePath, filePath); } catch (Exception ex) { throw ex; } }