Back to Devexpress

PdfGraphics.DrawString(String, DXFont, DXSolidBrush, PointF, PdfStringFormat) Method

officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-drawstring-x28-system-dot-string-devexpress-dot-drawing-dot-dxfont-devexpress-dot-drawing-dot-dxsolidbrush-system-dot-drawing-dot-pointf-devexpress-dot-pdf-dot-pdfstringformat-x29.md

latest5.9 KB
Original Source

PdfGraphics.DrawString(String, DXFont, DXSolidBrush, PointF, PdfStringFormat) Method

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

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public void DrawString(
    string text,
    DXFont font,
    DXSolidBrush brush,
    PointF point,
    PdfStringFormat format
)
vb
Public Sub DrawString(
    text As String,
    font As DXFont,
    brush As DXSolidBrush,
    point As PointF,
    format As PdfStringFormat
)

Parameters

NameTypeDescription
textString

A text to draw.

| | font | DXFont |

An object that defines font options.

| | brush | DXSolidBrush |

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

| | point | PointF |

A point in the world coordinate system where you can position 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 point parameter define the point where the text’s upper-left corner is located. Set the TextOrigin property to Baseline to make the point parameter define the point where the start of the text’s baseline is located. Set this property before DrawString the 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.AddToPageBackgroundThese methods allow you to draw content on an existing page.PdfDocumentProcessor.RenderNewPageDraws content on a new page.

Example

The following code snippet draws text in the center of an empty page:

csharp
using DevExpress.Pdf;
using System.Drawing;
using DevExpress.Drawing;
//...

using (var processor = new PdfDocumentProcessor())
{
    processor.CreateEmptyDocument();
    using (PdfGraphics graphics = processor.CreateGraphicsWorldSystem())
    {
        // 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 a point where to draw text
            PointF textPoint =
                new PointF((float)((pageSize.Width - textSize.Width) / 2), (float)((pageSize.Height - textSize.Height) / 2));

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

            // Add graphics content to the page foreground
            graphics.AddToPageForeground(page);
        }
    }
    processor.SaveDocument("result.pdf");
}
vb
Imports DevExpress.Pdf
Imports System.Drawing
Imports DevExpress.Drawing
'...

Using processor = New PdfDocumentProcessor()
  processor.CreateEmptyDocument()

  Using graphics As PdfGraphics = processor.CreateGraphicsWorldSystem()
    ' Obtain the first document page
    Dim page As PdfPage = processor.AddNewPage(PdfPaperSize.A4)
    Dim pageSize As PdfRectangle = page.CropBox

    ' Specify text to draw
    Dim text As String = "PDF Document API"
    Using textBrush As New DXSolidBrush(Color.FromArgb(255, Color.DarkOrange))
      font As New DXFont("Segoe UI", 20, DXFontStyle.Regular)
      ' Calculate text size
      Dim textSize As SizeF = graphics.MeasureString(text, font)

      ' Calculate a point where to draw text
      Dim textPoint As New PointF(CSng((pageSize.Width - textSize.Width) \ 2), CSng((pageSize.Height - textSize.Height) \ 2))

      ' Draw text at the calculated point
      graphics.DrawString(text, font, textBrush, textPoint)

      ' Add graphics content to the page foreground
      graphics.AddToPageForeground(page)
    End Using
  End Using
  processor.SaveDocument("result.pdf")
End Using

See Also

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace