Well... I spent some time with IQueryGeometry.QueryEnvelope and keep getting "method or operation not implemented".I would think there is a way to get the balloon callout's envelope directly, but if you can't get it to work -- or if someone else doesn't jump into this thread with the solution -- I found a cludgy way to get it: First place the element without an anchor and get the bounds of the callout. Then add the anchor.
public void PerformTest()
{
double fromX = 0;
double fromY = 0;
ITextElement textElement = new TextElementClass();
IElement element = textElement as IElement;
IElementProperties elementProperties = element as IElementProperties;
textElement.Text = "Sample text to go in a callout to test getting the size. " +
"\r\nThis adds some text on a second line." +
"\r\nThis adds some text on a third line.";
ESRI.ArcGIS.Geometry.Point point = new ESRI.ArcGIS.Geometry.Point();
double offSetX = 0;
double offSetY = 0;
if (ArcMap.Document.ActivatedView is IPageLayout)
{
// inches, page layout units
offSetX = 0.5;
offSetY = 1;
}
else
{
// feet. This worked for my test map's scale.
offSetX = 800;
offSetY = 1500;
}
// Set the location of the callout balloon (lower-left corner of text)
point.PutCoords(fromX + offSetX, fromY + offSetY);
element.Geometry = point;
// Create text symbology
IFormattedTextSymbol formattedTextSymbol = new TextSymbol() as IFormattedTextSymbol;
stdole.IFontDisp fontFrom = formattedTextSymbol.Font;
fontFrom.Size = 8;
formattedTextSymbol.Font = fontFrom;
formattedTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;
ICallout callOut = new BalloonCalloutClass();
formattedTextSymbol.Background = (ITextBackground)callOut;
textElement.Symbol = formattedTextSymbol;
IGraphicsContainer graphicsContainer = ArcMap.Document.ActivatedView as IGraphicsContainer;
// Add the callout element without a leader to an anchor point
graphicsContainer.AddElement(element, 0);
// Get the bounds of callout envelope for just the balloon
IEnvelope elementEnvelope = new EnvelopeClass();
element.QueryBounds(ArcMap.Document.ActiveView.ScreenDisplay, elementEnvelope);
System.Diagnostics.Debug.WriteLine("Element (Before Anchor): " + elementEnvelope.Width + ", " + elementEnvelope.Height);
System.Diagnostics.Debug.WriteLine(" SpatialReference: " + (textElement as IGraphicElement).SpatialReference.Name);
System.Diagnostics.Debug.WriteLine(" UpperLeft:" + elementEnvelope.UpperLeft.X + ", " + elementEnvelope.UpperLeft.Y);
System.Diagnostics.Debug.WriteLine(" LowerLeft:" + elementEnvelope.LowerLeft.X + ", " + elementEnvelope.LowerLeft.Y);
System.Diagnostics.Debug.WriteLine(" UpperRight:" + elementEnvelope.UpperRight.X + ", " + elementEnvelope.UpperRight.Y);
System.Diagnostics.Debug.WriteLine(" LowerRight:" + elementEnvelope.LowerRight.X + ", " + elementEnvelope.LowerRight.Y);
// Set the leader anchor point
ESRI.ArcGIS.Geometry.Point anchorPTFrom = new ESRI.ArcGIS.Geometry.Point();
anchorPTFrom.PutCoords(fromX, fromY);
callOut.AnchorPoint = anchorPTFrom;
// Must re-assign this or leader to anchor is not created on map and querybounds
// returns same value as before
textElement.Symbol = formattedTextSymbol;
// Get the bounds of the entire callout envelope surrounding balloon and leader
element.QueryBounds(ArcMap.Document.ActiveView.ScreenDisplay, elementEnvelope);
System.Diagnostics.Debug.WriteLine("Element (After Anchor): " + elementEnvelope.Width + ", " + elementEnvelope.Height);
System.Diagnostics.Debug.WriteLine(" SpatialReference: " + (textElement as IGraphicElement).SpatialReference.Name);
System.Diagnostics.Debug.WriteLine(" UpperLeft:" + elementEnvelope.UpperLeft.X + ", " + elementEnvelope.UpperLeft.Y);
System.Diagnostics.Debug.WriteLine(" LowerLeft:" + elementEnvelope.LowerLeft.X + ", " + elementEnvelope.LowerLeft.Y);
System.Diagnostics.Debug.WriteLine(" UpperRight:" + elementEnvelope.UpperRight.X + ", " + elementEnvelope.UpperRight.Y);
System.Diagnostics.Debug.WriteLine(" LowerRight:" + elementEnvelope.LowerRight.X + ", " + elementEnvelope.LowerRight.Y);
}