GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / GetTextWidth Method
The resource name of the font you prefer.

You can easily obtain this name using the AddStandardFont method or any of the AddTrueTypeFont...() methods. For further assistance, please see the section Fonts of the GdPicturePDF class in the Reference Guide.

The size of the text (the font size actually), in points.
The required text to measure.
Example





In This Topic
GetTextWidth Method (GdPicturePDF)
In This Topic
Calculates the width of entered text, using the font you have specified, in the currently defined units within the loaded PDF document.

You can use the GetMeasurementUnit method to determine the currently defined units and you can also use the SetMeasurementUnit method to reset the units according to your preference.

Syntax
'Declaration
 
Public Function GetTextWidth( _
   ByVal FontResName As String, _
   ByVal TextSize As Single, _
   ByVal Text As String _
) As Single
public float GetTextWidth( 
   string FontResName,
   float TextSize,
   string Text
)
public function GetTextWidth( 
    FontResName: String;
    TextSize: Single;
    Text: String
): Single; 
public function GetTextWidth( 
   FontResName : String,
   TextSize : float,
   Text : String
) : float;
public: float GetTextWidth( 
   string* FontResName,
   float TextSize,
   string* Text
) 
public:
float GetTextWidth( 
   String^ FontResName,
   float TextSize,
   String^ Text
) 

Parameters

FontResName
The resource name of the font you prefer.

You can easily obtain this name using the AddStandardFont method or any of the AddTrueTypeFont...() methods. For further assistance, please see the section Fonts of the GdPicturePDF class in the Reference Guide.

TextSize
The size of the text (the font size actually), in points.
Text
The required text to measure.

Return Value

The width of specified text, expressed in the current units specified by the SetMeasurementUnit method.

The GetStat method can be subsequently used to determine if this method has been successful.

Remarks
This method is only allowed for use with non-encrypted documents.

It is recommend to use the GetStat method to identify the specific reason for the method's failure, if any. For example, if the currently loaded PDF document does not contain any page, this method will fail.

Just to remind you that 1 point is equivalent to 1/72 of an inch.

Example
How to determine the width of the specified text using the standard Times-Roman font.
Dim caption As String = "Example: GetTextWidth"
Dim gdpicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.LoadFromFile("test.pdf", False)
If status = GdPictureStatus.OK Then
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    status = gdpicturePDF.GetStat()
    If status = GdPictureStatus.OK Then
        Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontTimesRoman)
        If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
            Dim width As Single = gdpicturePDF.GetTextWidth(fontName, 20, "I love GdPicture.NET")
            status = gdpicturePDF.GetStat()
            If status = GdPictureStatus.OK Then
                MessageBox.Show("The width of your text ""I love GdPicture.NET"" using the Times-Roman font with the size 20 is " + width.ToString() + " centimeters.", caption)
            Else
                MessageBox.Show("The GetTextWidth() method has failed with the status: " + status.ToString(), caption)
            End If
        Else
            MessageBox.Show("The AddStandardFont() method has failed with the status: " + status.ToString(), caption)
        End If
    Else
        MessageBox.Show("Units can't be set correctly.", caption)
    End If
Else
    MessageBox.Show("The file can't be loaded.", caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: GetTextWidth";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.LoadFromFile("test.pdf", false);
if (status == GdPictureStatus.OK)
{
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
    status = gdpicturePDF.GetStat();
    if (status == GdPictureStatus.OK)
    {
        string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontTimesRoman);
        if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
        {
            float width = gdpicturePDF.GetTextWidth(fontName, 20, "I love GdPicture.NET");
            status = gdpicturePDF.GetStat();
            if (status == GdPictureStatus.OK)
                MessageBox.Show("The width of your text \"I love GdPicture.NET\" using the Times-Roman font with the size 20 is " + width.ToString() + " centimeters.", caption);
            else
                MessageBox.Show("The GetTextWidth() method has failed with the status: " + status.ToString(), caption);
        }
        else
            MessageBox.Show("The AddStandardFont() method has failed with the status: " + status.ToString(), caption);
    }
    else
        MessageBox.Show("Units can't be set correctly.", caption);
}
else
    MessageBox.Show("The file can't be loaded.", caption);
gdpicturePDF.Dispose();
See Also