Skip to main content
All docs
V25.1
  • PdfGraphics.DrawString(String, DXFont, DXSolidBrush, RectangleF, PdfStringFormat) Method

    Draws text in the specified page rectangle. Allows you to specify brush, font, and string parameters.

    Namespace: DevExpress.Pdf

    Assembly: DevExpress.Pdf.v25.1.Drawing.dll

    NuGet Package: DevExpress.Pdf.Drawing

    Declaration

    public void DrawString(
        string text,
        DXFont font,
        DXSolidBrush brush,
        RectangleF layout,
        PdfStringFormat format
    )

    Parameters

    Name Type Description
    text String

    A text to draw.

    font DXFont

    An object that defines text font options.

    brush DXSolidBrush

    An object that determines the color and texture of the drawn text.

    layout RectangleF

    A page rectangle in the world coordinate system where you can draw text.

    format PdfStringFormat

    An object that specifies text formatting attributes.

    Remarks

    Use the MeasureString method to calculate a size of the drawn text and a page area where you can draw text.

    Use the UseKerning property to enable kerning in drawn text.

    To draw a string on the PDF page, use one of the following methods:

    PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackground
    These methods allow you to draw content on an existing page.
    PdfDocumentProcessor.RenderNewPage
    Draws content on a new page.

    Example

    The code sample below draws text in the center of an empty page:

    draw string result

    using DevExpress.Pdf;
    using System.Drawing;
    using DevExpress.Drawing;
    //...
    
    using (var processor = new PdfDocumentProcessor())
    {
        processor.CreateEmptyDocument();
    
        using (PdfGraphics graphics = processor.CreateGraphics())
        {
            // Obtain the first document page
            PdfPage page = processor.AddNewPage(PdfPaperSize.A4);
            PdfRectangle pageSize = page.CropBox;
    
            // Specify text to draw
            string text = "PDF Document API";
            using (DXSolidBrush textBrush = new DXSolidBrush(Color.FromArgb(255, Color.DarkOrange)))
            {
                DXFont font = new DXFont("Segoe UI", 20, DXFontStyle.Regular)
                // Calculate text size
                SizeF textSize = graphics.MeasureString(text, font, PdfStringFormat.GenericTypographic);
    
                // Calculate an area where to draw text
                float x = (float)((pageSize.Width - textSize.Width) / 2);
                float y = (float)((pageSize.Height - textSize.Height) / 2);
    
                RectangleF textRect = new RectangleF(x, y, textSize.Width, textSize.Height);
    
                // Draw text in the calculated area
                graphics.DrawString(text, font, textBrush, textRect, PdfStringFormat.GenericTypographic);
    
                // Add graphics content to the page foreground
                graphics.AddToPageForeground(page, 72, 72);
            }
        }
        processor.SaveDocument("result.pdf");
    }
    
    See Also