How do I clip or truncate text to a given length?

Q: How do I clip or truncate text to a given length?

In other words, if I'm given a text string that would normally require
6
inches to print, but I am printing in a report column that is 2 inches
wide,
how to specify that the target text run is to be no more than 2
inches?

I have:

eBuilder.Reset();

element = eBuilder.CreateTextBegin(font, pointSize);
eWriter.WriteElement(element);
element = eBuilder.CreateTextRun(text);
element.SetTextMatrix(1, 0, 0, 1, docX, docY);
eWriter.WriteElement(element);
eWriter.WriteElement(eBuilder.CreateTextEnd());

Thank you for your help.
--------
A:

After you create a text run, you coulod use use element.GetTextLength
() or
element.GtBBox(rect) to obtain the dimensions of text on the page. You
could
use this information to crop, to clip, to skip the string, or to move
the
the next line. You could use something along the following lines:

Element element = eBuilder.CreateTextRun(text);
element.SetTextMatrix(1, 0, 0, 1, docX, docY);

// Get bounding box for the text.
Rect bbox = new Rect();
element.GetBBox(bbox);

// Clip the remainder of the string, if necessary...
if (bbox.Width()>max_width) {
  Element clip_rect = eBuilder.CreateRect(bbox.x1, bbox.y1, max_width,
bbox.Height());
  clip_rect.SetPathClip(true);
  clip_rect.SetPathStroke(false);
  clip_rect.SetPathFill(false);
  eWriter.WriteElement(clip_rect);

  // Recreate the string, since CreateRect() deleted the last text
run.
  element = eBuilder.CreateTextRun(text);
  element.SetTextMatrix(1, 0, 0, 1, docX, docY);
}

eWriter.WriteElement(eBuilder.CreateTextBegin(font, pointSize));
eWriter.WriteElement(element);
eWriter.WriteElement(eBuilder.CreateTextEnd());