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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to apply automatically Line Break with DrawString

Thread ID:

Created:

Updated:

Platform:

Replies:

130817 Jun 5,2017 12:38 PM UTC Jun 6,2017 07:35 AM UTC Xamarin.Forms 1
loading
Tags: PDF
RODRIGO AMARAL
Asked On June 5, 2017 12:38 PM UTC

Hi, I'm trying to apply Line break to a text with the follow code:

element = new PdfTextElement("Descrição:", fontBold)
            {
                Brush = PdfBrushes.Black,
            };
            result = element.Draw(page, new PointF(0, result.Bounds.Bottom + 25));

            string text15 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu aliquet justo. Cras mattis nisi pulvinar placerat sodales. Nunc nisi enim, interdum quis elit non, finibus pharetra felis. Fusce sed.";

            //Measures the width of the text to place it in the correct location
            textSize = font.MeasureString(text15);
            textPosition = new PointF(165, result.Bounds.Y - 50);
            graphics.DrawString(text15, font, element.Brush, textPosition, new PdfStringFormat()
            {
                WordWrap = PdfWordWrapType.Word
            });
 

But nothing heapens. How can I apply automatically Line Break with DrawString to a long text?

Thanks.



Sasi Kumar Sekar [Syncfusion]
Replied On June 6, 2017 07:35 AM UTC

Hi Rodrigo, 
 
Thank you for contacting Syncfusion support, 
 
We can achieve automatic line break by specifying the bounds to draw the text and we used the two more classes PdfLayoutFormat and PdfLayoutResult, which used to allow the text to flow to across pages and rendering the text with the specified bounds without overlapping the text respectively. Please find the Code snippet and online UG documentation link for automatic line break. 
 
Code snippet: 
//Create a PDF document instance 
PdfDocument document = new PdfDocument(); 
 
//Add page to the document 
PdfPage page = document.Pages.Add(); 
 
PdfGraphics graphics = page.Graphics; 
 
//A long string. 
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"; 
 
const int paragraphGap = 10; 
 
//Create a text element with the text and font 
PdfTextElement textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
 
PdfLayoutFormat layoutFormat = new PdfLayoutFormat(); 
 
layoutFormat.Layout = PdfLayoutType.Paginate; 
 
layoutFormat.Break = PdfLayoutBreakType.FitPage; 
 
//Draw the first paragraph 
PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, 0, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat); 
 
//Draw the second paragraph from the first paragraph end position 
result = textElement.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphGap, page.GetClientSize().Width / 2, page.GetClientSize().Height), layoutFormat); 
 
MemoryStream stream = new MemoryStream(); 
document.Save(stream); 
document.Close(true); 
 
Online UG documentation link: 
 
Please let us know If you need further assistance in this. 
 
Regards, 
Sasi Kumar S. 


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

or the page will be automatically redirected to 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

;