GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / GetWrappedTextHeight Method
The resource name of the font you prefer for drawing your text.

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 horizontal (X) coordinate of the closest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
The vertical (Y) coordinate of the closest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
The horizontal (X) coordinate of the furthest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
The vertical (Y) coordinate of the furthest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
The required horizontal text alignment mode, that is used to draw text within the specified rectangle.
The required text to draw.
Set this parameter to true if you want to calculate the text height by using the font boundary box. It is an imaginary box that encloses all glyphs from the font, usually as tightly as possible.

Set this parameter to false if you want to calculate the text height by using the font's ascent and descent properties. The ascent property is the distance from the baseline to the highest grid coordinate used to place an outline point, the descent property to the lowest grid coordinate.

It is both the input and the output parameter.

As the input parameter it sets the position of the first character from the specified text you want to start with when drawing.

As the output parameter it returns the position of the character that has not been drawn because of the rectangle's boundaries or -1 if the whole text has been wrapped successfully to fit the specified rectangle.

Example





In This Topic
GetWrappedTextHeight Method (GdPicturePDF)
In This Topic
Calculates the total height, expressed in the current units defined in the loaded PDF document, of the given text, which will fit the defined rectangle when trying to wrap the text inside this rectangle, with respect to other parameters you have specified. You can subsequently use the DrawWrappedText method to draw the text inside the required rectangle. Please find more details in the documentation of the mentioned method.
Syntax
'Declaration
 
Public Function GetWrappedTextHeight( _
   ByVal FontResName As String, _
   ByVal Left As Single, _
   ByVal Top As Single, _
   ByVal Right As Single, _
   ByVal Bottom As Single, _
   ByVal HorizontalAlignment As TextAlignment, _
   ByVal Text As String, _
   ByVal UseFontBBox As Boolean, _
   ByRef StartPos As Integer _
) As Single
public float GetWrappedTextHeight( 
   string FontResName,
   float Left,
   float Top,
   float Right,
   float Bottom,
   TextAlignment HorizontalAlignment,
   string Text,
   bool UseFontBBox,
   ref int StartPos
)
public function GetWrappedTextHeight( 
    FontResName: String;
    Left: Single;
    Top: Single;
    Right: Single;
    Bottom: Single;
    HorizontalAlignment: TextAlignment;
    Text: String;
    UseFontBBox: Boolean;
   var  StartPos: Integer
): Single; 
public function GetWrappedTextHeight( 
   FontResName : String,
   Left : float,
   Top : float,
   Right : float,
   Bottom : float,
   HorizontalAlignment : TextAlignment,
   Text : String,
   UseFontBBox : boolean,
   StartPos : int
) : float;
public: float GetWrappedTextHeight( 
   string* FontResName,
   float Left,
   float Top,
   float Right,
   float Bottom,
   TextAlignment HorizontalAlignment,
   string* Text,
   bool UseFontBBox,
   ref int StartPos
) 
public:
float GetWrappedTextHeight( 
   String^ FontResName,
   float Left,
   float Top,
   float Right,
   float Bottom,
   TextAlignment HorizontalAlignment,
   String^ Text,
   bool UseFontBBox,
   int% StartPos
) 

Parameters

FontResName
The resource name of the font you prefer for drawing your text.

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.

Left
The horizontal (X) coordinate of the closest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
Top
The vertical (Y) coordinate of the closest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
Right
The horizontal (X) coordinate of the furthest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
Bottom
The vertical (Y) coordinate of the furthest point to the currently defined origin, where the text box is located, in the current units used in the PDF document, related to the currently selected page.
HorizontalAlignment
The required horizontal text alignment mode, that is used to draw text within the specified rectangle.
Text
The required text to draw.
UseFontBBox
Set this parameter to true if you want to calculate the text height by using the font boundary box. It is an imaginary box that encloses all glyphs from the font, usually as tightly as possible.

Set this parameter to false if you want to calculate the text height by using the font's ascent and descent properties. The ascent property is the distance from the baseline to the highest grid coordinate used to place an outline point, the descent property to the lowest grid coordinate.

StartPos
It is both the input and the output parameter.

As the input parameter it sets the position of the first character from the specified text you want to start with when drawing.

As the output parameter it returns the position of the character that has not been drawn because of the rectangle's boundaries or -1 if the whole text has been wrapped successfully to fit the specified rectangle.

Return Value

The total height of the wrapped text within the whole specified rectangle, 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.

Be aware that the values of coordinates and dimensions are expressed in the current units defined by the SetMeasurementUnit method according to the current coordinate space defined by the SetOrigin method.

Example
How to calculate the height of the given text to determine the height of the rectangle which will border the text.
Dim caption As String = "Example: GetWrappedTextHeight"
Dim message As String = "The example has not been followed successfully." + vbCrLf + "The last known status is "
Dim gdpicturePDF As GdPicturePDF = New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.NewPDF()
If status = GdPictureStatus.OK Then
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontTimesBold)
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        If gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK Then
            
            Dim text As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
                                 "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " +
                                 "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
                                 "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
            
            Dim xp As Integer = 3, yp As Integer = 4 'This is the point where we start drawing.
            Dim textsize As Integer = 20 'This is the size of the drawn text.
            Dim rectwidth As Integer = 14 'This is the width of the rectangle, in which we want to write the whole text.
            
            If gdpicturePDF.SetTextSize(textsize) = GdPictureStatus.OK Then 'It is important to set the size of the text before all drawing operations.
                Dim startPos As Integer = 0
                'Computing the total height of the wrapped text.
                Dim rectheight As Single = gdpicturePDF.GetWrappedTextHeight(fontName, xp, yp, xp + 4, yp + 8, TextAlignment.TextAlignmentCenter, text, True, startPos)
                'You can find out, if the whole text is drawn, by using the startPos parameter above.
                If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                    startPos = 0
                    If (gdpicturePDF.SetFillColor(0, 0, 255) = GdPictureStatus.OK) AndAlso
                       (gdpicturePDF.SetLineWidth(0.04F) = GdPictureStatus.OK) AndAlso
                       (gdpicturePDF.DrawRectangle(xp, yp, rectwidth, rectheight, False, True) = GdPictureStatus.OK) AndAlso
                       (gdpicturePDF.DrawWrappedText(fontName, xp, yp, xp + rectwidth, yp + rectheight, TextAlignment.TextAlignmentCenter, text, False, startPos) = GdPictureStatus.OK) Then
                        status = gdpicturePDF.SaveToFile("test_GetWrappedTextHeight.pdf")
                        If status = GdPictureStatus.OK Then
                            message = "The example has been followed successfully and the file has been saved."
                        Else
                            message = "The example has been followed successfully, but the file can't be saved. Status: " + status.ToString()
                        End If
                    Else
                        message = message + gdpicturePDF.GetStat().ToString()
                    End If
                Else
                    message = message + gdpicturePDF.GetStat().ToString()
                End If
            Else
                message = message + gdpicturePDF.GetStat().ToString()
            End If
        Else
            message = message + gdpicturePDF.GetStat().ToString()
        End If
    Else
        message = "The AddStandardFont() method has failed with the status: " + status.ToString()
    End If
Else
    message = "The NewPDF() method has failed with the status: " + status.ToString()
End If
MessageBox.Show(message, caption)
gdpicturePDF.Dispose()
string caption = "Example: GetWrappedTextHeight";
string message = "The example has not been followed successfully.\nThe last known status is ";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.NewPDF();
if (status == GdPictureStatus.OK)
{
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
    string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontTimesBold);
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        if (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK)
        {
            string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
                          "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. " +
                          "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
                          "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
            
            int xp = 3, yp = 4; //This is the point where we start drawing.
            int textsize = 20; //This is the size of the drawn text.
            int rectwidth = 14; //This is the width of the rectangle, in which we want to write the whole text.
            if (gdpicturePDF.SetTextSize(textsize) == GdPictureStatus.OK) //It is important to set the size of the text before all drawing operations.
            {
                int startPos = 0;
                //Computing the total height of the wrapped text.
                float rectheight = gdpicturePDF.GetWrappedTextHeight(fontName, xp, yp, xp+4, yp+8, TextAlignment.TextAlignmentCenter, text, true, ref startPos);
                if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
                {
                    //You can find out, if the whole text is drawn, by using the startPos parameter above.
                    startPos = 0;
                    if ((gdpicturePDF.SetFillColor(0, 0, 255) == GdPictureStatus.OK) &&
                        (gdpicturePDF.SetLineWidth(0.04f) == GdPictureStatus.OK) &&
                        (gdpicturePDF.DrawRectangle(xp, yp, rectwidth, rectheight, false, true) == GdPictureStatus.OK) &&
                        (gdpicturePDF.DrawWrappedText(fontName, xp, yp, xp + rectwidth, yp + rectheight, TextAlignment.TextAlignmentCenter, text, false, ref startPos) == GdPictureStatus.OK))
                    {
                        status = gdpicturePDF.SaveToFile("test_GetWrappedTextHeight.pdf");
                        if (status == GdPictureStatus.OK)
                            message = "The example has been followed successfully and the file has been saved.";
                        else
                            message = "The example has been followed successfully, but the file can't be saved. Status: " + status.ToString();
                    }
                    else
                        message = message + gdpicturePDF.GetStat().ToString();
                }
                else
                    message = message + gdpicturePDF.GetStat().ToString();
            }
            else
                message = message + gdpicturePDF.GetStat().ToString();
        }
        else
            message = message + gdpicturePDF.GetStat().ToString();
    }
    else
        message = "The AddStandardFont() method has failed with the status: " + status.ToString();
}
else
    message = "The NewPDF() method has failed with the status: " + status.ToString();
MessageBox.Show(message, caption);
gdpicturePDF.Dispose();
See Also