/* * This method defines the visual representation for each signature. */ private PadesVisualRepresentation getVisualRepresentation() { return(new PadesVisualRepresentation() { // The tags {{name}} and {{national_id}} will be substituted according to the user's certificate // // name : full name of the signer // national_id : if the certificate is ICP-Brasil, contains the signer's CPF // // For more information, see: https://github.com/LacunaSoftware/RestPkiSamples/blob/master/PadesTags.md Text = new PadesVisualText("Signed by {{name}} ({{national_id}})") { // Specify that the signing time should also be rendered IncludeSigningTime = true, // Optionally set the horizontal alignment of the text ('Left' or 'Right'), if not set the default is Left HorizontalAlign = PadesTextHorizontalAlign.Left }, // We'll use as background the image in Content/PdfStamp.png Image = new PadesVisualImage(Util.GetPdfStampContent(), "image/png") { // Opacity is an integer from 0 to 100 (0 is completely transparent, 100 is completely opaque) Opacity = 50, // Align the image to the right HorizontalAlign = PadesHorizontalAlign.Right }, // Position of the visual representation. We have encapsulated this code in a method to include several // possibilities depending on the argument passed. Experiment changing the argument to see different examples // of signature positioning. Once you decide which is best for your case, you can place the code directly here. Position = PadesVisualElements.GetVisualPositioning(1) }); }
// This function is called by the Pades Signature Starter action (see PadesSignatureController.cs). // It contains examples of PDF marks, isual elements of arbitrary content placed in every page. public static PdfMark GetPdfMark(int sampleNumber) { switch (sampleNumber) { case 1: // Example #1: A sample text and image are placed at the bottom of every page. // First, we create the mark object. It contains no elements, being a simple empty box. var mark = new PdfMark() { // Here, we set the mark's position in every page. Container = new PadesVisualRectangle() { // Specifying the width (but no left nor right) results in a horizontally centered fixed-width container Width = 8, // Specifying bottom and height (but no top) results in a bottom-aligned fixed-height container Bottom = 0.2, Height = 0.6 } // This example has no background and no borders, so we don't set BackgroundColor nor BorderColor }; // First, the image. mark.Elements.Add(new PdfMarkImageElement() { // We'll position it to the right of the text. RelativeContainer = new PadesVisualRectangle() { // Specifying right and width (but no left) results in a right-aligned fixed-width container Right = 0, Width = 1, // Specifying top and bottom (but no height) results in a variable-height container with the given margins Top = 0, Bottom = 0 }, // We'll use the image at 'Content/PdfStamp.png'. Image = new PdfMarkImage(Util.GetPdfStampContent(), "image/png") { // Opacity is an integer from 0 to 100 (0 is completely transparent, 100 is completely opaque). Opacity = 75, } }); // Then, the text. mark.Elements.Add(new PdfMarkTextElement() { // We center the text. RelativeContainer = new PadesVisualRectangle() { // Specifying left and right (but no width) results in a variable-width container with the given margins Left = 1, Right = 0, // Specifying just the height results in a vertically centered fixed-height container Height = 0.5 }, // Then add the text sections. TextSections = { // First, a simple message. new PdfTextSection() { // We set the text. Text = "This document was digitally signed with ", // Its color. Color = Color.Black, // Its size. FontSize = 8, // And the style. Style = PdfTextStyle.Normal }, // And a bold ending. new PdfTextSection() { // We set the text. Text = "RestPKI", // Its color. Color = Color.Black, // Its size. FontSize = 8, // And the style. Style = PdfTextStyle.Bold } } }); return(mark); case 2: // Example #2: An image will be placed at the bottom of every page. // First, we create the mark object. It contains no elements, being a simple empty box. mark = new PdfMark() { // Then, we set the mark's position in every page. Container = new PadesVisualRectangle() { // Specifying right and width (but no left) results in a right-aligned fixed-width container Right = 1, Width = 2.54, // Specifying bottom and height (but no top) results in a bottom-aligned fixed-height container Bottom = 1, Height = 2.54 }, // After that, its border must be configured. BorderWidth = 0.02, BorderColor = Color.Black }; // Add a single image element mark.Elements.Add(new PdfMarkImageElement() { // We'll make the image fill the entire mark, leaving space for the border RelativeContainer = new PadesVisualRectangle() { Left = 0.1, Right = 0.1, Top = 0.1, Bottom = 0.1 }, // We'll use the 'Content/PdfStamp.png' as background. Image = new PdfMarkImage(Util.GetPdfStampContent(), "image/png") { // Opacity is an integer from 0 to 100 (0 is completely transparent, 100 is completely opaque). Opacity = 50, } }); return(mark); case 3: // Example #3: 'Signed with RestPKI' is printed at the top of every page in a blue horizontal bar. // First, we create the mark object. It contains no elements, being a simple empty box. mark = new PdfMark() { // Then, we set the mark's position in every page. Container = new PadesVisualRectangle() { // Specifying left and right (but no width) results in a variable-width container with the given margins Left = 0, Right = 0, // Specifying top and height (but no bottom) results in a top-aligned fixed-height container Top = 0.5, Height = 1 }, // We'll not need a border, just a background color. BackgroundColor = Color.FromArgb(127, 0, 128, 192) }; // Add a single text element. mark.Elements.Add(new PdfMarkTextElement() { // We center the text. RelativeContainer = new PadesVisualRectangle() { // Specifying just the width results in a horizontally centered fixed-width container Width = 5, // Specifying just the height results in a vertically centered fixed-height container Height = 1 }, // Then add the text sections. TextSections = { // This example has a single section. new PdfTextSection() { // We set the text. Text = "Signed with RestPKI", // Its color. Color = Color.White, // Its size. FontSize = 12, // And the style. Style = PdfTextStyle.Bold } } }); return(mark); default: return(null); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Get an instance of the PadesSignatureStarter class, responsible for receiving the signature elements and start the // signature process var signatureStarter = Util.GetRestPkiClient().GetPadesSignatureStarter(); // Set the unit of measurement used to edit the pdf marks and visual representations signatureStarter.MeasurementUnits = PadesMeasurementUnits.Centimeters; // Set the file to be signed as a byte array signatureStarter.SetPdfToSign(Util.GetSampleDocContent()); // Set the signature policy signatureStarter.SetSignaturePolicy(StandardPadesSignaturePolicies.Basic); // Set a SecurityContext to be used to determine trust in the certificate chain signatureStarter.SetSecurityContext(StandardSecurityContexts.PkiBrazil); // Note: By changing the SecurityContext above you can accept only certificates from a certain PKI, // for instance, ICP-Brasil (Lacuna.RestPki.Api.StandardSecurityContexts.PkiBrazil). // Set a visual representation for the signature signatureStarter.SetVisualRepresentation(new PadesVisualRepresentation() { // The tags {{signerName}} and {{signerNationalId}} will be substituted according to the user's certificate // signerName -> full name of the signer // signerNationalId -> if the certificate is ICP-Brasil, contains the signer's CPF Text = new PadesVisualText("Signed by {{signerName}} ({{signerNationalId}})") { // Specify that the signing time should also be rendered IncludeSigningTime = true, // Optionally set the horizontal alignment of the text ('Left' or 'Right'), if not set the default is Left HorizontalAlign = PadesTextHorizontalAlign.Left }, // We'll use as background the image in Content/PdfStamp.png Image = new PadesVisualImage(Util.GetPdfStampContent(), "image/png") { // Opacity is an integer from 0 to 100 (0 is completely transparent, 100 is completely opaque). Opacity = 50, // Align the image to the right HorizontalAlign = PadesHorizontalAlign.Right }, // Position of the visual representation. We have encapsulated this code in a method to include several // possibilities depending on the argument passed. Experiment changing the argument to see different examples // of signature positioning. Once you decide which is best for your case, you can place the code directly here. Position = PadesVisualElements.GetVisualPositioning(1) }); /* * Optionally, add marks to the PDF before signing. These differ from the signature visual representation in that * they are actually changes done to the document prior to signing, not binded to any signature. Therefore, any number * of marks can be added, for instance one per page, whereas there can only be one visual representation per signature. * However, since the marks are in reality changes to the PDF, they can only be added to documents which have no previous * signatures, otherwise such signatures would be made invalid by the changes to the document (see property * PadesSignatureStarter.BypassMarksIfSigned). This problem does not occurr with signature visual representations. * * We have encapsulated this code in a method to include several possibilities depending on the argument passed. * Experiment changing the argument to see different examples of PDF marks. Once you decide which is best for your case, * you can place the code directly here. */ //signatureStarter.PdfMarks.Add(PadesVisualElements.GetPdfMark(1)); // Call the StartWithWebPki() method, which initiates the signature. This yields the token, a 43-character // case-sensitive URL-safe string, which identifies this signature process. We'll use this value to call the // signWithRestPki() method on the Web PKI component (see javascript on the view) and also to complete the signature // on the POST action below (this should not be mistaken with the API access token). var token = signatureStarter.StartWithWebPki(); ViewState["Token"] = token; } }