public void Sign(String src, String name, String dest, ICollection <X509Certificate> chain, ICipherParameters pk, String digestAlgorithm, CryptoStandard subfilter, String reason, String location) { // Creating the reader and the stamper PdfReader reader = new PdfReader(src); FileStream os = new FileStream(dest, FileMode.Create); PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0'); // Creating the appearance PdfSignatureAppearance appearance = stamper.SignatureAppearance; appearance.Reason = reason; appearance.Location = location; appearance.SetVisibleSignature(name); // Creating the appearance for layer 0 PdfTemplate n0 = appearance.GetLayer(0); float x = n0.BoundingBox.Left; float y = n0.BoundingBox.Bottom; float width = n0.BoundingBox.Width; float height = n0.BoundingBox.Height; n0.SetColorFill(BaseColor.LIGHT_GRAY); n0.Rectangle(x, y, width, height); n0.Fill(); // Creating the appearance for layer 2 PdfTemplate n2 = appearance.GetLayer(2); ColumnText ct = new ColumnText(n2); ct.SetSimpleColumn(n2.BoundingBox); Paragraph p = new Paragraph("This document was signed by Bruno Specimen."); ct.AddElement(p); ct.Go(); // Creating the signature IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String dest, ICollection <X509Certificate> chain, X509Certificate2 pk, String digestAlgorithm, CryptoStandard subfilter, String reason, String location, ICollection <ICrlClient> crlList, IOcspClient ocspClient, ITSAClient tsaClient, int estimatedSize, int RowIdx, int RowHeight, int x, int y, int NameWidth, int DateWidth, String RevIndex, String RevStep, String Reason, String Name, String Date) { // Creating the reader and the stamper PdfReader reader = null; PdfStamper stamper = null; FileStream os = null; try { reader = new PdfReader(src); os = new FileStream(dest, FileMode.Create); // os = new FileStream(dest, FileMode.Create, FileAccess.Write); //Activate MultiSignatures stamper = PdfStamper.CreateSignature(reader, os, '\0', null, true); //To disable Multi signatures uncomment this line : every new signature will invalidate older ones ! //stamper = PdfStamper.CreateSignature(reader, os, '\0'); // Creating the appearance PdfSignatureAppearance appearance = stamper.SignatureAppearance; Rectangle rectangle = new Rectangle(x, y + RowIdx * RowHeight, x + NameWidth + DateWidth, y + (RowIdx + 1) * RowHeight); appearance.SetVisibleSignature(rectangle, 1, "Revision " + RevIndex + "|" + RevStep); appearance.Reason = "marked as changed"; appearance.Location = location; appearance.Layer2Text = "Signed on " + DateTime.Now; appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION; PdfTemplate n2 = appearance.GetLayer(2); Font font = new Font(); font.SetColor(255, 0, 0); font.Size = 10; ColumnText ct1 = new ColumnText(n2); ct1.SetSimpleColumn(new Phrase(Name, font), 0, 0, NameWidth, rectangle.Height, 15, Element.ALIGN_LEFT); ct1.Go(); ColumnText ct2 = new ColumnText(n2); ct2.SetSimpleColumn(new Phrase(Date, font), NameWidth, 0, rectangle.Width, rectangle.Height, 15, Element.ALIGN_LEFT); ct2.Go(); //n2.ConcatCTM(1, 0, 0, -1, 0, 0); //n2.SaveState(); // Creating the signature IExternalSignature pks = new X509Certificate2Signature(pk, digestAlgorithm); MakeSignature.SignDetached(appearance, pks, chain, crlList, ocspClient, tsaClient, estimatedSize, subfilter); } catch (Exception ex) { Console.WriteLine("GMA: " + ex.Message); } finally { if (reader != null) { reader.Close(); } if (stamper != null) { stamper.Close(); } if (os != null) { os.Close(); } } }
public static void SignHashed(MemoryStream Source, string Target, SysX509.X509Certificate2 Certificate, string Reason, string Location, bool AddVisibleSign, Image img, int nroHojaFirma, string path, float h, string att_1, string att_2, string att_3, string url_terminos) { try { X509CertificateParser objCP = new X509CertificateParser(); X509Certificate[] objChain = new X509Certificate[] { objCP.ReadCertificate(Certificate.RawData) }; IExternalSignature externalSignature = new X509Certificate2Signature(Certificate, "SHA-1"); PdfReader objReader = new PdfReader(Source); //string[] msg = Certificate.SubjectName.Name.Split(','); //Document document = new Document(PageSize.A4, 50, 50, 150, 100); //PdfWriter pdfwritter = PdfWriter.GetInstance(document, new FileStream("C:\\Users\\Public\\terminos_condiciones.pdf", FileMode.OpenOrCreate)); using (PdfReader readerTerm = new PdfReader(url_terminos)) using (MemoryStream workStream = new MemoryStream()) { PdfStamper objStamper = PdfStamper.CreateSignature(objReader, new FileStream(Target, FileMode.OpenOrCreate, FileAccess.Write), '\0'); int nroPages = objReader.NumberOfPages + 1; Rectangle rectangle = readerTerm.GetPageSize(1); objStamper.InsertPage(nroPages, rectangle); PdfImportedPage bg = objStamper.GetImportedPage(readerTerm, 1); objStamper.GetUnderContent(nroPages).AddTemplate(bg, 0, 0); PdfSignatureAppearance objSA = objStamper.SignatureAppearance; img.ScaleAbsolute(120f, 60f); img.SetAbsolutePosition(0, 28); BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false); BaseFont bfBold = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false); if (true) { objSA.SetVisibleSignature(new Rectangle(50, h - 120, 200, h), nroHojaFirma, "Firma Digital emitida por el sistema BV Digital"); } PdfTemplate n2Layer = objSA.GetLayer(2); n2Layer.BeginText(); n2Layer.SetFontAndSize(bfBold, 7); n2Layer.ShowTextAligned(Element.ALIGN_LEFT, "Inspectorate Services Perú S.A.C", 0, 100, 0); n2Layer.ShowTextAligned(Element.ALIGN_LEFT, "A Bureau Veritas Group Company", 0, 90, 0); n2Layer.EndText(); n2Layer.AddImage(img); n2Layer.BeginText(); n2Layer.SetFontAndSize(bf, 7); n2Layer.ShowTextAligned(Element.ALIGN_LEFT, "Firmado Digitalmente por", 0, 40, 0); //string user = msg[2].Substring(msg[2].IndexOf('=') + 1); //user += " " + msg[3].Substring(msg[3].IndexOf('=') + 1); n2Layer.ShowTextAligned(Element.ALIGN_LEFT, att_3, 0, 30, 0); n2Layer.ShowTextAligned(Element.ALIGN_LEFT, "Fecha: " + objSA.SignDate.ToString(), 0, 20, 0); n2Layer.ShowTextAligned(Element.ALIGN_LEFT, att_1, 0, 10, 0); n2Layer.ShowTextAligned(Element.ALIGN_LEFT, att_2, 0, 0, 0); n2Layer.EndText(); objSA.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION; MakeSignature.SignDetached(objSA, externalSignature, objChain, null, null, null, 0, CryptoStandard.CMS); objStamper.SetFullCompression(); } } catch (Exception e) { Utility.log_err.save(null, e); } }
private void AddSignature(PdfReader pdf, PdfSignatureAppearance sap, Juschubut.PdfDigitalSign.CertificateInfo certificate) { int anchoPagina = (int)pdf.GetPageSize(1).Width; var rectangle = this.Appearance.GetRectangle(anchoPagina); var signatureRect = new iTextSharp.text.Rectangle((float)rectangle.X, (float)rectangle.Y, (float)(rectangle.X + rectangle.Width), (float)(rectangle.Y + rectangle.Height)); int pagina = pdf.NumberOfPages; if (this.Appearance.Page.HasValue && this.Appearance.Page.Value <= pdf.NumberOfPages) { pagina = this.Appearance.Page.Value; } sap.SetVisibleSignature(signatureRect, pagina, null); sap.SignDate = DateTime.Now; sap.Acro6Layers = true; sap.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION; if (!string.IsNullOrEmpty(this.Appearance.SignatureImage)) { var fileInfo = new FileInfo(this.Appearance.SignatureImage); if (!fileInfo.Exists) { this.Log(string.Format("No se encontro archivo de firma ológrafa ({0})", this.Appearance.SignatureImage)); } else if (fileInfo.Length == 0) { this.Log(string.Format("El archivo de firma ológrafa está vacio ({0})", this.Appearance.SignatureImage)); } else { try { sap.SignatureGraphic = iTextSharp.text.Image.GetInstance(new Uri(this.Appearance.SignatureImage)); } catch (Exception ex) { Log(string.Format("Error al leer el archivo de firma ológrafo. El archivo esta dañado, no es un archivo .png, o no se puede tener acceso de lectura. {0}", this.Appearance.SignatureImage)); StringBuilder sb = new StringBuilder(); var ex2 = ex; do { sb.AppendLine(ex2.Message); ex2 = ex2.InnerException; }while (ex2 != null); Log("Error: " + sb.ToString()); if (!string.IsNullOrEmpty(this.Appearance.SignatureImageDefault)) { try { Log("Cargando firma por default"); sap.SignatureGraphic = iTextSharp.text.Image.GetInstance(new Uri(this.Appearance.SignatureImageDefault)); } catch (Exception ex3) { Log(ex3.Message); } } } sap.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC; sap.SignatureGraphic.Alignment = iTextSharp.text.Image.ALIGN_TOP; } } // Descripcion de la Firma iTextSharp.text.Font textFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, (float)10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK); var phrase = new iTextSharp.text.Phrase(); phrase.Font = textFont; int fontPre = 6; int fontFirma = this.Appearance.Height < 100 ? 8 : 10; int fontPost = this.Appearance.Height < 100 ? 7 : 10; if (this.Appearance.PreFirma != null) { AddTexto(phrase, this.Appearance.PreFirma, iTextSharp.text.Font.NORMAL, fontPre); } AddTexto(phrase, certificate.CN, iTextSharp.text.Font.BOLD, fontFirma); if (this.Appearance.PostFirma != null) { AddTexto(phrase, this.Appearance.PostFirma, iTextSharp.text.Font.NORMAL, fontPost); } PdfTemplate n2 = sap.GetLayer(0); ColumnText ct = new ColumnText(n2); float x = n2.BoundingBox.Left; float y = n2.BoundingBox.Top; float w = n2.BoundingBox.Width; float h = n2.BoundingBox.Height; float x1 = x; float y1 = y; float x2 = x + w; float y2 = y - h; // new working code: crreate a table PdfPTable table = new PdfPTable(1); table.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER; table.SetWidths(new[] { 1 }); table.WidthPercentage = 100; table.AddCell(new PdfPCell(phrase) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER, VerticalAlignment = iTextSharp.text.Element.ALIGN_BOTTOM, FixedHeight = y1, Border = iTextSharp.text.Rectangle.NO_BORDER }); ct.SetSimpleColumn(x1, y1, x2, y2); ct.AddElement(table); ct.Go(); }