[fix:] single address lines were too long

This commit is contained in:
Elias Fierke
2026-01-14 11:47:54 +01:00
parent 48852e4505
commit af1c3ff8cc

View File

@@ -230,6 +230,11 @@ public class PdfBuilder
while (i < line.Length) while (i < line.Length)
{ {
if (currentX - x >= maxWidth)
break;
var remainingWidth = maxWidth - (currentX - x);
// Check for small-font tag <font6> ... </font6> // Check for small-font tag <font6> ... </font6>
if (i <= line.Length - 7 && line.Substring(i, 7) == "<font6>") if (i <= line.Length - 7 && line.Substring(i, 7) == "<font6>")
{ {
@@ -239,10 +244,17 @@ public class PdfBuilder
var inner = line.Substring(i + 7, endTag - (i + 7)); var inner = line.Substring(i + 7, endTag - (i + 7));
if (!string.IsNullOrEmpty(inner)) if (!string.IsNullOrEmpty(inner))
{ {
gfx.DrawString(inner, _smallFont, XBrushes.Black,
new XRect(currentX, y, maxWidth - (currentX - x), _smallFont.Size * 1.2),
XStringFormats.TopLeft);
var measuredSmall = gfx.MeasureString(inner, _smallFont); 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; currentX += measuredSmall.Width;
} }
@@ -259,11 +271,18 @@ public class PdfBuilder
if (endIndex != -1) if (endIndex != -1)
{ {
var boldText = line.Substring(i + 2, endIndex - (i + 2)); 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 // Draw bold text and measure width accurately
gfx.DrawString(boldText, _boldFont, XBrushes.Black, gfx.DrawString(boldText, _boldFont, XBrushes.Black,
new XRect(currentX, y, maxWidth - (currentX - x), _boldFont.Size * 1.2), new XRect(currentX, y, remainingWidth, _boldFont.Size * 1.2),
XStringFormats.TopLeft); XStringFormats.TopLeft);
var measured = gfx.MeasureString(boldText, _boldFont);
currentX += measured.Width; currentX += measured.Width;
i = endIndex + 2; i = endIndex + 2;
continue; continue;
@@ -277,9 +296,16 @@ public class PdfBuilder
if (!string.IsNullOrEmpty(regularText)) if (!string.IsNullOrEmpty(regularText))
{ {
gfx.DrawString(regularText, _regularFont, XBrushes.Black,
new XRect(currentX, y, maxWidth - (currentX - x), _regularFont.Size * 1.2), XStringFormats.TopLeft);
var measured = gfx.MeasureString(regularText, _regularFont); 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; currentX += measured.Width;
} }
@@ -287,6 +313,22 @@ public class PdfBuilder
} }
} }
private string TruncateTextToWidth(XGraphics gfx, string text, XFont font, double maxWidth)
{
if (string.IsNullOrEmpty(text))
return text;
for (int 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;
}
/// <summary> /// <summary>
/// Converts millimeters to points (1 mm = 2.834645669 points) /// Converts millimeters to points (1 mm = 2.834645669 points)
/// </summary> /// </summary>