GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / GetWrappedTextLineCount 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 required width of the rectangle, expressed in the current units specified by the SetMeasurementUnit method, 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 line 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 line 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.

Example





In This Topic
GetWrappedTextLineCount Method (GdPicturePDF)
In This Topic
Calculates the number of text lines of the given text, which will fit the rectangle with the defined width, 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 GetWrappedTextLineCount( _
   ByVal FontResName As String, _
   ByVal Width As Single, _
   ByVal HorizontalAlignment As TextAlignment, _
   ByVal Text As String, _
   ByVal UseFontBBox As Boolean _
) As Integer
public int GetWrappedTextLineCount( 
   string FontResName,
   float Width,
   TextAlignment HorizontalAlignment,
   string Text,
   bool UseFontBBox
)
public function GetWrappedTextLineCount( 
    FontResName: String;
    Width: Single;
    HorizontalAlignment: TextAlignment;
    Text: String;
    UseFontBBox: Boolean
): Integer; 
public function GetWrappedTextLineCount( 
   FontResName : String,
   Width : float,
   HorizontalAlignment : TextAlignment,
   Text : String,
   UseFontBBox : boolean
) : int;
public: int GetWrappedTextLineCount( 
   string* FontResName,
   float Width,
   TextAlignment HorizontalAlignment,
   string* Text,
   bool UseFontBBox
) 
public:
int GetWrappedTextLineCount( 
   String^ FontResName,
   float Width,
   TextAlignment HorizontalAlignment,
   String^ Text,
   bool UseFontBBox
) 

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.

Width
The required width of the rectangle, expressed in the current units specified by the SetMeasurementUnit method, 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 line 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 line 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.

Return Value

The number of text lines of the wrapped text that fits within the rectangle with the specified width.

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.

Example
How to find out the resulting number of text lines for the given text that will fit into a required rectangle.
Dim caption As String = "Example: GetWrappedTextLineCount"
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.
            
            Dim textheight As Single = gdpicturePDF.GetTextHeight(fontName, textsize) 'This is the height of the text = the height of one text line.
            If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
               (gdpicturePDF.SetTextSize(textsize) = GdPictureStatus.OK) Then
                'It is important to set the size of the text before all drawing operations.
                'Computing the number of text lines for the whole text.
                Dim textlines As Integer = gdpicturePDF.GetWrappedTextLineCount(fontName, rectwidth, TextAlignment.TextAlignmentCenter, text, False)
                If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                    Dim startPos As Integer = 0
                    Dim rectheight As Integer = CInt(Math.Abs(textheight * textlines)) + 1 'This is the whole height of the rectangle for writing the text.
                    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
                        'You can find out, if the whole text is drawn, by using the startPos parameter.
                        status = gdpicturePDF.SaveToFile("test_GetWrappedTextLineCount.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: GetWrappedTextLineCount";
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.
            float textheight = gdpicturePDF.GetTextHeight(fontName, textsize); //This is the height of the text = the height of one text line.
            if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
                (gdpicturePDF.SetTextSize(textsize) == GdPictureStatus.OK))
                //It is important to set the size of the text before all drawing operations.
            {
                //Computing the number of text lines for the whole text.
                int textlines = gdpicturePDF.GetWrappedTextLineCount(fontName, rectwidth, TextAlignment.TextAlignmentCenter, text, false);
                if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
                {
                    int startPos = 0;
                    int rectheight = (int)Math.Abs(textheight * textlines) + 1; //This is the whole height of the rectangle for writing the text.
                    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))
                    {
                        //You can find out, if the whole text is drawn, by using the startPos parameter.
                        status = gdpicturePDF.SaveToFile("test_GetWrappedTextLineCount.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