Available for: UI for Blazor | UI for Xamarin | UI for ASP.NET Core

New to Telerik Document Processing? Download free 30-day trial

Text Measuring

SpreadProcessing provides a built-in functionality and an extensibility point for text measuring to overcome the limitation of .NET Standard. Since the platform is intended to work on different devices, it doesn't expose text measuring API and SpreadProcessing must have its own one to find out the letters and words size when exporting to PDF.

This topic describes the default and the extended implementations used to measure text while exporting to PDF.

Using a Text Measurer

The SpreadExtensibilityManager static class exposes the TextMeasurer property. This property defines the specific implementation that is used to measure the text content when needed. You can use the following values for this property:

  • SimpleTextMeasurer: This is the default value.
  • SpreadFixedTextMeasurer: An additional implementation that provides more accurate results than the SimpleTextMeasurer.
  • Custom implementation: Implementing the abstract SpreadTextMeasurerBase class enables you to provide your own implementation for text measuring.

SimpleTextMeasurer

This is the measurer used by default when exporting to PDF.

The width of the columns in Excel is stored as a character count calculated based on the maximum width of the digits from 0 to 9 with the Normal Style of the document applied to them. SimpleTextMeasurer uses the measured width of the digits for a predefined set of font families to calculate the column width based on the length of the text multiplied by the calculated character size.

  • TextMeasurer: Gets or sets a SpreadTextMeasurerBase instance used to provide text measuring. The TextMeasurer has a SimpleTextMeasurer as a default value.

The SimpleTextMeasurer provides basic functionality for text measuring and the results might not be satisfying in each case. For better results, use SpreadFixedTextMeasurer.

SpreadFixedTextMeasurer

This implementation uses PdfProcessing to obtain the size of the text and provides great precision. You need to explicitly set it to the TextMeasurer property of SpreadExtensibilityManager.

To use this class, you must add a reference to Telerik.Documents.Fixed.dll.

Example 1: Set the SpreadFixedTextMeasurer as a text measurer

SpreadTextMeasurerBase fixedTextMeasurer = new SpreadFixedTextMeasurer(); 
SpreadExtensibilityManager.TextMeasurer = fixedTextMeasurer; 

Custom Text Measurer

You can assign any SpreadTextMeasurerBase implementation to the SpreadExtensibilityManager.TextMeasurer property. All you should do is to inherit the abstract SpreadTextMeasurerBase, implement the required members and set the new implementation to the TextMeasurer property.

Example 2: Create a custom implementation inheriting the SpreadTextMeasurerBase abstract class

public class CustomTextMeasurer : SpreadTextMeasurerBase  
{  
    private static readonly double ratioX = 1.035;  
    private static readonly double ratioY = 1;  
    private static readonly double ratioBaseline = 1;  
 
    private readonly SpreadTextMeasurerBase originalMeasurer;  
 
    public CustomTextMeasurer(SpreadTextMeasurerBase originalMeasurer)  
    {  
        this.originalMeasurer = originalMeasurer;  
    }  
 
    public override TextMeasurementInfo MeasureText(TextProperties textProperties, FontProperties fontProperties)  
    {  
        TextMeasurementInfo info = originalMeasurer.MeasureText(textProperties, fontProperties);  
 
        Size size = info.Size;  
        return new TextMeasurementInfo()  
        {  
            BaselineOffset = info.BaselineOffset * ratioBaseline,  
            Size = new Size(  
                size.Width * ratioX,  
                size.Height * ratioY),  
        };  
    }  
 
    public override TextMeasurementInfo MeasureTextWithWrapping(TextProperties textProperties, FontProperties fontProperties, double wrappingWidth)  
    {  
        TextMeasurementInfo info = originalMeasurer.MeasureText(textProperties, fontProperties);  
 
        Size size = info.Size;  
        return new TextMeasurementInfo()  
        {  
            BaselineOffset = info.BaselineOffset * ratioBaseline,  
            Size = new Size(  
                size.Width * ratioX,  
                size.Height * ratioY),  
        };  
    }  
}  

Example 3: Set the custom implementation as a text measurer

SpreadTextMeasurerBase customTextMeasurer = new CustomTextMeasurer();  
SpreadExtensibilityManager.TextMeasurer = customTextMeasurer;  

See Also

In this article
Not finding the help you need? Improve this article