We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

MeasureString and line breaks

Thread ID:

Created:

Updated:

Platform:

Replies:

145472 Jun 24,2019 11:08 AM UTC Jun 28,2019 07:31 AM UTC UWP 3
loading
Tags: Pdf
Alexander Schröders
Asked On June 24, 2019 11:08 AM UTC

Hi I have a little trouble with strings containing line breaks an measuring there size. I am using the MeasureString method of PdfFont at the moment and get the correct size for strings without any line break. But when I have a line break in them then I get the size as if the line breaks were normal characters. Is there any way to get MeasureString to recognize the line breaks or another way to get the correct size?


Surya Kumar [Syncfusion]
Replied On June 25, 2019 08:50 AM UTC

Hi Alexander, 

Greetings from Syncfusion. 

The MesaureString function will only measure the width and height of the given string along with spaces in between with respect to the font which is specified, it does not identify the line breaks present in the lengthy string.  

In order to  use the bounds of last layout text contents or paginate the text PdfTextElement can be used and you may need to set layout Format for the text element as shown in the code below: 
//Create a text element 
PdfTextElement element = new PdfTextElement(text, font); 
element.Brush = new PdfSolidBrush(Color.Black); 
element.StringFormat = format; 
element.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); 
//Set the properties to paginate the text. 
PdfLayoutFormat layoutFormat = new PdfLayoutFormat(); 
layoutFormat.Break = PdfLayoutBreakType.FitPage; 
layoutFormat.Layout = PdfLayoutType.Paginate; 
//Draw the text element with the properties and formats set. 
PdfTextLayoutResult result = element.Draw(page, bounds, layoutFormat); 
// Set bounds of next text based on last layout bounds 
bounds = new RectangleF( result.LastLineBounds.X , result.LastLineBounds.Y+20, page.Graphics.ClientSize.Width, page.Graphics.ClientSize.Height); 
// Use the  PdfTextLayoutResult Page propoerty whihch gets the last layout page after pagination. 
element.Draw(result.Page, bounds, layoutFormat); 
 

We have created a simple sample for the same it can be downloaded from below link: 

If this does not full fill your needs , could you please let us know your exact requirement for performing the MeasureString so that we can provide you a better solution. 

Regards, 
Surya Kumar 



Alexander Schröders
Replied On June 27, 2019 06:14 PM UTC

Hi Surya,

thanks for the reply. I am not quite sure if this helps me but I found an example in your samples section which does this pretty much what I wanted to do(https://help.syncfusion.com/file-formats/pdf/working-with-text#string-layout). So I am now using PdfStringLayouter to get the size of a string with line breaks. 

Regards Alexander

Surya Kumar [Syncfusion]
Replied On June 28, 2019 07:31 AM UTC

Hi Alexander, 

We are glad to know that you find suitable solution. Let us know if you need any further information. 

Regards, 
Surya Kumar 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon