officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-measurestring-x28-system-dot-string-devexpress-dot-drawing-dot-dxfont-x29.md
Measures the specified string when drawn with specified font parameters.
Namespace : DevExpress.Pdf
Assembly : DevExpress.Pdf.v25.2.Drawing.dll
NuGet Package : DevExpress.Pdf.Drawing
public SizeF MeasureString(
string text,
DXFont font
)
Public Function MeasureString(
text As String,
font As DXFont
) As SizeF
| Name | Type | Description |
|---|---|---|
| text | String |
A text to measure.
| | font | DXFont |
An object that defines font parameters.
|
| Type | Description |
|---|---|
| SizeF |
The string’s measured size.
|
Use this method to calculate a size of the drawn text. Use the returned System.Drawing.SizeF object to calculate a page area or a point where you can draw text.
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 (var 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 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);
// 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, 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
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the MeasureString(String, DXFont) 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.
pdf-document-api-generate-document-layout/CS/DocumentCreationAPI/Program.cs#L58
using (PdfGraphics graphics = documentProcessor.CreateGraphicsPageSystem()) {
SizeF stringSize = graphics.MeasureString(text,font);
float scale = (float)(watermarkSize / (double)stringSize.Width);
See Also