Add PDF signing feature with UI and backend support
Introduced a new `PdfSigningService` for digitally signing PDFs using a PFX certificate and DevExpress's `PdfDocumentSigner`. Updated `Program.cs` to register the service and added a new configuration section `PdfSigning` in `appsettings.json` for certificate management. Added a Razor Page `TestSignature.cshtml` and its page model `TestSignature.cshtml.cs` to provide a user interface for testing PDF signing. The page includes file upload, signature validation, and result display functionality. Implemented supporting classes `PdfSigningOptions` for signature customization and `SignatureInformation` for extracting and displaying signature details, including signer name, location, and certificate validity.
This commit is contained in:
90
DXApp.TemplateKitProject/Pages/TestSignature.cshtml
Normal file
90
DXApp.TemplateKitProject/Pages/TestSignature.cshtml
Normal file
@@ -0,0 +1,90 @@
|
||||
@page
|
||||
@model DXApp.TemplateKitProject.Pages.TestSignatureModel
|
||||
@{
|
||||
ViewData["Title"] = "PDF-Signatur testen";
|
||||
}
|
||||
|
||||
<div class="content-block">
|
||||
<h2><i class="dx-icon-key"></i> PDF-Signatur Test</h2>
|
||||
<p class="text-muted">Einfacher Test zum Signieren einer PDF mit dem DevExpress PdfDocumentSigner.</p>
|
||||
|
||||
<form method="post" enctype="multipart/form-data" class="mt-4">
|
||||
<div class="mb-3">
|
||||
<label for="pdfFile" class="form-label">PDF-Datei auswählen</label>
|
||||
<input type="file" class="form-control" id="pdfFile" name="PdfFile" accept=".pdf" required />
|
||||
<span asp-validation-for="PdfFile" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="dx-icon-key"></i> PDF signieren
|
||||
</button>
|
||||
</form>
|
||||
|
||||
@if (Model.Success)
|
||||
{
|
||||
<div class="alert alert-success mt-4">
|
||||
<h5><i class="dx-icon-check"></i> ? PDF erfolgreich signiert!</h5>
|
||||
<p class="mb-2">
|
||||
<strong>Original:</strong> @Model.OriginalFileName (@Model.OriginalSizeKb KB)<br />
|
||||
<strong>Signiert:</strong> @Model.SignedFileName (@Model.SignedSizeKb KB)<br />
|
||||
<strong>Gespeichert:</strong> <code>@Model.OutputPath</code>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@if (Model.SignatureInfo != null && Model.SignatureInfo.Count > 0)
|
||||
{
|
||||
<div class="alert alert-info mt-3">
|
||||
<h6><i class="dx-icon-info"></i> Signatur-Details:</h6>
|
||||
@foreach (var sig in Model.SignatureInfo)
|
||||
{
|
||||
<div class="mb-3 p-2 border-start border-3 border-primary">
|
||||
<strong>Signatur:</strong> @sig.FieldName<br />
|
||||
<strong>Unterzeichner:</strong> @sig.SignerName<br />
|
||||
<strong>Ort:</strong> @sig.Location<br />
|
||||
<strong>Grund:</strong> @sig.Reason<br />
|
||||
<strong>Datum:</strong> @sig.Date.ToString("dd.MM.yyyy HH:mm:ss")<br />
|
||||
<strong>Signatur gültig:</strong>
|
||||
@if (sig.IsSignatureValid)
|
||||
{
|
||||
<span class="badge bg-success">? Ja</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="badge bg-danger">? Nein</span>
|
||||
}
|
||||
<br />
|
||||
<strong>Zertifikat gültig:</strong>
|
||||
@if (sig.IsCertificateValid)
|
||||
{
|
||||
<span class="badge bg-success">? Ja</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="badge bg-danger">? Nein</span>
|
||||
}
|
||||
<br />
|
||||
<strong>Zertifikat gültig bis:</strong> @sig.CertificateValidUntil.ToString("dd.MM.yyyy")<br />
|
||||
<small class="text-muted">Issuer: @sig.CertificateIssuer</small>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
@if (!string.IsNullOrEmpty(Model.ErrorMessage))
|
||||
{
|
||||
<div class="alert alert-danger mt-4">
|
||||
<i class="dx-icon-warning"></i> <strong>Fehler:</strong> @Model.ErrorMessage
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="alert alert-light mt-4">
|
||||
<h6>?? Test-Schritte:</h6>
|
||||
<ol class="mb-0">
|
||||
<li>PDF-Datei auswählen (z.B. eine ZUGFeRD-Rechnung aus <code>/Invoices</code>)</li>
|
||||
<li>Auf "PDF signieren" klicken</li>
|
||||
<li>Signierte PDF wird in <code>C:\PdfResults</code> gespeichert</li>
|
||||
<li>Mit Adobe Acrobat Reader öffnen und Signatur prüfen</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
82
DXApp.TemplateKitProject/Pages/TestSignature.cshtml.cs
Normal file
82
DXApp.TemplateKitProject/Pages/TestSignature.cshtml.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using DXApp.TemplateKitProject.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
|
||||
namespace DXApp.TemplateKitProject.Pages;
|
||||
|
||||
public class TestSignatureModel(
|
||||
PdfSigningService signingService,
|
||||
IConfiguration configuration,
|
||||
ILogger<TestSignatureModel> logger) : PageModel
|
||||
{
|
||||
[BindProperty]
|
||||
public IFormFile? PdfFile { get; set; }
|
||||
|
||||
public bool Success { get; private set; }
|
||||
public string? ErrorMessage { get; private set; }
|
||||
public string? OriginalFileName { get; private set; }
|
||||
public long OriginalSizeKb { get; private set; }
|
||||
public string? SignedFileName { get; private set; }
|
||||
public long SignedSizeKb { get; private set; }
|
||||
public string? OutputPath { get; private set; }
|
||||
public List<SignatureInformation>? SignatureInfo { get; private set; }
|
||||
|
||||
public void OnGet()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
if (PdfFile is null || PdfFile.Length == 0)
|
||||
{
|
||||
ModelState.AddModelError(nameof(PdfFile), "Bitte eine PDF-Datei auswählen.");
|
||||
return Page();
|
||||
}
|
||||
|
||||
if (!PdfFile.FileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
ModelState.AddModelError(nameof(PdfFile), "Nur PDF-Dateien sind erlaubt.");
|
||||
return Page();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
OriginalFileName = PdfFile.FileName;
|
||||
|
||||
// PDF in Byte-Array laden
|
||||
using var memStream = new MemoryStream();
|
||||
await PdfFile.CopyToAsync(memStream);
|
||||
var originalBytes = memStream.ToArray();
|
||||
OriginalSizeKb = originalBytes.Length / 1024;
|
||||
|
||||
logger.LogInformation("Signiere PDF: {FileName} ({Size} KB)", OriginalFileName, OriginalSizeKb);
|
||||
|
||||
// PDF signieren
|
||||
var signedBytes = await signingService.SignPdfAsync(originalBytes);
|
||||
SignedSizeKb = signedBytes.Length / 1024;
|
||||
|
||||
// Speichern
|
||||
var outputDir = configuration["PdfResults:OutputDirectory"] ?? Path.GetTempPath();
|
||||
Directory.CreateDirectory(outputDir);
|
||||
|
||||
SignedFileName = Path.GetFileNameWithoutExtension(PdfFile.FileName) + "_signed.pdf";
|
||||
OutputPath = Path.Combine(outputDir, SignedFileName);
|
||||
|
||||
await System.IO.File.WriteAllBytesAsync(OutputPath, signedBytes);
|
||||
|
||||
logger.LogInformation("Signierte PDF gespeichert: {Path}", OutputPath);
|
||||
|
||||
// Signatur-Informationen auslesen
|
||||
SignatureInfo = await signingService.GetSignatureInfoAsync(signedBytes);
|
||||
|
||||
Success = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Fehler beim Signieren der PDF: {FileName}", PdfFile.FileName);
|
||||
ErrorMessage = ex.Message;
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user