PDF for UWP | ComponentOne
Using PDF for UWP / Adding Text / Drawing Text
In This Topic
    Drawing Text
    In This Topic

    Adding text to PDF for UWP documents is easy – all the work is done by the C1PdfDocument.DrawString method.

    C1PdfDocument.DrawString draws a given string at a specified location using a given font and brush. For example:

    VB
    Copy Code
    pdf.DrawString("Hello World!", font, Colors.Black, rect)  
    
    C#
    Copy Code
    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 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 StringFormat()
     sf.Alignment = HorizontalAlignment.Center
     sf.LineAlignment = VerticalAlignment.Center
     pdf.DrawString(text, font, Windows.UI.Colors.Black, rect, sf)
     pdf.DrawRectangle(Windows.UI.Colors.Gray, rect)   
        
    
    C#
    Copy Code
    Font font = new 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.
    StringFormat sf = new StringFormat();
    sf.Alignment = HorizontalAlignment.Center;
    sf.LineAlignment = VerticalAlignment.Center;
    pdf.DrawString(text, font, Windows.UI.Colors.Black, rect, sf);
    pdf.DrawRectangle(Windows.UI.Colors.Gray, rect);
    

    Here is the resulting PDF document: