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. Image for the cookie policy date

How to apply automatically Line Break with DrawString

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?


1 Reply

SK Sasi Kumar Sekar Syncfusion Team 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(); 
Online UG documentation link: 
Please let us know If you need further assistance in this. 
Sasi Kumar S. 

Live Chat Icon For mobile
Up arrow icon