From b82473ada27ed1740c087b0e920b91775729ecfb Mon Sep 17 00:00:00 2001 From: Elias Fierke Date: Sun, 21 Dec 2025 11:43:41 +0100 Subject: [PATCH] [chore:] PdfBuilder.cs skips empty addresses --- Tasks/PdfBuilder.cs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/Tasks/PdfBuilder.cs b/Tasks/PdfBuilder.cs index 672bc3d..9096269 100644 --- a/Tasks/PdfBuilder.cs +++ b/Tasks/PdfBuilder.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using PdfSharp; using PdfSharp.Drawing; @@ -12,7 +13,6 @@ public class PdfBuilder private const int CellsPerRow = 3; private const int CellsPerPage = 21; // 3 columns × 7 rows private readonly XFont _boldFont = new("Arial", 9, XFontStyleEx.Bold); - private readonly XFont _smallFont = new("Arial", 6, XFontStyleEx.Regular); private readonly double _cellHeight = 42.43; // mm private readonly double _cellPaddingBottom = 5; // mm @@ -33,6 +33,7 @@ public class PdfBuilder private readonly double _marginRight = 0; // mm private readonly double _marginTop = 0; // mm private readonly XFont _regularFont = new("Arial", 9, XFontStyleEx.Regular); + private readonly XFont _smallFont = new("Arial", 6, XFontStyleEx.Regular); /// /// Creates a PDF document with address stickers from an AddressSet in a 3×7 grid layout on A4 pages. @@ -50,7 +51,8 @@ public class PdfBuilder throw new ArgumentException($"AddressSet with ID {addressSetId} contains no addresses"); // Generate markdown addresses from all KasPersons in the set - var addresses = new string?[addressSet.KasPersons.Count]; + //var addresses = new string?[addressSet.KasPersons.Count]; + var addresses = new List(); // find customer (owner) to include sender_address string senderLine = null; @@ -69,10 +71,11 @@ public class PdfBuilder for (var i = 0; i < addressSet.KasPersons.Count; i++) { var addr = AddressCreator.CreateFinalMarkdownString(addressSet.KasPersons[i].refsid); + if (string.IsNullOrWhiteSpace(addr.Trim())) continue; if (!string.IsNullOrEmpty(senderLine)) - addresses[i] = senderLine + (addr ?? ""); + addresses.Add(senderLine + addr); else - addresses[i] = addr; + addresses.Add(addr); } CreateAddressLabelPdf(addresses, outputPath); @@ -96,7 +99,8 @@ public class PdfBuilder throw new ArgumentException($"AddressSet with ID {addressSetId} contains no addresses"); // Generate markdown addresses from all KasPersons in the set - var addresses = new string?[addressSet.KasPersons.Count]; + //var addresses = new string?[addressSet.KasPersons.Count]; + var addresses = new List(); // find customer (owner) to include sender_address string senderLine = null; @@ -114,10 +118,11 @@ public class PdfBuilder for (var i = 0; i < addressSet.KasPersons.Count; i++) { var addr = AddressCreator.CreateFinalMarkdownString(addressSet.KasPersons[i].refsid); + if (string.IsNullOrWhiteSpace(addr.Trim())) continue; if (!string.IsNullOrEmpty(senderLine)) - addresses[i] = senderLine + (addr ?? ""); + addresses.Add(senderLine + (addr ?? "")); else - addresses[i] = addr; + addresses.Add(addr); } CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath); @@ -128,15 +133,15 @@ public class PdfBuilder /// /// Array of addresses (from CreateFinalMarkdownString) /// Path where the PDF should be saved - public void CreateAddressLabelPdf(string?[] addresses, string outputPath) + public void CreateAddressLabelPdf(List addresses, string outputPath) { - if (addresses == null || addresses.Length == 0) + if (addresses == null || addresses.Count == 0) throw new ArgumentException("Addresses array cannot be null or empty"); var document = new PdfDocument(); var addressIndex = 0; - while (addressIndex < addresses.Length) + while (addressIndex < addresses.Count) { var page = document.AddPage(); page.Size = PageSize.A4; @@ -158,9 +163,9 @@ public class PdfBuilder /// Array of addresses /// Text for the first cell (top-left) /// Path where the PDF should be saved - public void CreateAddressLabelPdfWithPlaceholder(string?[] addresses, string placeholderText, string outputPath) + public void CreateAddressLabelPdfWithPlaceholder(List addresses, string placeholderText, string outputPath) { - if (addresses == null || addresses.Length == 0) + if (addresses == null || addresses.Count == 0) throw new ArgumentException("Addresses array cannot be null or empty"); var document = new PdfDocument(); @@ -168,7 +173,7 @@ public class PdfBuilder var addressIndex = 0; var isFirstCell = true; - while (addressIndex < addresses.Length || isFirstCell) + while (addressIndex < addresses.Count || isFirstCell) { var page = document.AddPage(); page.Size = PageSize.A4; @@ -182,7 +187,7 @@ public class PdfBuilder document.Save(outputPath); } - private void DrawPage(XGraphics gfx, string?[] addresses, ref int addressIndex) + private void DrawPage(XGraphics gfx, List addresses, ref int addressIndex) { var cellCount = 0; @@ -190,7 +195,7 @@ public class PdfBuilder { for (var col = 0; col < 3; col++) { - if (addressIndex >= addresses.Length) break; + if (addressIndex >= addresses.Count) break; var x = MmToPoints(_marginLeft + col * _cellWidth); var y = MmToPoints(_marginTop + row * _cellHeight); @@ -200,11 +205,11 @@ public class PdfBuilder cellCount++; } - if (addressIndex >= addresses.Length) break; + if (addressIndex >= addresses.Count) break; } } - private void DrawPageWithPlaceholder(XGraphics gfx, string?[] addresses, ref int addressIndex, + private void DrawPageWithPlaceholder(XGraphics gfx, List addresses, ref int addressIndex, ref bool isFirstCell, string placeholderText) { var cellCount = 0; @@ -221,7 +226,7 @@ public class PdfBuilder DrawCell(gfx, x, y, placeholderText); isFirstCell = false; } - else if (addressIndex < addresses.Length) + else if (addressIndex < addresses.Count) { DrawCell(gfx, x, y, addresses[addressIndex]); addressIndex++;