measureString method
Measures a string by using this font.
//Create a new PDF document.
PdfDocument document = PdfDocument();
//Add a page to the document.
PdfPage page = document.pages.add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.graphics;
//Create a new PDF font instance.
PdfFont font = PdfStandardFont(PdfFontFamily.helvetica, 12);
String text = "Hello World!";
//Measure the text.
Size size = font.measureString(text);
//Draw string to PDF page.
graphics.drawString(text, font,
brush: PdfBrushes.black,
bounds: Rect.fromLTWH(0, 0, size.width, size.height));
//Saves the document.
List<int> bytes = await document.save();
//Dispose the document.
document.dispose();
Implementation
Size measureString(String text, {Size? layoutArea, PdfStringFormat? format}) {
layoutArea ??= Size.zero;
final PdfStringLayouter layouter = PdfStringLayouter();
final PdfStringLayoutResult result = layouter.layout(text, this, format,
width: layoutArea.width, height: layoutArea.height);
return result.size.size;
}