Skip to main content
.NET Framework 4.5.2+

PdfGraphics.DrawString(String, Font, SolidBrush, Single, Single, PdfStringFormat) Method

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

Namespace: DevExpress.Pdf

Assembly: DevExpress.Pdf.v22.2.Drawing.dll

NuGet Package: DevExpress.Pdf.Drawing

Declaration

public void DrawString(
    string text,
    Font font,
    SolidBrush brush,
    float x,
    float y,
    PdfStringFormat format
)

Parameters

Name Type Description
text String

A text to draw.

font Font

An object that defines font options.

brush SolidBrush

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

x Single

The x-coordinate in the world coordinate system of a point where you can draw text.

y Single

The y-coordinate in the world coordinate system of a point where you can draw text.

format PdfStringFormat

An object that specifies text formatting attributes.

Remarks

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

The x and y parameters define the point where the text’s upper-left corner is located. Set the TextOrigin property to Baseline to make the x and y parameters define the point where the start of the text’s baseline is located. Set this property before the DrawString method call.

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.

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

draw string result

using DevExpress.Pdf;
using System.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 (SolidBrush textBrush = new SolidBrush(Color.FromArgb(255, Color.DarkOrange)))
        {
            using (Font font = new Font("Segoe UI", 20, FontStyle.Regular))
            {
                // Calculate text size
                SizeF textSize = graphics.MeasureString(text, font, PdfStringFormat.GenericTypographic);

                // Calculate a point where to draw text
                float x = (float)((pageSize.Width - textSize.Width) / 2);
                float y = (float)((pageSize.Height - textSize.Height) / 2);

                // Draw text at the calculated point
                graphics.DrawString(text, font, textBrush, x,y, PdfStringFormat.GenericTypographic);

                // Add graphics content to the page foreground
                graphics.AddToPageForeground(page, 72, 72);
            }
        }
    }
    processor.SaveDocument("result.pdf");
}

Example

See Also