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-rectanglef-x29.md
Draws text with the specified brush and font parameters in the specified page rectangle.
Namespace : DevExpress.Pdf
Assembly : DevExpress.Pdf.v25.2.Drawing.dll
NuGet Package : DevExpress.Pdf.Drawing
public void DrawString(
string text,
DXFont font,
DXSolidBrush brush,
RectangleF layout
)
Public Sub DrawString(
text As String,
font As DXFont,
brush As DXSolidBrush,
layout As RectangleF
)
| Name | Type | Description |
|---|---|---|
| text | String |
A text to draw.
| | font | DXFont |
A DXFont object that defines the text format of the string.
| | brush | DXSolidBrush |
A DXSolidBrush 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.
|
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.AddToPageBackgroundThese methods allow you to draw content on an existing page.PdfDocumentProcessor.RenderNewPageDraws content on a new page.
The following code snippet draws text in the center of an empty page:
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);
// 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);
// Add graphics content to the page foreground
graphics.AddToPageForeground(page);
}
}
processor.SaveDocument("result.pdf");
}
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))
Dim font As New DXFont("Segoe UI", 20, FontStyle.Regular)
' Calculate text size
Dim textSize As SizeF = graphics.MeasureString(text, font)
' Calculate an area where to draw text
Dim x As Single = CSng((pageSize.Width - textSize.Width) \ 2)
Dim y As Single = CSng((pageSize.Height - textSize.Height) \ 2)
Dim textRect As New RectangleF(x, y, textSize.Width, textSize.Height)
' Draw text in the calculated area
graphics.DrawString(text, font, textBrush, textRect)
' Add graphics content to the page foreground
graphics.AddToPageForeground(page)
End Using
End Using
processor.SaveDocument("result.pdf")
End Using
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the DrawString(String, DXFont, DXSolidBrush, RectangleF) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
office-file-api-ai-implementation/CS/BusinessObjects/Helpers.cs#L232
// Draw text at the calculated area
graphics.DrawString(text, font, textBrush, rectangle);
graphics.AddToPageForeground(page);
See Also