Back to Devexpress

PdfGraphics.MeasureString(String, DXFont, SizeF, PdfStringFormat) Method

officefileapi-devexpress-dot-pdf-dot-pdfgraphics-dot-measurestring-x28-system-dot-string-devexpress-dot-drawing-dot-dxfont-system-dot-drawing-dot-sizef-devexpress-dot-pdf-dot-pdfstringformat-x29.md

latest4.9 KB
Original Source

PdfGraphics.MeasureString(String, DXFont, SizeF, PdfStringFormat) Method

Measures the specified string when drawn with the specified font, text layout size, and format.

Namespace : DevExpress.Pdf

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

NuGet Package : DevExpress.Pdf.Drawing

Declaration

csharp
public SizeF MeasureString(
    string text,
    DXFont font,
    SizeF layoutSize,
    PdfStringFormat format
)
vb
Public Function MeasureString(
    text As String,
    font As DXFont,
    layoutSize As SizeF,
    format As PdfStringFormat
) As SizeF

Parameters

NameTypeDescription
textString

A String to measure.

| | font | DXFont |

An object that contains font parameters.

| | layoutSize | SizeF |

Specifies the maximum layout area for the text.

| | format | PdfStringFormat |

An object that contains text formatting parameters.

|

Returns

TypeDescription
SizeF

The string’s measured size.

|

Remarks

Use this method to calculate a size of the drawn text. Use the returned SizeF object to calculate a page area or a point where you can draw text.

Example

The following code snippet uses the MeasureString method to draw text in the page center:

csharp
using DevExpress.Pdf;
using System.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))) {
        using (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
'...

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

  Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
    ' 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))
      Using font As New DXFont("Segoe UI", 20, DXFontStyle.Regular)
        ' Calculate text size
        Dim textSize As SizeF = graphics.MeasureString(text, font, PdfStringFormat.GenericTypographic)

        ' 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, PdfStringFormat.GenericTypographic)

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

See Also

PdfGraphics Class

PdfGraphics Members

DevExpress.Pdf Namespace