diff --git a/Tasks/PdfBuilder.cs b/Tasks/PdfBuilder.cs
index 17c4073..f1b5f82 100644
--- a/Tasks/PdfBuilder.cs
+++ b/Tasks/PdfBuilder.cs
@@ -230,6 +230,11 @@ public class PdfBuilder
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) == "")
{
@@ -239,10 +244,17 @@ public class PdfBuilder
var inner = line.Substring(i + 7, endTag - (i + 7));
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);
+
+ 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;
}
@@ -259,11 +271,18 @@ public class PdfBuilder
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, maxWidth - (currentX - x), _boldFont.Size * 1.2),
+ new XRect(currentX, y, remainingWidth, _boldFont.Size * 1.2),
XStringFormats.TopLeft);
- var measured = gfx.MeasureString(boldText, _boldFont);
currentX += measured.Width;
i = endIndex + 2;
continue;
@@ -277,9 +296,16 @@ public class PdfBuilder
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);
+
+ 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;
}
@@ -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;
+ }
+
///
/// Converts millimeters to points (1 mm = 2.834645669 points)
///