using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
namespace Logof_Client;
public class PdfBuilder
{
private readonly PdfExportSettings _settings;
private readonly XFont _boldFont;
private readonly XFont _regularFont;
private readonly XFont _smallFont;
public PdfBuilder(PdfExportSettings? settings = null)
{
try
{
EnsureFontResolverRegistered();
_settings = settings ?? new PdfExportSettings();
// Select first font from build output fonts folder (AppContext.BaseDirectory/fonts)
var chosenFamily = "Arial";
try
{
if (Directory.Exists(Global._instance.font_path))
{
var first = Directory.EnumerateFiles(Global._instance.font_path, "*.ttf").FirstOrDefault();
if (!string.IsNullOrEmpty(first))
chosenFamily = StripStyleSuffix(Path.GetFileNameWithoutExtension(first)) ?? chosenFamily;
}
}
catch (Exception ex)
{
Logger.Log($"Error while searching for fonts: {ex.Message}",Logger.LogType.Error);
chosenFamily = "Arial";
}
_boldFont = new XFont(chosenFamily, _settings.fontSize, XFontStyleEx.Bold);
_regularFont = new XFont(chosenFamily, _settings.fontSize, XFontStyleEx.Regular);
_smallFont = new XFont(chosenFamily, _settings.smallFontSize, XFontStyleEx.Regular);
}
catch (Exception ex)
{
Logger.Log($"Error while font resolving: {ex.Message}",Logger.LogType.Error);
}
}
private static void EnsureFontResolverRegistered()
{
try
{
if (GlobalFontSettings.FontResolver != null) return;
//var fontsDir = Path.Combine(AppContext.BaseDirectory, "fonts");
GlobalFontSettings.FontResolver = new StableFontResolver(Global._instance.font_path);
}
catch (Exception ex)
{
Logger.Log($"Error while ensuring font resolver register state: {ex.Message}",Logger.LogType.Error);
}
}
private static string StripStyleSuffix(string name)
{
try
{
if (string.IsNullOrEmpty(name)) return name;
var idx = name.IndexOf('-');
if (idx < 0) idx = name.IndexOf('_');
if (idx > 0)
return name.Substring(0, idx);
return name;
}
catch (Exception ex)
{
Logger.Log($"Error while stripping style suffix: {ex.Message}",Logger.LogType.Error);
}
return null;
}
///
/// Creates a PDF document with address stickers from an AddressSet with a placeholder in the first cell.
///
/// The ID of the AddressSet to use
/// Text for the first cell (top-left)
/// Path where the PDF should be saved
public void CreateAddressLabelPdfFromAddressSetWithPlaceholder(int addressSetId, string placeholderText,
string outputPath)
{
try
{
// Find the AddressSet by ID
var addressSet = Settings._instance.addressSets.GetAddressSetByID(addressSetId);
if (addressSet == null)
throw new ArgumentException($"AddressSet with ID {addressSetId} not found");
if (addressSet.KasPersons == null || addressSet.KasPersons.Count == 0)
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 List();
// find customer (owner) to include sender_address
string senderLine = null;
try
{
var owner = Settings._instance.customers.customers.FirstOrDefault(c => c.ID == addressSet.owner_id);
if (owner != null && !string.IsNullOrWhiteSpace(owner.sender_address))
senderLine = "" + owner.sender_address.Replace("\n", " ").Trim() + "\n";
}
catch
{
senderLine = null;
}
for (var i = 0; i < addressSet.KasPersons.Count; i++)
{
var addr = AddressCreator.CreateFinalMarkdownString(addressSet.KasPersons[i].id);
if (string.IsNullOrWhiteSpace(addr)) continue;
if (!string.IsNullOrEmpty(senderLine))
addresses.Add(senderLine + (addr ?? ""));
else
addresses.Add(addr);
}
if (addresses.Count == 0)
{
MessageBox.Show(MainWindow._instance, "Keine validen Adressen konnten generiert werden. Abbruch.", "Fehler");
return;
}
CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath);
if (_settings.exportRunningSheets)
{
ExportRunningSheets(addressSetId, outputPath);
}
CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath);
}
catch (Exception ex)
{
Logger.Log($"Error while creating address label pdf from address set with placeholder: {ex.Message}",Logger.LogType.Error);
}
}
///
/// Creates a PDF document with a single placeholder cell for other information.
///
/// Array of addresses
/// Text for the first cell (top-left)
/// Path where the PDF should be saved
public void CreateAddressLabelPdfWithPlaceholder(List addresses, string placeholderText, string outputPath)
{
try
{
if (addresses == null || addresses.Count == 0)
throw new ArgumentException("Addresses array cannot be null or empty");
var document = new PdfDocument();
var addressIndex = 0;
var isFirstCell = true;
while (addressIndex < addresses.Count || isFirstCell)
{
var page = document.AddPage();
page.Size = PageSize.A4;
using (var gfx = XGraphics.FromPdfPage(page))
{
DrawPageWithPlaceholder(gfx, addresses, ref addressIndex, ref isFirstCell, placeholderText);
}
}
document.Save(outputPath);
}
catch (Exception ex)
{
Logger.Log($"Error while creating address label pdf with placeholder: {ex.Message}",Logger.LogType.Error);
}
}
private void DrawPage(XGraphics gfx, List addresses, ref int addressIndex)
{
for (var row = 0; row < _settings.rowsPerPage; row++)
{
for (var col = 0; col < _settings.columnsPerPage; col++)
{
if (addressIndex >= addresses.Count) break;
var x = MmToPoints(_settings.pageMarginLeftMm + col * GetCellWidthMm());
var y = MmToPoints(_settings.pageMarginTopMm + row * GetCellHeightMm());
DrawCell(gfx, x, y, addresses[addressIndex]);
addressIndex++;
}
if (addressIndex >= addresses.Count) break;
}
}
private void DrawPageWithPlaceholder(XGraphics gfx, List addresses, ref int addressIndex,
ref bool isFirstCell, string placeholderText)
{
try
{
for (var row = 0; row < _settings.rowsPerPage; row++)
for (var col = 0; col < _settings.columnsPerPage; col++)
{
var x = MmToPoints(_settings.pageMarginLeftMm + col * GetCellWidthMm());
var y = MmToPoints(_settings.pageMarginTopMm + row * GetCellHeightMm());
// First cell: placeholder
if (isFirstCell)
{
DrawCell(gfx, x, y, placeholderText);
isFirstCell = false;
}
else if (addressIndex < addresses.Count)
{
DrawCell(gfx, x, y, addresses[addressIndex]);
addressIndex++;
}
else
{
DrawEmptyCell(gfx, x, y);
}
}
}
catch (Exception ex)
{
Logger.Log($"Error while drawing page with placholder: {ex.Message}",Logger.LogType.Error);
}
}
private void DrawCell(XGraphics gfx, double x, double y, string? address)
{
try
{
var cellWidthPoints = MmToPoints(GetCellWidthMm());
var cellHeightPoints = MmToPoints(GetCellHeightMm());
// Draw cell border
var rect = new XRect(x, y, cellWidthPoints, cellHeightPoints);
gfx.DrawRectangle(XPens.Black, rect);
// Draw address content if available
if (!string.IsNullOrEmpty(address)) DrawMarkdownText(gfx, address, x, y, cellWidthPoints, cellHeightPoints);
}
catch (Exception ex)
{
Logger.Log($"Error while drawing cell: {ex.Message}",Logger.LogType.Error);
}
}
private void DrawEmptyCell(XGraphics gfx, double x, double y)
{
try
{
var cellWidthPoints = MmToPoints(GetCellWidthMm());
var cellHeightPoints = MmToPoints(GetCellHeightMm());
var rect = new XRect(x, y, cellWidthPoints, cellHeightPoints);
gfx.DrawRectangle(XPens.Black, rect);
}
catch (Exception ex)
{
Logger.Log($"Error while drawing empty cell: {ex.Message}",Logger.LogType.Error);
}
}
private void DrawMarkdownText(XGraphics gfx, string text, double x, double y, double cellWidth, double cellHeight)
{
try
{
var paddingLeftPoints = MmToPoints(_settings.cellPaddingLeftMm);
var paddingRightPoints = MmToPoints(_settings.cellPaddingRightMm);
var paddingTopPoints = MmToPoints(_settings.cellPaddingTopMm);
var paddingBottomPoints = MmToPoints(_settings.cellPaddingBottomMm);
var maxWidth = Math.Max(0, cellWidth - paddingLeftPoints - paddingRightPoints);
// Split text by newlines and remove empty trailing lines
var rawLines = text.Split(new[] { "\n" }, StringSplitOptions.None);
var lines = rawLines.Where(l => l != null).ToArray();
// Use a conservative line height in points
var lineHeight = _regularFont.Size * 1.2;
// Calculate total height of the text block
var totalHeight = lines.Length * lineHeight;
// Start drawing from the top of the cell (align addresses to top)
var startY = y + paddingTopPoints;
var currentY = startY;
foreach (var line in lines)
{
// Stop if we've reached the top boundary
if (currentY + lineHeight > y + cellHeight - paddingBottomPoints + 0.001)
break;
// Parse and draw the line with markdown support
DrawLineWithMarkdown(gfx, line, x + paddingLeftPoints, currentY, maxWidth);
currentY += lineHeight;
}
}
catch (Exception ex)
{
Logger.Log($"Error while drawing markdown text: {ex.Message}",Logger.LogType.Error);
}
}
private void DrawLineWithMarkdown(XGraphics gfx, string line, double x, double y, double maxWidth)
{
try
{
if (string.IsNullOrWhiteSpace(line)) return;
var currentX = x;
var i = 0;
while (i < line.Length)
{
if (currentX - x >= maxWidth)
break;
var remainingWidth = maxWidth - (currentX - x);
// Check for small-font tag ...
if (i <= line.Length - 7 && line.Substring(i, 7) == "")
{
var endTag = line.IndexOf("", i + 7, StringComparison.Ordinal);
if (endTag != -1)
{
var inner = line.Substring(i + 7, endTag - (i + 7));
if (!string.IsNullOrEmpty(inner))
{
var measuredSmall = gfx.MeasureString(inner, _smallFont);
if (measuredSmall.Width > remainingWidth)
{
inner = TruncateTextToWidth(gfx, inner, _smallFont, remainingWidth);
measuredSmall = gfx.MeasureString(inner, _smallFont);
}
gfx.DrawString(inner, _smallFont, XBrushes.Black,
new XRect(currentX, y, remainingWidth, _smallFont.Size * 1.2),
XStringFormats.TopLeft);
currentX += measuredSmall.Width;
}
i = endTag + 8; // move past
continue;
}
}
// Check for bold marker (**)
if (i < line.Length - 1 && line[i] == '*' && line[i + 1] == '*')
{
// Find closing **
var endIndex = line.IndexOf("**", i + 2, StringComparison.Ordinal);
if (endIndex != -1)
{
var boldText = line.Substring(i + 2, endIndex - (i + 2));
var measured = gfx.MeasureString(boldText, _boldFont);
if (measured.Width > remainingWidth)
{
boldText = TruncateTextToWidth(gfx, boldText, _boldFont, remainingWidth);
measured = gfx.MeasureString(boldText, _boldFont);
}
// Draw bold text and measure width accurately
gfx.DrawString(boldText, _boldFont, XBrushes.Black,
new XRect(currentX, y, remainingWidth, _boldFont.Size * 1.2),
XStringFormats.TopLeft);
currentX += measured.Width;
i = endIndex + 2;
continue;
}
}
// Regular text until next ** or end of line
var nextBoldIndex = line.IndexOf("**", i, StringComparison.Ordinal);
var textEnd = nextBoldIndex == -1 ? line.Length : nextBoldIndex;
var regularText = line.Substring(i, textEnd - i);
if (!string.IsNullOrEmpty(regularText))
{
var measured = gfx.MeasureString(regularText, _regularFont);
if (measured.Width > remainingWidth)
{
regularText = TruncateTextToWidth(gfx, regularText, _regularFont, remainingWidth);
measured = gfx.MeasureString(regularText, _regularFont);
}
gfx.DrawString(regularText, _regularFont, XBrushes.Black,
new XRect(currentX, y, remainingWidth, _regularFont.Size * 1.2), XStringFormats.TopLeft);
currentX += measured.Width;
}
i = textEnd;
}
}
catch (Exception ex)
{
Logger.Log($"Error while drawing markdown line: {ex.Message}",Logger.LogType.Error);
}
}
private string TruncateTextToWidth(XGraphics gfx, string text, XFont font, double maxWidth)
{
try
{
if (string.IsNullOrEmpty(text))
return text;
for (var len = text.Length; len > 0; len--)
{
var truncated = text.Substring(0, len);
var measured = gfx.MeasureString(truncated, font);
if (measured.Width <= maxWidth)
return truncated;
}
return string.Empty;
}
catch (Exception ex)
{
Logger.Log($"Error while truncating text to width: {ex.Message}",Logger.LogType.Error);
}
return null;
}
///
/// Converts millimeters to points (1 mm = 2.834645669 points)
///
private double MmToPoints(double mm)
{
return mm * 2.834645669;
}
private double GetCellWidthMm()
{
var availableWidthMm = 210d - _settings.pageMarginLeftMm - _settings.pageMarginRightMm;
return availableWidthMm / _settings.columnsPerPage;
}
private double GetCellHeightMm()
{
var availableHeightMm = 297d - _settings.pageMarginTopMm - _settings.pageMarginBottomMm;
return availableHeightMm / _settings.rowsPerPage;
}
// Configuration methods to allow customization
///
/// Sets the cell dimensions in millimeters
///
public void SetCellDimensions(double width, double height)
{
if (width <= 0 || height <= 0)
throw new ArgumentException("Cell dimensions must be positive");
}
///
/// Sets the page margins in millimeters
///
public void SetMargins(double left, double top, double right, double bottom)
{
if (left < 0 || top < 0 || right < 0 || bottom < 0)
throw new ArgumentException("Margins cannot be negative");
}
public void ExportRunningSheets(int setID, string path)
{
string international_path = path;
if (path.EndsWith(".pdf"))
{
path = path.Substring(0, path.Length - 4);
international_path = path;
path = path + "-Laufzettel.pdf";
international_path = international_path + "-Laufzettel-International.pdf";
}
else
{
path = path + "-Laufzettel.pdf";
international_path = international_path + "-Laufzettel-International.pdf";
}
CreateGermanyRunningSheets(setID, path);
CreateInternationalRunningSheets(setID, international_path);
}
public void CreateGermanyRunningSheets(int setID, string path)
{
KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID);
var document = new PdfDocument();
document.Info.Title = $"Laufzettel für {list.Name}";
document.Info.Subject = "powered by logofclient";
document.Info.Author = "logofclient";
var grouped_nums = GroupAddresses(setID).ToList();
// Zwei Tabellen pro A4-Seite
int sheets = (grouped_nums.Count + 1) / 2;
// Layout
double marginX = 20;
double marginY = 20;
double gapY = 12;
for (int pageIndex = 0; pageIndex < sheets; pageIndex++)
{
var page = document.AddPage();
page.Size = PageSize.A4;
var gfx = XGraphics.FromPdfPage(page);
double pageW = page.Width.Point;
double pageH = page.Height.Point;
double usableW = pageW - 2 * marginX;
double usableH = pageH - 2 * marginY;
double tableH = (usableH - gapY) / 2.0;
double top1 = marginY;
double top2 = marginY + tableH + gapY;
var fontLabel = new XFont("Arial", 6, XFontStyleEx.Bold);
var fontText = new XFont("Arial", 6, XFontStyleEx.Regular);
var fontBig = new XFont("Arial", 30, XFontStyleEx.Bold);
int firstIndex = pageIndex * 2;
DrawGermanyRunningSheet(
gfx,
marginX,
top1,
usableW,
tableH,
list,
grouped_nums[firstIndex],
grouped_nums,
fontLabel,
fontText,
fontBig
);
if (firstIndex + 1 < grouped_nums.Count)
{
DrawGermanyRunningSheet(
gfx,
marginX,
top2,
usableW,
tableH,
list,
grouped_nums[firstIndex + 1],
grouped_nums,
fontLabel,
fontText,
fontBig
);
}
}
document.Save(path);
}
private void DrawGermanyRunningSheet(
XGraphics gfx,
double x,
double y,
double w,
double h,
KasAddressList list,
dynamic result,
List<(int,string,string,string,int)> grouped_nums,
XFont fontLabel,
XFont fontText,
XFont fontBig)
{
double line = 1.0;
string sender = Customer.GetCustomerByID(list.owner_id)?.sender_address ?? "[Absender]";
string customerName = Customer.GetCustomerByID(list.owner_id)?.name ?? "[Kunde]";
string start = result.Item3?.ToString() ?? "[Start]";
string end = result.Item4?.ToString() ?? "[Ende]";
string amount = result.Item5?.ToString() ?? "[Anzahl]";
string groupNo = result.Item2?.ToString() ?? "[PLZ]";
string fraction = result.Item1?.ToString() ?? "[Fraktion]";
int total_frac = 0;
foreach (var item in grouped_nums)
{
if (item.Item2 == result.Item2) total_frac++;
}
// Outer border
gfx.DrawRectangle(XPens.Black, x, y, w, h);
// Row heights
double r1 = h * 0.1;
double r2 = h * 0.1;
double r3 = h * 0.54;
double r4 = h * 0.30;
// Main horizontal lines
gfx.DrawLine(XPens.Black, x, y + r1, x + w, y + r1);
gfx.DrawLine(XPens.Black, x, y + r1 + r2, x + w, y + r1 + r2);
gfx.DrawLine(XPens.Black, x, y + r1 + r2 + r3, x + w, y + r1 + r2 + r3);
// Top row columns
double c1 = w * 0.39;
double c2 = w * 0.20;
double c3 = w * 0.26;
double c4 = w * 0.15;
gfx.DrawLine(XPens.Black, x + c1, y, x + c1, y + r1);
gfx.DrawLine(XPens.Black, x + c1 + c2, y, x + c1 + c2, y + r1);
gfx.DrawLine(XPens.Black, x + c1 + c2 + c3, y, x + c1 + c2 + c3, y + r1);
// Second row columns
gfx.DrawLine(XPens.Black, x + c1, y + r1, x + c1, y + r1 + r2);
gfx.DrawLine(XPens.Black, x + c1 + c2, y + r1, x + c1 + c2, y + r1 + r2);
gfx.DrawLine(XPens.Black, x + c1 + c2 + c3, y + r1, x + c1 + c2 + c3, y + r1 + r2);
// Middle large area split
double midSplit = x + w * 0.55;
gfx.DrawLine(XPens.Black, midSplit, y + r1 + r2, midSplit, y + r1 + r2 + r3);
// Bottom section split
double leftBottomW = w * 0.42;
gfx.DrawLine(XPens.Black, x + leftBottomW, y + r1 + r2 + r3, x + leftBottomW, y + h);
// Bottom left rows
double blY1 = y + r1 + r2 + r3 + (r4 * 0.14);
double blY2 = y + r1 + r2 + r3 + (r4 * 0.28);
double blY3 = y + r1 + r2 + r3 + (r4 * 0.42);
double blY4 = y + r1 + r2 + r3 + (r4 * 0.56);
double blY5 = y + r1 + r2 + r3 + (r4 * 0.70);
gfx.DrawLine(XPens.Black, x, blY1, x + leftBottomW, blY1);
gfx.DrawLine(XPens.Black, x, blY2, x + leftBottomW, blY2);
gfx.DrawLine(XPens.Black, x, blY3, x + leftBottomW, blY3);
gfx.DrawLine(XPens.Black, x, blY4, x + leftBottomW, blY4);
gfx.DrawLine(XPens.Black, x, blY5, x + leftBottomW, blY5);
// Labels top row
gfx.DrawString("Absender:", fontLabel, XBrushes.Black, new XRect(x + 5, y + 4, c1 - 10, 14), XStringFormats.TopLeft);
gfx.DrawString("Kunden-Nr. Absender:", fontLabel, XBrushes.Black, new XRect(x + c1 + 5, y + 4, c2 - 10, 14), XStringFormats.TopLeft);
gfx.DrawString("ZKZ/Titel:", fontLabel, XBrushes.Black, new XRect(x + c1 + c2 + 5, y + 4, c3 - 10, 14), XStringFormats.TopLeft);
gfx.DrawString("Anzahl Sendungen:", fontLabel, XBrushes.Black, new XRect(x + c1 + c2 + c3 + 5, y + 4, c4 - 10, 14), XStringFormats.TopLeft);
// Values top row
gfx.DrawString(sender, fontText, XBrushes.Black, new XRect(x + 5, y + 20, c1 - 10, r1 - 22), XStringFormats.TopLeft);
gfx.DrawString("[Kunden-Nr. Absender]", fontText, XBrushes.Black, new XRect(x + c1 + 5, y + 20, c2 - 10, r1 - 22), XStringFormats.TopLeft);
gfx.DrawString("[ZKZ/Titel]", fontText, XBrushes.Black, new XRect(x + c1 + c2 + 5, y + 20, c3 - 10, r1 - 22), XStringFormats.TopLeft);
gfx.DrawString(amount, fontText, XBrushes.Black, new XRect(x + c1 + c2 + c3 + 5, y + 20, c4 - 10, r1 - 22), XStringFormats.TopLeft);
// Second row labels
gfx.DrawString("Einlieferer:", fontLabel, XBrushes.Black, new XRect(x + 5, y + r1 + 4, c1 - 10, 14), XStringFormats.TopLeft);
gfx.DrawString("Kunden-Nr. Einlieferer:", fontLabel, XBrushes.Black, new XRect(x + c1 + 5, y + r1 + 4, c2 - 10, 14), XStringFormats.TopLeft);
gfx.DrawString("Interne Vermerke:", fontLabel, XBrushes.Black, new XRect(x + c1 + c2 + 5, y + r1 + 4, c3 - 10, 14), XStringFormats.TopLeft);
gfx.DrawString("Laufzeit", fontLabel, XBrushes.Black, new XRect(x + c1 + c2 + c3 + 5, y + r1 + 4, c4 - 10, 14), XStringFormats.TopLeft);
// Second row values
gfx.DrawString(customerName, fontText, XBrushes.Black, new XRect(x + 5, y + r1 + 20, c1 - 10, r2 - 22), XStringFormats.TopLeft);
gfx.DrawString("[Kunden-Nr. Einlieferer]", fontText, XBrushes.Black, new XRect(x + c1 + 5, y + r1 + 20, c2 - 10, r2 - 22), XStringFormats.TopLeft);
gfx.DrawString("[Interne Vermerke]", fontText, XBrushes.Black, new XRect(x + c1 + c2 + 5, y + r1 + 20, c3 - 10, r2 - 22), XStringFormats.TopLeft);
gfx.DrawString("[Laufzeit]", fontText, XBrushes.Black, new XRect(x + c1 + c2 + c3 + 5, y + r1 + 20, c4 - 10, r2 - 22), XStringFormats.TopLeft);
// Middle area
gfx.DrawString(groupNo, fontBig, XBrushes.Black,
new XRect(x + 5, y + r1 + r2 + 5, w * 0.50 - 10, r3 - 10),
XStringFormats.Center);
gfx.DrawString($"Fraktion {fraction}/{total_frac}", fontText, XBrushes.Black,
new XRect(x + 5, y + r1 + r2 + r3 - 18, w * 0.50 - 10, 14),
XStringFormats.CenterLeft);
gfx.DrawString("Bereich für postalische Zwecke:", fontLabel, XBrushes.Black,
new XRect(midSplit + 5, y + r1 + r2 + 4, w - (midSplit - x) - 10, 14),
XStringFormats.TopLeft);
// Bottom left labels
// Bottom left labels
gfx.DrawString("Einlieferungsdatum:", fontLabel, XBrushes.Black,
new XRect(x + 5, y + r1 + r2 + r3 + 4, leftBottomW - 10, 14),
XStringFormats.TopLeft);
gfx.DrawString("AM-Auftragsnummer:", fontLabel, XBrushes.Black,
new XRect(x + 5, blY1 + 4, leftBottomW - 10, 14),
XStringFormats.TopLeft);
gfx.DrawString("Bundgewicht:", fontLabel, XBrushes.Black,
new XRect(x + 5, blY2 + 4, leftBottomW - 10, 14),
XStringFormats.TopLeft);
gfx.DrawString("Paletten-Nr.:", fontLabel, XBrushes.Black,
new XRect(x + 5, blY3 + 4, leftBottomW - 10, 14),
XStringFormats.TopLeft);
gfx.DrawString("Bund-Nr./Bunde auf Palette:", fontLabel, XBrushes.Black,
new XRect(x + 5, blY4 + 4, leftBottomW - 10, 14),
XStringFormats.TopLeft);
gfx.DrawString("Bund-Nr. von Gesamtanzahl:", fontLabel, XBrushes.Black,
new XRect(x + 5, blY5 + 4, leftBottomW - 10, 14),
XStringFormats.TopLeft);
// Bottom placeholders rechts daneben, gleiche Zeile
double valueX = x + leftBottomW * 0.55;
double valueW = leftBottomW - (valueX - x) - 5;
gfx.DrawString("[Einlieferungsdatum]", fontText, XBrushes.Black,
new XRect(valueX, y + r1 + r2 + r3 + 4, valueW, 14),
XStringFormats.TopRight);
gfx.DrawString("[AM-Auftragsnummer]", fontText, XBrushes.Black,
new XRect(valueX, blY1 + 4, valueW, 14),
XStringFormats.TopRight);
gfx.DrawString("[Bundgewicht]", fontText, XBrushes.Black,
new XRect(valueX, blY2 + 4, valueW, 14),
XStringFormats.TopRight);
gfx.DrawString("[Paletten-Nr.]", fontText, XBrushes.Black,
new XRect(valueX, blY3 + 4, valueW, 14),
XStringFormats.TopRight);
gfx.DrawString("[Bund-Nr./Bunde auf Palette]", fontText, XBrushes.Black,
new XRect(valueX, blY4 + 4, valueW, 14),
XStringFormats.TopRight);
gfx.DrawString("[Bund-Nr. von Gesamtanzahl]", fontText, XBrushes.Black,
new XRect(valueX, blY5 + 4, valueW, 14),
XStringFormats.TopRight);
// Right bottom postal area label
gfx.DrawString("Bereich für postalische Zwecke:", fontLabel, XBrushes.Black,
new XRect(x + leftBottomW + 5, y + r1 + r2 + r3 + 4, w - leftBottomW - 10, 14),
XStringFormats.TopLeft);
}
public void CreateInternationalRunningSheets(int setID, string path)
{
KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID);
var document = new PdfDocument();
document.Info.Title = $"Laufzettel für {list.Name}";
document.Info.Subject = "powered by logofclient";
document.Info.Author = "logofclient";
int margin = 50;
var grouped_nums = GroupAddressesInternational(setID);
foreach (var result in grouped_nums)
{
var page = document.AddPage();
page.Size = PageSize.A4;
var gfx = XGraphics.FromPdfPage(page);
var width = page.Width.Point-margin;
var height = page.Height.Point-margin;
gfx.DrawLine(XPens.Black, margin, margin, margin, height);
gfx.DrawLine(XPens.Black, margin, margin, width, margin);
gfx.DrawLine(XPens.Black, width, margin, width, height);
gfx.DrawLine(XPens.Black, margin, height, width, height);
var boldfont = new XFont("Cantarell", 11, XFontStyleEx.Bold);
var font = new XFont("Cantarell", 11, XFontStyleEx.Regular);
var bigboldfont = new XFont("Cantarell", 35, XFontStyleEx.Bold);
// Versandinfo
gfx.DrawString($"Versand {list.Name}", boldfont, XBrushes.Black,
new XRect(margin+5, margin, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"Start: ", font, XBrushes.Black,
new XRect(margin+5, margin+25, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"{result.Item3}", font, XBrushes.Black,
new XRect(margin+75, margin+25, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"Ende: ", font, XBrushes.Black,
new XRect(margin+5, margin+40, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"{result.Item4}", font, XBrushes.Black,
new XRect(margin+75, margin+40, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"Kunde: ", font, XBrushes.Black,
new XRect(margin+5, margin+55, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"{Customer.GetCustomerByID(list.owner_id).name}", font, XBrushes.Black,
new XRect(margin+75, margin+55, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"Absender: ", font, XBrushes.Black,
new XRect(margin+5, margin+70, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"{Customer.GetCustomerByID(list.owner_id).sender_address}", font, XBrushes.Black,
new XRect(margin+75, margin+70, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"Anzahl: ", font, XBrushes.Black,
new XRect(margin+5, margin+85, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"{result.Item5}", font, XBrushes.Black,
new XRect(margin+75, margin+85, width-margin, 25), XStringFormats.CenterLeft);
// logofclient ad
gfx.DrawString($"powered by logofclient", font, XBrushes.Black,
new XRect(margin+5, height-55, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"(c) 2026 MyPapertown", font, XBrushes.Black,
new XRect(margin+5, height-40, width-margin, 25), XStringFormats.CenterLeft);
gfx.DrawString($"mypapercloud.de/logof", font, XBrushes.Black,
new XRect(margin+5, height-25, width-margin, 25), XStringFormats.CenterLeft);
int total_frac = 0;
foreach (var item in grouped_nums)
{
if (item.Item2 == result.Item2) total_frac++;
}
// group number
gfx.DrawString($"{result.Item2}", bigboldfont, XBrushes.Black,
new XRect(margin, margin, width-margin, (height-margin)/2), XStringFormats.Center);
gfx.DrawString($"Fraktion {result.Item1}/{total_frac}", font, XBrushes.Black,
new XRect(margin, margin, width-margin, (height-margin)/2 + 50), XStringFormats.Center);
}
if (document.PageCount > 0)
{
document.Save(path);
}
}
///
/// Calculates address groups to summarize for the single pages of the running sheets.
///
///
/// List of quadruples consisting of a number and the starting of the plz, as well as first, last plz and total amount of addresses
public List<(int, string, string, string, int)> GroupAddresses(int setID)
{
int grpcount = Settings._instance.pdfExport.rsNumGrouped; // Amount of addresses per group
int stpoint = Settings._instance.pdfExport.rsPlzStartpoint; // group starting point (first n characters of the plz)
KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID);
if (list == null)
throw new Exception("AddressSet nicht gefunden");
List<(int, string, string, string, int)> output = new();
List> sorted_list = list.KasPersons
.Where(x => !string.IsNullOrEmpty(x?.used_plz) &&
x.used_plz.Length >= stpoint && x.IsGermany())
.OrderBy(x => x.used_plz)
.GroupBy(x => x.used_plz.Substring(0, stpoint))
.ToList();
foreach (var group in sorted_list)
{
string start = group.Key;
int fraktion = 0;
for (int count = 0; count < group.Count(); count += grpcount)
{
fraktion++;
int currentGroupSize = Math.Min(grpcount, group.Count() - count);
string first = group.ElementAt(count).used_plz;
string last = group.ElementAt(count + currentGroupSize - 1).used_plz;
output.Add((fraktion, start, first, last, currentGroupSize));
}
}
return output;
}
public List<(int, string, string, string, int)> GroupAddressesInternational(int setID)
{
int grpcount = Settings._instance.pdfExport.rsNumGrouped; // Amount of addresses per group
KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID);
if (list == null)
throw new Exception("AddressSet nicht gefunden");
List<(int, string, string, string, int)> output = new();
List> sorted_list = list.KasPersons
.Where(x => !string.IsNullOrEmpty(x?.used_plz) && !x.IsGermany())
.OrderBy(x => x.used_plz)
.GroupBy(x => x.land)
.ToList();
foreach (var group in sorted_list)
{
string start = group.Key;
int fraktion = 0;
for (int count = 0; count < group.Count(); count += grpcount)
{
fraktion++;
int currentGroupSize = Math.Min(grpcount, group.Count() - count);
string first = group.ElementAt(count).used_plz;
string last = group.ElementAt(count + currentGroupSize - 1).used_plz;
output.Add((fraktion, start, first, last, currentGroupSize));
}
}
return output;
}
}