Articles in this section
Category / Section

How to find the length of the string while drawing into PDF?

1 min read

You can calculate the length of the string by using the MeasureString method in the PdfFont class.

C#

// Creates a new instance of the PdfDocument class.
PdfDocument document = new PdfDocument();
// Adds a page to the document.
PdfPage page = document.Pages.Add();
// Creates PDF graphics for the page.
PdfGraphics g = page.Graphics;
// Creates a solid brush.
PdfBrush brush = new PdfSolidBrush(Color.Black);
// Sets the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f);
//String used to draw.
string lengthString = "Hello world!";
//Measures string method for finding the length.
SizeF size= font.MeasureString(lengthString);
// Draws the text.
g.DrawString(lengthString, font, brush, new PointF(20, 20));
// Saves and closes the document.
document.Save("Sample.pdf");
document.Close(true);

Sample Link:

https://www.syncfusion.com/downloads/support/directtrac/general/MeasureString1238131623.zip

 

Conclusion

I hope you enjoyed learning about how to find the length of the string while drawing into PDF.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied