private void SignatureDefinitionCommandHandler(object sender, RoutedEventArgs e) { SignatureDefinition sigDefDialog = new SignatureDefinition(); if (sigDefDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { XpsSignatureDefinition signatureDefinition = new XpsSignatureDefinition(); signatureDefinition.RequestedSigner = sigDefDialog.RequestedSigner.Text; signatureDefinition.Intent = sigDefDialog.Intent.Text; signatureDefinition.SigningLocale = sigDefDialog.SigningLocale.Text; try { signatureDefinition.SignBy = DateTime.Parse(sigDefDialog.SignBy.Text); } catch (FormatException) { } signatureDefinition.SpotId = Guid.NewGuid(); IXpsFixedDocumentSequenceReader docSeq = _xpsDocument.FixedDocumentSequenceReader; //_xpsDocument is type System.Windows.Xps.Packaging.XpsDocument IXpsFixedDocumentReader doc = docSeq.FixedDocuments[0]; doc.AddSignatureDefinition(signatureDefinition); doc.CommitSignatureDefinition(); InitializeSignatureDisplay(); } }
private void SignXPS(string inputFile) { try { Licensing.ShowDemoMessage(); DigitalCertificate.LogOnEToken(this.DigitalSignatureCertificate); XpsDocument xpsDocument = new XpsDocument(inputFile, FileAccess.ReadWrite); if (!this.AllowMultipleSignatures) { XpsSignatureDefinition xpsSignatureDefinition = new XpsSignatureDefinition() { Intent = this.SigningIntent, SigningLocale = this.SigningLocation, SpotId = new Guid?(Guid.NewGuid()) }; IXpsFixedDocumentReader item = xpsDocument.FixedDocumentSequenceReader.FixedDocuments[0]; item.AddSignatureDefinition(xpsSignatureDefinition); item.CommitSignatureDefinition(); X509Certificate2 digitalSignatureCertificate = this.DigitalSignatureCertificate; Guid? spotId = xpsSignatureDefinition.SpotId; xpsDocument.SignDigitally(digitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.SignatureOrigin, spotId.Value); } else { xpsDocument.SignDigitally(this.DigitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.None); } xpsDocument.Close(); } catch { throw; } }
private void SignXPSStream(Stream inputStream) { try { Licensing.ShowDemoMessage(); DigitalCertificate.LogOnEToken(this.DigitalSignatureCertificate); using (Package package = Package.Open(inputStream, FileMode.Open, FileAccess.ReadWrite)) { string str = "memorystream://myXps.xps"; Uri uri = new Uri(str); PackageStore.AddPackage(uri, package); XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Maximum, str); if (!this.AllowMultipleSignatures) { XpsSignatureDefinition xpsSignatureDefinition = new XpsSignatureDefinition() { Intent = this.SigningIntent, SigningLocale = this.SigningLocation, SpotId = new Guid?(Guid.NewGuid()) }; IXpsFixedDocumentReader item = xpsDocument.FixedDocumentSequenceReader.FixedDocuments[0]; item.AddSignatureDefinition(xpsSignatureDefinition); item.CommitSignatureDefinition(); X509Certificate2 digitalSignatureCertificate = this.DigitalSignatureCertificate; Guid? spotId = xpsSignatureDefinition.SpotId; xpsDocument.SignDigitally(digitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.SignatureOrigin, spotId.Value); } else { xpsDocument.SignDigitally(this.DigitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.None); } PackageStore.RemovePackage(uri); xpsDocument.Close(); } } catch { throw; } }