ability to specify custom objects encryption and serialization
ability to collect and populate standard document signatures
Here are the steps to embed custom object into Metadata signature with GroupDocs.Signature:
Implement if needed custom data serialization class that implement IDataSerializer interface. By default GroupDocs.Signature uses embedded json format serialization but allows user to customize it.
Implement if needed custom data encryption class that implements IDataEncryption interface. By default GroupDocs.Signature has several encryption implementation you can use but allows user to customize it.
Implement class with properties and specify if needed class attributes (like custom serialization attribute, custom encryption attribute), specify attributes for properties like FormatAttribute to specify serialization name and display format, same as SkipSerializationAttribute to mark property of class as not serialize
Create new instance of Signature class and pass source document path as a constructor parameter.
Following topics show more details of these features
Implementation of custom data serialization
This example shows how to specify custom serialization class. This class should be implemented as Attribute and IDataSerializer interface.
/// <summary>/// Creates class that implements IDataSerializer interface/// It cam support common serialization like JSon or custom data format/// </summary>classCustomSerializationAttribute:Attribute,IDataSerializer{publicTDeserialize<T>(stringsource)whereT:class{DocumentSignatureDataresult=newDocumentSignatureData();byte[]bytes=Encoding.UTF8.GetBytes(source);using(MemoryStreamstream=newMemoryStream(bytes)){stream.Seek(0,SeekOrigin.Begin);using(BinaryReaderreader=newBinaryReader(stream)){result.ID=reader.ReadString();result.Author=reader.ReadString();result.Signed=newDateTime(reader.ReadInt64());result.DataFactor=reader.ReadDecimal();}}varconverter=TypeDescriptor.GetConverter(typeof(T));return(T)converter.ConvertFrom(result);}publicstringSerialize(objectdata){stringresult=string.Empty;DocumentSignatureDatasignatureData=dataasDocumentSignatureData;if(signatureData!=null){using(MemoryStreamstream=newMemoryStream()){using(BinaryWriterwriter=newBinaryWriter(stream)){writer.Write(signatureData.ID);writer.Write(signatureData.Author);writer.Write(signatureData.Signed.Ticks);writer.Write(signatureData.DataFactor);}result=Encoding.UTF8.GetString(stream.ToArray());}}returnresult;}}
Implementation of custom data encryption
This example shows how to specify custom serialization class. This class could be implemented also as Attribute (optional) to specify as class attribute.
// Define class that implements IDataEncryption interfaceprivateclassCustomXOREncryptionAttribute:Attribute,IDataEncryption{/// <summary>/// Gets or sets non empty key for encryption (at least one character)/// </summary>publicintKey{get;set;}/// <summary>/// Encode method to encrypt string./// </summary>/// <param name="source">Source string to encode.</param>/// <returns>Returns enccrypted string</returns>publicstringEncode(stringsource){returnProcess(source);}/// <summary>/// Decode method to obtain decrypted string./// </summary>/// <param name="source">Source string to decode.</param>/// <returns>Returns decrypted string</returns>publicstringDecode(stringsource){returnProcess(source);}/// <summary>/// Using XOR operation get encoded / decoded string/// </summary>/// <param name="source"></param>/// <returns></returns>privatestringProcess(stringsource){StringBuildersrc=newStringBuilder(source);StringBuilderdst=newStringBuilder(src.Length);charchTmp;for(intindex=0;index<src.Length;++index){chTmp=src[index];chTmp=(char)(chTmp^this.Key);dst.Append(chTmp);}returndst.ToString();}}
Definition of class
This example shows how to define custom class with serialization and encryption properties and setup Format attributes for properties.
// setup CustomSerialization Attribute to setup customer serialization(see example above)[CustomSerialization]// setup CustomXOREncryption Attribute for custom encryption (see example above)[CustomXOREncryption]privateclassDocumentSignatureData{ [Format("SignID")]publicstringID{get;set;} [Format("SAuth")]publicstringAuthor{get;set;} [Format("SDate", "yyyy-MM-dd")]publicDateTimeSigned{get;set;} [Format("SDFact", "N2")]publicdecimalDataFactor{get;set;} [SkipSerialization]publicstringComments{get;set;}}
Sign Images with custom encrypted objects and values into Metadata signature
This example shows how to add custom object into metadata signature to Image document.
using(Signaturesignature=newSignature("sample.jpg")){// setup key and passphrasestringkey="1234567890";stringsalt="1234567890";// create data encryptionIDataEncryptionencryption=newSymmetricEncryption(SymmetricAlgorithmType.Rijndael,key,salt);// setup options with text of signatureMetadataSignOptionsoptions=newMetadataSignOptions();// create custom objectDocumentSignatureDatadocumentSignature=newDocumentSignatureData(){ID=Guid.NewGuid().ToString(),Author=Environment.UserName,Signed=DateTime.Now,DataFactor=11.22M};// Specify different Metadata Signatures and add them to options signature collectionushortimgsMetadataId=41996;// Specify different Metadata Signatures and add them to options signature collection// setup Author propertyImageMetadataSignaturemdDocument=newImageMetadataSignature(imgsMetadataId++,documentSignature);// set encryptionmdDocument.DataEncryption=encryption;// setup Author propertyImageMetadataSignaturemdAuthor=newImageMetadataSignature(imgsMetadataId++,"Mr.Scherlock Holmes");// set encryptionmdAuthor.DataEncryption=encryption;// setup data of document idImageMetadataSignaturemdDocId=newImageMetadataSignature(imgsMetadataId++,Guid.NewGuid().ToString());// set encryptionmdDocId.DataEncryption=encryption;// add signatures to optionsoptions.Signatures.Add(mdDocument);options.Signatures.Add(mdAuthor);options.Signatures.Add(mdDocId);// sign document to filesignature.Sign("signed.jpg",options);}
Sign PDF with embedded object and encrypted data in Metadata signatures
This example shows how to add or update standard embedded PDF document metadata signatures.
using(Signaturesignature=newSignature("sample.pdf")){// setup options with text of signatureMetadataSignOptionsoptions=newMetadataSignOptions();// create custom objectDocumentSignatureDatadocumentSignature=newDocumentSignatureData(){ID=Guid.NewGuid().ToString(),Author=Environment.UserName,Signed=DateTime.Now,DataFactor=11.22M};IDataEncryptionencryption=newCustomXOREncryptionAttribute();// Specify different Metadata Signatures and add them to options signature collection// for this metadata encryption will be used from class attributePdfMetadataSignaturemdDocument=newPdfMetadataSignature("DocumentSignature",documentSignature);// setup Author propertyPdfMetadataSignaturemdAuthor=newPdfMetadataSignature("Author","Mr.Scherlock Holmes");// set encryptionmdAuthor.DataEncryption=encryption;// setup data of document idPdfMetadataSignaturemdDocId=newPdfMetadataSignature("DocumentId",Guid.NewGuid().ToString());// set encryptionmdDocId.DataEncryption=encryption;// add signatures to optionsoptions.Signatures.Add(mdDocument);options.Signatures.Add(mdAuthor);options.Signatures.Add(mdDocId);// sign document to filesignature.Sign("sample_signed.pdf",options);}
Note
Examples above also work for different document types. For Presentations documents only objects of PresentationMetadataSignature should be used with same properties and behavior, for Spreadsheet documents only objects of SpreadsheetMetadataSignature should be used with same properties and behavior, with WordProcessing documents the class WordProcessingMetadataSignature should be used
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: