Get text length for single line FreeText

I would like to create a single line FreeText. To do this, I need to calculate the FreeText annotation’s bounds, specified in user space coordinates, based on the length of the text. Below is the idea of what I am trying to accomplish based on other messages in this forum. I have noticed that the height seems a little too small. For the width, it seems too small for a 1 character string, and as the string size grows the width grows by more than double the correct size (and might even be non-linear).

Rect bbox = new Rect(0, 0, 0, 0);

using (ElementBuilder builder = new ElementBuilder())
{
Font font = Font.Create(_parent, Font.StandardType1Font.e_helvetica);
builder.CreateTextBegin(font, 30.0);
Element element = builder.CreateUnicodeTextRun(contents);
element.GetBBox(bbox);
}

double x = Math.Abs(bbox.x2 - bbox.x1);
double y = Math.Abs(bbox.y2 - bbox.y1);

I use the x, y values as an offset from the start point. Any help would be appreciated!

– Michael

You did everything correct, the problem is the usage of CreateUnicodeTextRun. The way you are using it results in a space between each character and therefore doubling the width. Just call CreateTextRun. We are looking to maybe deprecate CreateUnicodeTextRun as it is for some very particular cases.