Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
765
Limiting IText content to only on one line with Elipsis (TextTrimming)
posted

Hi

I would like to create  A header on a report section page  and for that I use an IText. But I dont want a text wrap if the text is too long for one line. If there is not enough room for one line I want the text to end with an elipsis. "..."

Something similar to the property  TextTrimming of the ultraLabel.

how can this be done

thanks.

  • 37774
    Verified Answer
    posted

    It doesn't seem like there is any support for text trimming, since this is generally performed by the .NET graphics object.  Since the Documents engine provides a proxy to this, I tried to work around the issue by drawing the string myself, but it seems that the proxy does not support this format flag.

    Your best option would be to try measuring the text yourself through the Documents engine (as mentioned in my reply to your post here); I think that in this case you could simply use the GetCharWidth() method and keep building the string until you run out of room:

    string desiredString = "This is some really long text that likely isn't going to fit the header but let's live a little bit because I like to write stuff that goes on for a while";
    Infragistics.Documents.Graphics.Font font = new Infragistics.Documents.Graphics.Font("Arial", 14);
    ICanvas canvas = section.AddCanvas();
    canvas.Font = font;
    float ellipsesWidth = canvas.GetCharWidth('.') * 3;
    float totalWidth = 0;
    StringBuilder currentString = new StringBuilder();
    foreach (char c in desiredString)
    {
        totalWidth += canvas.GetCharWidth(c);
        if (totalWidth < section.PageSize.Width - ellipsesWidth)
            currentString.Append(c);
        else
        {
            // Remove any trailing spaces at the end before the ellipses
            while (currentString[currentString.Length - 1] == ' ')
                currentString.Remove(currentString.Length - 1, 1);

            currentString.Append("...");
            break;
        }
    }

    IText text = header.AddText(0, 0);
    text.Style.Font = font;
    text.AddContent(currentString.ToString());

    -Matt