// -------------------- IterateSignatureDefinitions -------------------- /// <summary> /// Interates through the SignatureDefinition contained in a given /// XPS document displaying and updating the signature properties /// through a user dialog.</summary> /// <param name="signatureDialog"> /// The user dialog to use in displaying and /// updating the signature information.</param> /// <param name="xpsDocument"> /// The XPS document containing the signature information.</param> public void IterateSignatureDefinitions( SignatureDialog signatureDialog, XpsDocument xpsDocument) { IXpsFixedDocumentSequenceReader docSeq = xpsDocument.FixedDocumentSequenceReader; // For every FixedDocument in the XPS document. foreach (IXpsFixedDocumentReader doc in docSeq.FixedDocuments) { // For every SignatureDefinition in each FixedDocument. foreach (XpsSignatureDefinition signature in doc.SignatureDefinitions) { SignatureDisplayItem item = signatureDialog.AddSignatureItem(signature); // Signatures are bound to signature definitions by GUID. // If the SignatureDefintion SpotId is the same as the // SignatureId, the signature is signing that definition. // For every signature in the XPS document. foreach (XpsDigitalSignature sig in xpsDocument.Signatures) { if (sig.Id != null && sig.Id == signature.SpotId) { X509Certificate2 cert = sig.SignerCertificate as X509Certificate2; item.Signer = cert.GetNameInfo(X509NameType.SimpleName, false); item.IsSigned = true; } } // end:foreach (XpsDigitalSignature } // end:foreach (XpsSignatureDefinition } // end:foreach (IXpsFixedDocumentReader } // end:IterateSignatureDefinitions()
}// end:OutlineCommandHandler() // ---------------------- SignatureCommandHandler --------------------- /// <summary> /// Displays a dialog showing the current digital signatures and /// signature definitions. A button on the dialog allows additional /// signatures to be added.</summary> /// <param name="sender"></param> /// <param name="e"></param> private void SignatureCommandHandler( object sender, ExecutedRoutedEventArgs e) { if (_fileName != null) { SignatureDialog signatureDialog = new SignatureDialog(_xpsDocument); signatureDialog.ShowDialog(); // Close to flush the new signature definition, then reopen. _xpsDocument.Close(); _xpsDocument = new XpsDocument(_fileName, FileAccess.ReadWrite); } }// end:SignatureCommandHandler()
} // end:IterateSignatureDefinitions() // ------------------------- IterateSignatures ------------------------ /// <summary> /// Interates through signatures that are not associated with /// a SignatureDefinition.</summary> /// <param name="signatureDialog"> /// The user dialog to use in displaying and /// updating the signature information.</param> /// <param name="xpsDocument"> /// The XPS document containing the signature information.</param> public void IterateSignatures( SignatureDialog signatureDialog, XpsDocument xpsDocument) { bool found = false; IXpsFixedDocumentSequenceReader docSeq = xpsDocument.FixedDocumentSequenceReader; // For every signature in the XPS document. foreach (XpsDigitalSignature sig in xpsDocument.Signatures) { found = false; // For every FixedDocument in the XPS document. foreach (IXpsFixedDocumentReader doc in docSeq.FixedDocuments) { // For every SignatureDefinition in each FixedDocument. foreach (XpsSignatureDefinition signature in doc.SignatureDefinitions) { if (sig.Id != null && sig.Id == signature.SpotId) { found = true; break; } }//end:foreach (XpsSignatureDefinition if (found) { break; } }// end:foreach (IXpsFixedDocument if (!found) { SignatureDisplayItem item = signatureDialog.AddSignatureItem(null); X509Certificate2 cert = sig.SignerCertificate as X509Certificate2; item.Signer = cert.GetNameInfo(X509NameType.SimpleName, false); item.IsSigned = true; } } // end:foreach (XpsDigitalSignature } // end:IterateSignatures()