[chore:] PdfBuilder.cs skips empty addresses
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using PdfSharp;
|
using PdfSharp;
|
||||||
using PdfSharp.Drawing;
|
using PdfSharp.Drawing;
|
||||||
@@ -12,7 +13,6 @@ public class PdfBuilder
|
|||||||
private const int CellsPerRow = 3;
|
private const int CellsPerRow = 3;
|
||||||
private const int CellsPerPage = 21; // 3 columns × 7 rows
|
private const int CellsPerPage = 21; // 3 columns × 7 rows
|
||||||
private readonly XFont _boldFont = new("Arial", 9, XFontStyleEx.Bold);
|
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 _cellHeight = 42.43; // mm
|
||||||
private readonly double _cellPaddingBottom = 5; // mm
|
private readonly double _cellPaddingBottom = 5; // mm
|
||||||
|
|
||||||
@@ -33,6 +33,7 @@ public class PdfBuilder
|
|||||||
private readonly double _marginRight = 0; // mm
|
private readonly double _marginRight = 0; // mm
|
||||||
private readonly double _marginTop = 0; // mm
|
private readonly double _marginTop = 0; // mm
|
||||||
private readonly XFont _regularFont = new("Arial", 9, XFontStyleEx.Regular);
|
private readonly XFont _regularFont = new("Arial", 9, XFontStyleEx.Regular);
|
||||||
|
private readonly XFont _smallFont = new("Arial", 6, XFontStyleEx.Regular);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a PDF document with address stickers from an AddressSet in a 3×7 grid layout on A4 pages.
|
/// 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");
|
throw new ArgumentException($"AddressSet with ID {addressSetId} contains no addresses");
|
||||||
|
|
||||||
// Generate markdown addresses from all KasPersons in the set
|
// 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<string>();
|
||||||
|
|
||||||
// find customer (owner) to include sender_address
|
// find customer (owner) to include sender_address
|
||||||
string senderLine = null;
|
string senderLine = null;
|
||||||
@@ -69,10 +71,11 @@ public class PdfBuilder
|
|||||||
for (var i = 0; i < addressSet.KasPersons.Count; i++)
|
for (var i = 0; i < addressSet.KasPersons.Count; i++)
|
||||||
{
|
{
|
||||||
var addr = AddressCreator.CreateFinalMarkdownString(addressSet.KasPersons[i].refsid);
|
var addr = AddressCreator.CreateFinalMarkdownString(addressSet.KasPersons[i].refsid);
|
||||||
|
if (string.IsNullOrWhiteSpace(addr.Trim())) continue;
|
||||||
if (!string.IsNullOrEmpty(senderLine))
|
if (!string.IsNullOrEmpty(senderLine))
|
||||||
addresses[i] = senderLine + (addr ?? "");
|
addresses.Add(senderLine + addr);
|
||||||
else
|
else
|
||||||
addresses[i] = addr;
|
addresses.Add(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
CreateAddressLabelPdf(addresses, outputPath);
|
CreateAddressLabelPdf(addresses, outputPath);
|
||||||
@@ -96,7 +99,8 @@ public class PdfBuilder
|
|||||||
throw new ArgumentException($"AddressSet with ID {addressSetId} contains no addresses");
|
throw new ArgumentException($"AddressSet with ID {addressSetId} contains no addresses");
|
||||||
|
|
||||||
// Generate markdown addresses from all KasPersons in the set
|
// 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<string>();
|
||||||
|
|
||||||
// find customer (owner) to include sender_address
|
// find customer (owner) to include sender_address
|
||||||
string senderLine = null;
|
string senderLine = null;
|
||||||
@@ -114,10 +118,11 @@ public class PdfBuilder
|
|||||||
for (var i = 0; i < addressSet.KasPersons.Count; i++)
|
for (var i = 0; i < addressSet.KasPersons.Count; i++)
|
||||||
{
|
{
|
||||||
var addr = AddressCreator.CreateFinalMarkdownString(addressSet.KasPersons[i].refsid);
|
var addr = AddressCreator.CreateFinalMarkdownString(addressSet.KasPersons[i].refsid);
|
||||||
|
if (string.IsNullOrWhiteSpace(addr.Trim())) continue;
|
||||||
if (!string.IsNullOrEmpty(senderLine))
|
if (!string.IsNullOrEmpty(senderLine))
|
||||||
addresses[i] = senderLine + (addr ?? "");
|
addresses.Add(senderLine + (addr ?? ""));
|
||||||
else
|
else
|
||||||
addresses[i] = addr;
|
addresses.Add(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath);
|
CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath);
|
||||||
@@ -128,15 +133,15 @@ public class PdfBuilder
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="addresses">Array of addresses (from CreateFinalMarkdownString)</param>
|
/// <param name="addresses">Array of addresses (from CreateFinalMarkdownString)</param>
|
||||||
/// <param name="outputPath">Path where the PDF should be saved</param>
|
/// <param name="outputPath">Path where the PDF should be saved</param>
|
||||||
public void CreateAddressLabelPdf(string?[] addresses, string outputPath)
|
public void CreateAddressLabelPdf(List<string> 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");
|
throw new ArgumentException("Addresses array cannot be null or empty");
|
||||||
|
|
||||||
var document = new PdfDocument();
|
var document = new PdfDocument();
|
||||||
|
|
||||||
var addressIndex = 0;
|
var addressIndex = 0;
|
||||||
while (addressIndex < addresses.Length)
|
while (addressIndex < addresses.Count)
|
||||||
{
|
{
|
||||||
var page = document.AddPage();
|
var page = document.AddPage();
|
||||||
page.Size = PageSize.A4;
|
page.Size = PageSize.A4;
|
||||||
@@ -158,9 +163,9 @@ public class PdfBuilder
|
|||||||
/// <param name="addresses">Array of addresses</param>
|
/// <param name="addresses">Array of addresses</param>
|
||||||
/// <param name="placeholderText">Text for the first cell (top-left)</param>
|
/// <param name="placeholderText">Text for the first cell (top-left)</param>
|
||||||
/// <param name="outputPath">Path where the PDF should be saved</param>
|
/// <param name="outputPath">Path where the PDF should be saved</param>
|
||||||
public void CreateAddressLabelPdfWithPlaceholder(string?[] addresses, string placeholderText, string outputPath)
|
public void CreateAddressLabelPdfWithPlaceholder(List<string> 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");
|
throw new ArgumentException("Addresses array cannot be null or empty");
|
||||||
|
|
||||||
var document = new PdfDocument();
|
var document = new PdfDocument();
|
||||||
@@ -168,7 +173,7 @@ public class PdfBuilder
|
|||||||
var addressIndex = 0;
|
var addressIndex = 0;
|
||||||
var isFirstCell = true;
|
var isFirstCell = true;
|
||||||
|
|
||||||
while (addressIndex < addresses.Length || isFirstCell)
|
while (addressIndex < addresses.Count || isFirstCell)
|
||||||
{
|
{
|
||||||
var page = document.AddPage();
|
var page = document.AddPage();
|
||||||
page.Size = PageSize.A4;
|
page.Size = PageSize.A4;
|
||||||
@@ -182,7 +187,7 @@ public class PdfBuilder
|
|||||||
document.Save(outputPath);
|
document.Save(outputPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawPage(XGraphics gfx, string?[] addresses, ref int addressIndex)
|
private void DrawPage(XGraphics gfx, List<string> addresses, ref int addressIndex)
|
||||||
{
|
{
|
||||||
var cellCount = 0;
|
var cellCount = 0;
|
||||||
|
|
||||||
@@ -190,7 +195,7 @@ public class PdfBuilder
|
|||||||
{
|
{
|
||||||
for (var col = 0; col < 3; col++)
|
for (var col = 0; col < 3; col++)
|
||||||
{
|
{
|
||||||
if (addressIndex >= addresses.Length) break;
|
if (addressIndex >= addresses.Count) break;
|
||||||
|
|
||||||
var x = MmToPoints(_marginLeft + col * _cellWidth);
|
var x = MmToPoints(_marginLeft + col * _cellWidth);
|
||||||
var y = MmToPoints(_marginTop + row * _cellHeight);
|
var y = MmToPoints(_marginTop + row * _cellHeight);
|
||||||
@@ -200,11 +205,11 @@ public class PdfBuilder
|
|||||||
cellCount++;
|
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<string> addresses, ref int addressIndex,
|
||||||
ref bool isFirstCell, string placeholderText)
|
ref bool isFirstCell, string placeholderText)
|
||||||
{
|
{
|
||||||
var cellCount = 0;
|
var cellCount = 0;
|
||||||
@@ -221,7 +226,7 @@ public class PdfBuilder
|
|||||||
DrawCell(gfx, x, y, placeholderText);
|
DrawCell(gfx, x, y, placeholderText);
|
||||||
isFirstCell = false;
|
isFirstCell = false;
|
||||||
}
|
}
|
||||||
else if (addressIndex < addresses.Length)
|
else if (addressIndex < addresses.Count)
|
||||||
{
|
{
|
||||||
DrawCell(gfx, x, y, addresses[addressIndex]);
|
DrawCell(gfx, x, y, addresses[addressIndex]);
|
||||||
addressIndex++;
|
addressIndex++;
|
||||||
|
|||||||
Reference in New Issue
Block a user