DrawWrappedText Method (GdPicturePDF)
In This Topic
Draws, in other words, wraps, required text using the specified font to fit the defined rectangle located on the currently selected page of the loaded PDF document. You need to set the rectangle coordinates in the current units with respect to the currently located origin defined in the PDF document, at the same all coordinates are related to the actual page, where the text is to be drawn. You are also allowed to specify the text alignment within the defined rectangle and the calculation of the height of the text line. The aim of this method is to respect the rectangle boundaries when drawing text, not to draw entire text.
You can use the GdPicturePDF.GetMeasurementUnit method to determine the currently defined units together with the GdPicturePDF.GetOrigin method to determine the current origin location and you can also use the GdPicturePDF.SetMeasurementUnit method to reset the units together with the GdPicturePDF.SetOrigin method to reset the origin location according to your preference.
Syntax
'Declaration
Public Function DrawWrappedText( _
ByVal As String, _
ByVal As Single, _
ByVal As Single, _
ByVal As Single, _
ByVal As Single, _
ByVal As TextAlignment, _
ByVal As String, _
ByVal As Boolean, _
ByRef As Integer _
) As GdPictureStatus
public GdPictureStatus DrawWrappedText(
string ,
float ,
float ,
float ,
float ,
TextAlignment ,
string ,
bool ,
ref int
)
public function DrawWrappedText(
: String;
: Single;
: Single;
: Single;
: Single;
: TextAlignment;
: String;
: Boolean;
var : Integer
): GdPictureStatus;
public function DrawWrappedText(
: String,
: float,
: float,
: float,
: float,
: TextAlignment,
: String,
: boolean,
: int
) : GdPictureStatus;
public: GdPictureStatus DrawWrappedText(
string* ,
float ,
float ,
float ,
float ,
TextAlignment ,
string* ,
bool ,
ref int
)
public:
GdPictureStatus DrawWrappedText(
String^ ,
float ,
float ,
float ,
float ,
TextAlignment ,
String^ ,
bool ,
int%
)
Parameters
- FontResName
- The resource name of the font you prefer for drawing your text.
You can easily obtain this name using the GdPicturePDF.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 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.
- 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
A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.
We strongly recommend always checking this status first.
Example
How to draw required text to be centered within the specified rectangle. The example also shows you what the StartPos parameter actually indicates.
Dim caption As String = "Example: DrawWrappedText"
Dim message As String = "The example has not been followed successfully." + vbCrLf + "The last known status is "
Dim gdpicturePDF As 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
Dim startPosDraw As Integer = 0, startPosH As Integer = 0
Dim text As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
If (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(0, 0, 255) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetTextSize(20) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetLineWidth(0.01F) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawRectangle(2, 2, 6, 6, False, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawWrappedText(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, False, startPosDraw) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 3, 9, "StartPos = " + startPosDraw.ToString()) = GdPictureStatus.OK) Then
Dim height As Single = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, False, startPosH)
If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 3, 11, "Height = " + height.ToString()) = GdPictureStatus.OK) Then
startPosDraw = 0
If (gdpicturePDF.DrawRectangle(10, 2, 6, 6, False, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawWrappedText(fontName, 10, 2, 16, 8, TextAlignment.TextAlignmentCenter, text, True, startPosDraw) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 11, 9, "StartPos = " + startPosDraw.ToString()) = GdPictureStatus.OK) Then
height = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, True, startPosH)
If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 11, 11, "Height = " + height.ToString()) = GdPictureStatus.OK) Then
status = gdpicturePDF.SaveToFile("test_DrawWrappedText.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: DrawWrappedText";
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)
{
int startPosDraw = 0, startPosH = 0;
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
if ((gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK) &&
(gdpicturePDF.SetFillColor(0, 0, 255) == GdPictureStatus.OK) &&
(gdpicturePDF.SetTextSize(20) == GdPictureStatus.OK) &&
(gdpicturePDF.SetLineWidth(0.01f) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawRectangle(2, 2, 6, 6, false, true) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawWrappedText(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, false, ref startPosDraw) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 3, 9, "StartPos = " + startPosDraw.ToString()) == GdPictureStatus.OK))
{
float height = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, false, ref startPosH);
if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 3, 11, "Height = " + height.ToString()) == GdPictureStatus.OK))
{
startPosDraw = 0;
if ((gdpicturePDF.DrawRectangle(10, 2, 6, 6, false, true) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawWrappedText(fontName, 10, 2, 16, 8, TextAlignment.TextAlignmentCenter, text, true, ref startPosDraw) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 11, 9, "StartPos = " + startPosDraw.ToString()) == GdPictureStatus.OK))
{
height = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, true, ref startPosH);
if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 11, 11, "Height = " + height.ToString()) == GdPictureStatus.OK))
{
status = gdpicturePDF.SaveToFile("test_DrawWrappedText.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();
Example
How to draw required text to be centered within the specified rectangle. The example also shows you what the StartPos parameter actually indicates.
Dim caption As String = "Example: DrawWrappedText"
Dim message As String = "The example has not been followed successfully." + vbCrLf + "The last known status is "
Dim gdpicturePDF As 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
Dim startPosDraw As Integer = 0, startPosH As Integer = 0
Dim text As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
If (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetFillColor(0, 0, 255) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetTextSize(20) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.SetLineWidth(0.01F) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawRectangle(2, 2, 6, 6, False, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawWrappedText(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, False, startPosDraw) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 3, 9, "StartPos = " + startPosDraw.ToString()) = GdPictureStatus.OK) Then
Dim height As Single = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, False, startPosH)
If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 3, 11, "Height = " + height.ToString()) = GdPictureStatus.OK) Then
startPosDraw = 0
If (gdpicturePDF.DrawRectangle(10, 2, 6, 6, False, True) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawWrappedText(fontName, 10, 2, 16, 8, TextAlignment.TextAlignmentCenter, text, True, startPosDraw) = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 11, 9, "StartPos = " + startPosDraw.ToString()) = GdPictureStatus.OK) Then
height = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, True, startPosH)
If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
(gdpicturePDF.DrawText(fontName, 11, 11, "Height = " + height.ToString()) = GdPictureStatus.OK) Then
status = gdpicturePDF.SaveToFile("test_DrawWrappedText.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: DrawWrappedText";
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)
{
int startPosDraw = 0, startPosH = 0;
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
if ((gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK) &&
(gdpicturePDF.SetFillColor(0, 0, 255) == GdPictureStatus.OK) &&
(gdpicturePDF.SetTextSize(20) == GdPictureStatus.OK) &&
(gdpicturePDF.SetLineWidth(0.01f) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawRectangle(2, 2, 6, 6, false, true) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawWrappedText(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, false, ref startPosDraw) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 3, 9, "StartPos = " + startPosDraw.ToString()) == GdPictureStatus.OK))
{
float height = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, false, ref startPosH);
if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 3, 11, "Height = " + height.ToString()) == GdPictureStatus.OK))
{
startPosDraw = 0;
if ((gdpicturePDF.DrawRectangle(10, 2, 6, 6, false, true) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawWrappedText(fontName, 10, 2, 16, 8, TextAlignment.TextAlignmentCenter, text, true, ref startPosDraw) == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 11, 9, "StartPos = " + startPosDraw.ToString()) == GdPictureStatus.OK))
{
height = gdpicturePDF.GetWrappedTextHeight(fontName, 2, 2, 8, 8, TextAlignment.TextAlignmentCenter, text, true, ref startPosH);
if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
(gdpicturePDF.DrawText(fontName, 11, 11, "Height = " + height.ToString()) == GdPictureStatus.OK))
{
status = gdpicturePDF.SaveToFile("test_DrawWrappedText.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