From 43d63e975daf70e5f111f1f50e0741ab4ca5b3d5 Mon Sep 17 00:00:00 2001 From: OlgunR Date: Tue, 2 Jun 2026 14:06:24 +0200 Subject: [PATCH] Enhance PDF processing and report integration Refactor `PdfResultPackageService` to improve PDF handling: - Load the main document into `PdfDocumentProcessor`. - Remove original metadata to attribute the result to `DXApp`. - Replace report file attachment with appending report pages. - Use a temporary processor to determine report page count. - Add logging for appended pages and total page count. - Update save operation to include detailed logging. Removed the functionality to attach the report file as a PDF attachment. Improved overall usability, traceability, and document attribution. --- .../Services/PdfResultPackageService.cs | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/DXApp.TemplateKitProject/Services/PdfResultPackageService.cs b/DXApp.TemplateKitProject/Services/PdfResultPackageService.cs index 3eb5a28..b04f790 100644 --- a/DXApp.TemplateKitProject/Services/PdfResultPackageService.cs +++ b/DXApp.TemplateKitProject/Services/PdfResultPackageService.cs @@ -56,7 +56,7 @@ public class PdfResultPackageService( converter.SaveDocument(convertedStream); convertedStream.Position = 0; - // Schritt 3: Anhang einbetten + // Schritt 3: Hauptdokument laden using var processor = new PdfDocumentProcessor(); processor.LoadDocument(convertedStream); @@ -68,15 +68,23 @@ public class PdfResultPackageService( processor.Document.Subject = string.Empty; processor.Document.Keywords = string.Empty; - processor.AttachFile(new PdfFileAttachment + // Schritt 3a: Berichts-Seiten ans Ende anhängen + int reportPageCount; + using (var reportStream = new FileStream(reportPath, FileMode.Open, FileAccess.Read)) { - FileName = Path.GetFileName(reportPath), - Description = "Ergebnisbericht", - MimeType = "application/pdf", - Relationship = PdfAssociatedFileRelationship.Supplement, - CreationDate = DateTime.Now, - Data = File.ReadAllBytes(reportPath) - }); + // Temporäres Dokument öffnen um Seitenanzahl zu ermitteln + using var tempProcessor = new PdfDocumentProcessor(); + tempProcessor.LoadDocument(reportStream); + reportPageCount = tempProcessor.Document.Pages.Count; + + logger.LogDebug( + "Hänge {PageCount} Seite(n) aus Bericht an das Dokument an.", + reportPageCount); + + // Stream zurücksetzen und ans Hauptdokument anhängen + reportStream.Position = 0; + processor.AppendDocument(reportStream); + } // Schritt 4: Stempel auf Seite 1 zeichnen var firstPage = processor.Document.Pages[0]; @@ -126,10 +134,15 @@ public class PdfResultPackageService( processor.Document.Pages[0], 96, 96); } - // Schritt 4: Speichern + // Schritt 5: Speichern try { processor.SaveDocument(outputPath); + + logger.LogInformation( + "Result-PDF erstellt mit {TotalPages} Seiten (Original + {ReportPages} Berichtseiten).", + processor.Document.Pages.Count, + reportPageCount); } catch (Exception ex) {