VintaSoft Imaging .NET SDK v8.7
Vintasoft.Imaging.Pdf.Drawing Namespace / PdfGraphics Class / DrawString Method / DrawString(String,Font,PdfBrush,RectangleF,StringFormat) Method
String to draw.
System.Drawing.Font that defines the string.
PdfBrush that determines the color and texture of the drawn text.
System.Drawing.RectangleF structure that specifies the location of the drawn text.
System.Drawing.StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.
Example



In This Topic
    DrawString(String,Font,PdfBrush,RectangleF,StringFormat) Method
    In This Topic
    Draws the specified text string in vector form at the specified rectangle with specified PdfBrush and System.Drawing.Font objects using the formatting attributes specified by System.Drawing.StringFormat.
    Syntax
    'Declaration
     
    Public Overloads Sub DrawString( _
       ByVal s As String, _
       ByVal font As Font, _
       ByVal brush As PdfBrush, _
       ByVal rect As RectangleF, _
       ByVal sf As StringFormat _
    ) 
    public void DrawString( 
       string s,
       Font font,
       PdfBrush brush,
       RectangleF rect,
       StringFormat sf
    )
    public: void DrawString( 
       string* s,
       Font* font,
       PdfBrush* brush,
       RectangleF rect,
       StringFormat* sf
    ) 
    public:
    void DrawString( 
       String^ s,
       Font^ font,
       PdfBrush^ brush,
       RectangleF rect,
       StringFormat^ sf
    ) 

    Parameters

    s
    String to draw.
    font
    System.Drawing.Font that defines the string.
    brush
    PdfBrush that determines the color and texture of the drawn text.
    rect
    System.Drawing.RectangleF structure that specifies the location of the drawn text.
    sf
    System.Drawing.StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.
    Remarks
    This method does NOT ignore carriage returns and new line characters.

    Use overloaded method that takes point that specifies location, instead of rectangle, if carriage returns and new line characters should be ignored.
    Example
    This example shows how to convert a text file to a PDF document.
    ''' <summary>
    ''' Converts a text file to a PDF document.
    ''' </summary>
    ''' <param name="sourceTextFilename">The filename of source text file.</param>
    ''' <param name="destPdfFilename">The filename of destination PDF document.</param>
    Public Shared Sub ConvertTextFileToPdfDocument(sourceTextFilename As String, destPdfFilename As String)
    	' get text of text file
    	Dim text As String = System.IO.File.ReadAllText(sourceTextFilename, System.Text.Encoding.UTF8)
                
    	' create PDF document
    	Using pdfDocument As New Vintasoft.Imaging.Pdf.PdfDocument(destPdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14)
    		' create page of A4 size
    		Dim pdfPage As New Vintasoft.Imaging.Pdf.Tree.PdfPage(pdfDocument, Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4))
    		' add page to the PDF document
    		pdfDocument.Pages.Add(pdfPage)
                
    		' get PdfGrahics of page
    		Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(pdfPage)
    			' create a font that should be used for drawing a text
    			Dim font As New System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 14)
                
    			' create a brush that should be used for drawing a text
    			Dim brush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black)
                
    			' specify a rectangle where text should be drawn
    			Dim rect As System.Drawing.RectangleF = pdfPage.MediaBox
                
    			' specify a string format that should be used for drawing a text 
    			Dim stringFormat As System.Drawing.StringFormat = System.Drawing.StringFormat.GenericDefault
                
    			' draw text on the PDF page
    			graphics.DrawString(text, font, brush, rect, stringFormat)
    		End Using
                
    		' save PDF document
    		pdfDocument.SaveChanges()
    	End Using
    End Sub
    /// <summary>
    /// Converts a text file to a PDF document.
    /// </summary>
    /// <param name="sourceTextFilename">The filename of source text file.</param>
    /// <param name="destPdfFilename">The filename of destination PDF document.</param>
    public static void ConvertTextFileToPdfDocument(
        string sourceTextFilename,
        string destPdfFilename)
    {
        // get text of text file
        string text = System.IO.File.ReadAllText(sourceTextFilename, System.Text.Encoding.UTF8);
                
        // create PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument = 
            new Vintasoft.Imaging.Pdf.PdfDocument(destPdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14))
        {
            // create page of A4 size
            Vintasoft.Imaging.Pdf.Tree.PdfPage pdfPage = 
                new Vintasoft.Imaging.Pdf.Tree.PdfPage(pdfDocument, 
                    Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4));
            // add page to the PDF document
            pdfDocument.Pages.Add(pdfPage);
                
            // get PdfGrahics of page
            using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = 
                Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(pdfPage))
            {
                // create a font that should be used for drawing a text
                System.Drawing.Font font = 
                    new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 14);
                
                // create a brush that should be used for drawing a text
                Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush = 
                    new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black);
                
                // specify a rectangle where text should be drawn
                System.Drawing.RectangleF rect = pdfPage.MediaBox;
                
                // specify a string format that should be used for drawing a text 
                System.Drawing.StringFormat stringFormat = System.Drawing.StringFormat.GenericDefault;
                
                // draw text on the PDF page
                graphics.DrawString(text, font, brush, rect, stringFormat);
            }
                
            // save PDF document
            pdfDocument.SaveChanges();
        }
    }
    Requirements

    Target Platforms: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also