Posted 20 June 2018, 10:19 am EST
We actually have this feature in our plans but did not have the time to add it, it will be in the next update. In the meantime though you can relatively easily do it in user code, attached is a complete sample that demonstrates how this can be done. The relevant code is as follows:
// ...
var doc = new GcPdfDocument();
var g = doc.Pages.Insert(0).Graphics;
const float maxContentWidth = 400f;
var text = "some long text that won't fit in a single line";
var fmt = new TextFormat
{
FontName = "Segoe UI",
FontSize = 20
};
var tl = new TextLayout();
tl.Append(text, fmt);
tl.PerformLayout(true);
// If the text does not fit, trim it:
if (tl.ContentWidth > maxContentWidth)
TrimToWidth(text, fmt, tl, maxContentWidth);
g.DrawTextLayout(tl, new PointF(100, 100));
// ...
}
static void TrimToWidth(string text, TextFormat fmt, TextLayout tl, float width)
{
var runs = tl.Lines[0].GlyphRuns;
int len = 0;
for (int i = runs.Length - 1; i >= 0; i--)
{
var run = runs[i];
if (run.Offset < width)
{
float xStop = (width - run.Offset) / run.FontUnitsToSize;
var gi = run.FirstGlyph;
for (int j = 0; j < run.GlyphCount; j++)
{
xStop -= gi.GlyphAdvance;
if (xStop < 0)
{
break;
}
gi = gi.NextGlyph;
}
len = gi.CodePointIndex;
break;
}
}
var sb = new StringBuilder(text.Substring(0, len));
if (len == 0)
{
sb.Append(' ');
len = 1;
}
do
{
while (len > 1 && Char.IsWhiteSpace(sb[len - 2]))
{
len -= 1;
}
sb.Length = len;
sb[len - 1] = '\u2026'; // Unicode ellipsis symbol
tl.Clear();
tl.Append(sb.ToString(), fmt);
tl.PerformLayout(true);
if (tl.ContentWidth <= width)
{
break;
}
len -= 1;
} while (len > 1);
}
Let me know whether this helps.
Ellipsis.zip