MeasureString and line breaks
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?
SIGN IN To post a reply.
3 Replies
SK
Surya Kumar
Syncfusion Team
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
AS
Alexander Schröders
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
SK
Surya Kumar
Syncfusion Team
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
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
AS Alexander Schröders
- Jun 24, 2019 11:08 AM UTC
- Jun 28, 2019 07:31 AM UTC