/// <summary> /// Sign document with digital signature /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Basic Usage] # SignWithDigital : Sign document with digital certificate\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_PDF; string fileName = Path.GetFileName(filePath); string imagePath = Constants.ImageHandwrite; string certificatePath = Constants.CertificatePfx; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithDigital", fileName); using (Signature signature = new Signature(filePath)) { DigitalSignOptions options = new DigitalSignOptions(certificatePath) { // optional: setup image file path ImageFilePath = imagePath, // Left = 50, Top = 50, PageNumber = 1, Password = "******" }; // sign document to file SignResult result = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {result.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); } }
/// <summary> /// Add digital signature options for word document /// </summary> /// <returns>SignOptions</returns> public override SignOptions SignWord() { DigitalSignOptions signOptions = new DigitalSignOptions(SignatureData.SignatureGuid); SetOptions(signOptions); return(signOptions); }
static DigitalSignOptions GetDigitalSignOptions() { string imagePath = Constants.ImageHandwrite; string certificatePath = Constants.CertificatePfx; string password = "******"; DigitalSignOptions result = new DigitalSignOptions(certificatePath, imagePath); result.Password = password; // alignment settings result.Left = 100; result.Top = 550; result.Width = 200; result.Height = 120; result.AllPages = true; result.PageNumber = 1; result.PagesSetup = new PagesSetup() { FirstPage = true, LastPage = false, OddPages = true, EvenPages = false, PageNumbers = { 1, 2, 3 } }; result.HorizontalAlignment = Domain.HorizontalAlignment.Left; result.VerticalAlignment = Domain.VerticalAlignment.Bottom; // border settings result.Border.Color = System.Drawing.Color.Red; result.Border.DashStyle = GroupDocs.Signature.Domain.DashStyle.DashLongDash; result.Border.Transparency = 0.8; result.Border.Weight = 2; result.Border.Visible = true; return(result); }
/// <summary> /// Sign document with digital signature applying specific options /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignWithDigitalAdvanced : Sign document with digital signature applying specific options\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_PDF; string fileName = Path.GetFileName(filePath); string imagePath = Constants.ImageStamp; string certificatePath = Constants.CertificatePfx; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithDigitalAdvanced", fileName); using (Signature signature = new Signature(filePath)) { DigitalSignOptions options = new DigitalSignOptions(certificatePath) { // certificate password Password = "******", // digital certificate details Reason = "Approved", Contact = "John Smith", Location = "New York", // image as digital certificate appearance on document pages ImageFilePath = imagePath, // AllPages = true, Width = 160, Height = 80, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Left, Margin = new Padding() { Bottom = 10, Right = 10 }, // setup signature border Border = new Border() { Visible = true, Color = Color.Red, DashStyle = DashStyle.DashDot, Weight = 2 }, }; SignResult signResult = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); Console.WriteLine("\nList of newly created signatures:"); int number = 1; foreach (BaseSignature temp in signResult.Succeeded) { Console.WriteLine($"Signature #{number++}: Type: {temp.SignatureType} Id:{temp.SignatureId}, Location: {temp.Left}x{temp.Top}. Size: {temp.Width}x{temp.Height}"); } } }
public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Quick Start] # Options serialization and deserialization\n"); List <SignOptions> collection = new List <SignOptions>(); TextSignOptions textSignOptions = GetTextSignOptions(); collection.Add(textSignOptions); ImageSignOptions imageSignOptions = GetImageSignOptions(); collection.Add(imageSignOptions); DigitalSignOptions digitalSignOptions = GetDigitalSignOptions(); collection.Add(digitalSignOptions); BarcodeSignOptions barcodeSignOptions = GetBarcodeSignOptions(); collection.Add(barcodeSignOptions); QrCodeSignOptions qrCodeSignOptions = GetQrCodeSignOptions(); collection.Add(qrCodeSignOptions); string serialized = JsonConvert.SerializeObject(collection); JsonConverter[] converters = { new SignOptionsJsonConverter(), new BarcodeTypeJsonConverter(), new QrCodeTypeJsonConverter() }; List <SignOptions> deserialized = JsonConvert.DeserializeObject <List <SignOptions> >(serialized, converters); Console.WriteLine(deserialized.Count); if (deserialized != null) { // The path to the documents directory. string filePath = Constants.SAMPLE_PDF; string fileName = System.IO.Path.GetFileName(filePath); string outputFilePath = Path.Combine(Constants.OutputPath, "OptionsSerialization", fileName); using (Signature signature = new Signature(filePath)) { // sign document to file SignResult signResult = signature.Sign(outputFilePath, deserialized); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); } } }
/// <summary> /// Sign document with digital signature applying PDF document-specific options /// Please be aware that this example works only on licensed product due to limitation with Pdf processing /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignWithDigitalAdvancedPdfCertificate : Sign document with digital signature applying PDF certificate options\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_PDF; string certificatePath = Constants.CertificatePfx; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithDigitalAdvancedPdf", "digitallyCertified.pdf"); //Certify pdf document with digital signature using (Signature signature = new Signature(filePath)) { PdfDigitalSignature pdfDigitalSignature = new PdfDigitalSignature() { ContactInfo = "Contact", Location = "Location", Reason = "Reason", // Setting pdf digital signature type as Certificate Type = PdfDigitalSignatureType.Certificate }; //Create digital signing options DigitalSignOptions options = new DigitalSignOptions(certificatePath) { // certificate password Password = "******", // Setting document-specific options Signature = pdfDigitalSignature, // Page position VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right }; SignResult signResult = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); Console.WriteLine("\nList of newly created signatures:"); int number = 1; foreach (BaseSignature temp in signResult.Succeeded) { Console.WriteLine($"Signature #{number++}: Type: {temp.SignatureType} Id:{temp.SignatureId}, Location: {temp.Left}x{temp.Top}. Size: {temp.Width}x{temp.Height}"); } } }
/// <summary> /// Sign document with digital signature applying specific options /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignWithDigitalAppearance : Sign document with digital signature applying specific options\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_WORDPROCESSING; string fileName = Path.GetFileName(filePath); string imagePath = Constants.ImageHandwrite; string certificatePath = Constants.CertificatePfx; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithAppearances", "DigitalAppearance.docx"); using (Signature signature = new Signature(filePath)) { DigitalSignOptions options = new DigitalSignOptions(certificatePath) { // certificate password Password = "******", // digital certificate details Reason = "Sign", Contact = "JohnSmith", Location = "Office1", // image as digital certificate appearance on document pages ImageFilePath = imagePath, // AllPages = true, Width = 80, Height = 60, VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Padding() { Bottom = 10, Right = 10 }, // Setup signature line appearance. // This appearance will add Signature Line on the first page. // Could be useful for .xlsx files. Appearance = new DigitalSignatureAppearance("John Smith", "Title", "*****@*****.**") }; SignResult signResult = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); } }
private static void SetOptions(DigitalSignOptions signOptions) { if (signOptions is DigitalSignOptions) { signOptions.Reason = SignatureData.Reason; signOptions.Contact = SignatureData.Contact; signOptions.Location = SignatureData.Address; } else { signOptions.Signature.Comments = SignatureData.SignatureComment; } if (!String.IsNullOrEmpty(SignatureData.Date)) { signOptions.Signature.SignTime = DateTime.ParseExact(SignatureData.Date, "dd-MM-yyyy", CultureInfo.InvariantCulture); } signOptions.Password = Password; signOptions.AllPages = true; }
private ImageSignOptions GetDigitalSignOptions(string signText, string signImagePath, string signLocation, string signSize) { string pfxPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6) + "\\sample_certificate.pfx"; var options = new DigitalSignOptions(pfxPath, signImagePath) { Contact = signText, Reason = "reason", Location = "location", Password = "******", Width = 100, Height = 100, VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Right, Margin = new Padding(10), }; QrCodeSignOptions locationOptions = GetLocationOptions(signLocation); options.HorizontalAlignment = locationOptions.HorizontalAlignment; options.VerticalAlignment = locationOptions.VerticalAlignment; switch (signSize) { case "small": options.Width = 50; options.Height = 50; break; case "medium": options.Width = 50 * 2; options.Height = 50 * 2; break; case "large": options.Width = 50 * 3; options.Height = 50 * 3; break; } return(options); }
protected override SignOptions Create(Type objectType, JObject jObject) { SignOptions result = null; var signatureType = GetSignatureType(jObject); // check SignatureType // check for Barcode options if (signatureType == SignatureType.Barcode) { result = new BarcodeSignOptions(); } // check for QrCode options if (result == null && signatureType == SignatureType.QrCode) { result = new QrCodeSignOptions(); } // check for digital options if (result == null && signatureType == SignatureType.Digital) { result = new DigitalSignOptions(); } // check for text options if (result == null && signatureType == SignatureType.Text) { result = new TextSignOptions(); } // check for image options if (result == null && signatureType == SignatureType.Image) { result = new ImageSignOptions(); } // check for stamp options if (result == null && signatureType == SignatureType.Stamp) { result = new StampSignOptions(); } return(result); }
/// <summary> /// Sign document with exception handling /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignWithExceptionHandling : Sign document with exception handling\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_WORDPROCESSING; string fileName = Path.GetFileName(filePath); string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithExceptionsHandling", fileName); try { using (Signature signature = new Signature(filePath)) { DigitalSignOptions options = new DigitalSignOptions() { CertificateFilePath = Constants.CertificatePfx, ImageFilePath = Constants.ImageHandwrite, // skip password specification //Password = "******" }; // sign document to file signature.Sign(outputFilePath, options); } } catch (GroupDocsSignatureException ex) { Console.WriteLine("GroupDocs Signature Exception: " + ex.Message); } catch (Exception ex) { Console.WriteLine("System Exception: " + ex.Message); } }
/// <summary> /// Sign document with XML Advanced Electronic Signatures (XAdES) /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignWithXAdESTypes : Sign document with XML Advanced Electronic Signatures (XAdES)\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_SPREADSHEET; string fileName = Path.GetFileName(filePath); string certificatePath = Constants.CertificatePfx; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithXAdESTypes", fileName); using (Signature signature = new Signature(filePath)) { DigitalSignOptions options = new DigitalSignOptions(certificatePath) { // set XAdES type XAdESType = XAdESType.XAdES, // certificate password Password = "******", // digital certificate details Reason = "Sign", Contact = "JohnSmith", Location = "Office1" }; SignResult signResult = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); Console.WriteLine("\nList of newly created signatures:"); int number = 1; foreach (BaseSignature temp in signResult.Succeeded) { Console.WriteLine($"Signature #{number++}: Type: {temp.SignatureType} Id:{temp.SignatureId}"); } } }
/// <summary> /// Sign PDF Document with digital signature applying special signature appearance settings. /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignWithPdfDigitalAdvanced : Sign PDF document with digital signature applying special signature appearance settings\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_PDF; string fileName = Path.GetFileName(filePath); string certificatePath = Constants.CertificatePfx; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithPdfDigitalAdvanced", fileName); using (Signature signature = new Signature(filePath)) { DigitalSignOptions options = new DigitalSignOptions(certificatePath) { // certificate password Password = "******", // digital certificate details Reason = "Approved", Location = "New York", // apply custom PDF signature appearance Appearance = new PdfDigitalSignatureAppearance() { // do now show contact details ContactInfoLabel = string.Empty, // simplify reason label ReasonLabel = "?", // change location label LocationLabel = "From", DigitalSignedLabel = "By", DateSignedAtLabel = "On", // apply custom appearance color Background = Color.Red }, // AllPages = true, Width = 160, Height = 80, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Left, Margin = new Padding() { Bottom = 10, Right = 10 }, // setup signature border Border = new Border() { Visible = true, Color = Color.Red, DashStyle = DashStyle.DashDot, Weight = 2 } }; SignResult signResult = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); Console.WriteLine("\nList of newly created signatures:"); int number = 1; foreach (BaseSignature temp in signResult.Succeeded) { Console.WriteLine($"Signature #{number++}: Type: {temp.SignatureType} Id:{temp.SignatureId}, Location: {temp.Left}x{temp.Top}. Size: {temp.Width}x{temp.Height}"); } } }
/// <summary> /// Sign document with multiple signature types /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Basic Usage] # SignWithMultipleOptions : Sign document with multiple signature types \n"); // The path to the documents directory. string filePath = Constants.SAMPLE_WORDPROCESSING; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithMultiple", "SignWithMultiple.docx"); using (Signature signature = new Signature(filePath)) { // define several signature options of different types and settings TextSignOptions textOptions = new TextSignOptions("Text signature") { VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left }; BarcodeSignOptions barcodeOptions = new BarcodeSignOptions("123456") { EncodeType = BarcodeTypes.Code128, Left = 0, Top = 150, Height = 50, Width = 200 }; QrCodeSignOptions qrcodeOptions = new QrCodeSignOptions("JohnSmith") { EncodeType = QrCodeTypes.QR, Left = 0, Top = 220 }; DigitalSignOptions digitalOptions = new DigitalSignOptions(Constants.CertificatePfx) { ImageFilePath = Constants.ImageHandwrite, Left = 20, Top = 400, Height = 100, Width = 100, Password = "******" }; ImageSignOptions imageOptions = new ImageSignOptions(Constants.ImageStamp) { Left = 20, Top = 550, Height = 100, Width = 100 }; MetadataSignOptions metaOptions = new MetadataSignOptions(); WordProcessingMetadataSignature[] metaSignatures = new WordProcessingMetadataSignature[] { new WordProcessingMetadataSignature("Author", "Mr.Scherlock Holmes"), new WordProcessingMetadataSignature("CreatedOn", DateTime.Now) }; metaOptions.Signatures.AddRange(metaSignatures); // define list of signature options List <SignOptions> listOptions = new List <SignOptions>(); listOptions.Add(textOptions); listOptions.Add(barcodeOptions); listOptions.Add(qrcodeOptions); listOptions.Add(digitalOptions); listOptions.Add(imageOptions); listOptions.Add(metaOptions); // sign document to file SignResult result = signature.Sign(outputFilePath, listOptions); Console.WriteLine($"\nSource document signed successfully with {result.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); } }