public void Sign2(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance.SetReason(reason); appearance.SetLocation(location); signer.SetFieldName(name); // Creating the appearance for layer 2 PdfFormXObject n2 = appearance.GetLayer2(); // Custom text, custom font, and right-to-left writing // Characters: لورانس العرب Text text = new Text("\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628"); text.SetFont(PdfFontFactory.CreateFont("../../../resources/font/NotoNaskhArabic-Regular.ttf", PdfEncodings.IDENTITY_H, true)); text.SetBaseDirection(BaseDirection.RIGHT_TO_LEFT); new Canvas(n2, signer.GetDocument()).Add(new Paragraph(text).SetTextAlignment(TextAlignment.RIGHT)); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public void Sign(String src, String name, String dest, X509Certificate[] chain, ICipherParameters pk, String digestAlgorithm, PdfSigner.CryptoStandard subfilter, String reason, String location) { PdfReader reader = new PdfReader(src); PdfSigner signer = new PdfSigner(reader, new FileStream(dest, FileMode.Create), new StampingProperties()); // Create the signature appearance PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance .SetReason(reason) .SetLocation(location); // This name corresponds to the name of the field that already exists in the document. signer.SetFieldName(name); // Get the background layer and draw a gray rectangle as a background. PdfFormXObject n0 = appearance.GetLayer0(); float x = n0.GetBBox().ToRectangle().GetLeft(); float y = n0.GetBBox().ToRectangle().GetBottom(); float width = n0.GetBBox().ToRectangle().GetWidth(); float height = n0.GetBBox().ToRectangle().GetHeight(); PdfCanvas canvas = new PdfCanvas(n0, signer.GetDocument()); canvas.SetFillColor(ColorConstants.LIGHT_GRAY); canvas.Rectangle(x, y, width, height); canvas.Fill(); // Set the signature information on layer 2 PdfFormXObject n2 = appearance.GetLayer2(); Paragraph p = new Paragraph("This document was signed by Bruno Specimen."); new Canvas(n2, signer.GetDocument()).Add(p); IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm); // Sign the document using the detached mode, CMS or CAdES equivalent. signer.SignDetached(pks, chain, null, null, null, 0, subfilter); }
public static byte[] Sign(IExternalSignature externalSignature, X509Certificate[] certChain, string src, string friendlyName, string subject, string sourceName, string documentLink, string documentName) { int numberOfSignatures = 0; int numberOfPages = 0; using (PdfReader reader = new PdfReader(src)) { using (PdfDocument pdf = new PdfDocument(reader)) { numberOfPages = pdf.GetNumberOfPages(); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, false); if (form != null) { foreach (var field in form.GetFormFields()) { if (field.Value is iText.Forms.Fields.PdfSignatureFormField) { numberOfSignatures++; } } } } } if (numberOfSignatures == 0) { string hash = GetMD5HashFromFile(src); src = AddPage(src, sourceName, documentLink, documentName, hash); numberOfPages += 1; } float posSignY = 615 - (numberOfSignatures * 70); using (PdfReader reader = new PdfReader(src)) { StampingProperties stampingProperties = new StampingProperties(); stampingProperties.UseAppendMode(); using (MemoryStream ms = new MemoryStream()) { PdfSigner signer = new PdfSigner(reader, ms, stampingProperties); Rectangle rect = new Rectangle(36, posSignY, 520, 65); PdfSignatureAppearance appearance = signer.GetSignatureAppearance(); appearance .SetPageRect(rect) .SetPageNumber(numberOfPages) .SetCertificate(certChain[0]); PdfFormXObject n2 = appearance.GetLayer2(); Canvas canvas = new Canvas(n2, signer.GetDocument()); canvas.Add(new Paragraph(friendlyName).SetMargin(0)); canvas.Add(new Paragraph("Assinado digitalmente por: " + friendlyName).SetFontSize(10).SetMargin(0)); canvas.Add(new Paragraph("Data: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz")).SetFontSize(10).SetMargin(0)); canvas.Add(new Paragraph("Subject: " + subject).SetFontSize(10).SetMargin(0)); signer.SignDetached(externalSignature, certChain, null, null, null, 0, PdfSigner.CryptoStandard.CADES); return(ms.ToArray()); } } }