-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomViewerOperationLogger.cs
36 lines (33 loc) · 1.92 KB
/
CustomViewerOperationLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.IO;
using DevExpress.Office.DigitalSignatures;
using DevExpress.Pdf;
using DevExpress.XtraReports.Web.ClientControls;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace SignPdfDocumentExample.Services {
public class CustomViewerOperationLogger : WebDocumentViewerOperationLogger {
readonly ILogger<CustomViewerOperationLogger> logger;
readonly IWebHostEnvironment webHostEnvironment;
public CustomViewerOperationLogger(ILogger<CustomViewerOperationLogger> logger, IHttpContextAccessor httpContextAccessor, IWebHostEnvironment webHostEnvironment) {
this.logger = logger;
this.webHostEnvironment = webHostEnvironment;
}
public override void CustomizeExportDocumentOnFinish(string documentId, string exportOperationId, ExportedDocument exportedDocument) {
if(exportedDocument.ContentType == "application/pdf") {
string certificateFile = Path.Join(webHostEnvironment.ContentRootPath, "Signatures", "certificate.pfx");
using(PdfDocumentSigner documentSigner = new PdfDocumentSigner(new MemoryStream(exportedDocument.Bytes))) {
var signer = new Pkcs7Signer(certificateFile, "123", HashAlgorithmType.SHA256);
PdfSignatureBuilder signature = new PdfSignatureBuilder(signer);
signature.ContactInfo = "John Smith";
signature.Reason = "I Agree";
MemoryStream stream = new MemoryStream();
documentSigner.SaveDocument(stream, signature);
exportedDocument.Bytes = stream.ToArray();
}
logger.LogInformation($"Exported document {documentId} signed with \"certificate.pfx\" signature certificate.");
}
}
}
}