PDF for WPF and Silverlight Overview > Features: PDF for WPF and Silverlight > Adding Text > Drawing Text |
VB |
Copy Code
|
---|---|
pdf.DrawString("Hello World!", font, Colors.Black, rect)
|
C# |
Copy Code
|
---|---|
pdf.DrawString("Hello World!", font, Colors.Black, rect);
|
pdf.DrawString("Hello World!", font, Colors.Black, rect);
By default, C1PdfDocument.DrawString will align the text to the left and to the top of the given rectangle, will wrap the string within the rectangle, and will not clip the output to the rectangle. You can change all these options by specifying a StringFormat parameter in the call to C1PdfDocument.DrawString. The StringFormat has members that allow you to specify the horizontal alignment (Alignment), vertical alignment (LineAligmnent), and flags that control wrapping and clipping (FormatFlags).
For example, the code below creates a StringFormat object and uses it to align the text to the center of the rectangle, both vertically and horizontally:
VB |
Copy Code
|
---|---|
Dim font As New C1.WPF.Pdf.Font("Arial", 12) Dim rect As New Rect(72, 72, 100, 50) Dim text As String = "Some long string to be rendered into a small rectangle. " text = Convert.ToString(Convert.ToString(Convert.ToString(Convert.ToString(text & text) & text) & text) & text) & text ' Center align string. Dim sf As New C1.WPF.Pdf.StringFormat() sf.Alignment = HorizontalAlignment.Center sf.LineAlignment = VerticalAlignment.Center pdf.DrawString(text, font, Colors.Black, rect, sf) pdf.DrawRectangle(Colors.Gray, rect) |
C# |
Copy Code
|
---|---|
C1.WPF.Pdf.Font font = new C1.WPF.Pdf.Font("Arial", 12); Rect rect = new Rect(72, 72, 100, 50); string text = "Some long string to be rendered into a small rectangle. "; text = text + text + text + text + text + text; // Center align string. C1.WPF.Pdf.StringFormat sf = new C1.WPF.Pdf.StringFormat(); sf.Alignment = HorizontalAlignment.Center; sf.LineAlignment = VerticalAlignment.Center; pdf.DrawString(text, font, Colors.Black, rect, sf); pdf.DrawRectangle(Colors.Gray, rect); //System.Windows.Media.Colors.Gray; |
pdf.DrawString("Hello World!", font, Colors.Black, rect);
Here is the resulting PDF document: