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

String Text Wrap

RectangleF answerRec = new RectangleF(20, 100, 520, 100);
            PdfPen borsder = new PdfPen(new PdfColor(51, 0, 0));
            graphics.DrawRectangle(borsder, answerRec);
            PdfFont answerF = new PdfStandardFont(PdfFontFamily.TimesRoman, 8);
            PdfTextElement answer = new PdfTextElement(peopleQ1comments, answerF);
            answer.Draw(page2, new PointF(answerRec.Left, answerRec.Top + 4));

in the above code "peopleQ1comments" is a string of text, when I create the PDF the text runs out of the "RectrangleF" & off the page, how do I wrap the text within the "answerRec"

5 Replies

GR Gayathri Ramalingam Syncfusion Team October 20, 2016 06:49 AM UTC

Hi Corey, 
Thank you for using Syncfusion Products. 
When we use Draw() method with PointF, the text will be truncated. Because by default the height and width for the text will be assigned as 0. So if we want to display the full text, we need to provide the appropriate height and width manually using RectangleF. 
The text can be wrapped into the RectangleF by using the below code snippet, 
//Set the bounds value. 
RectangleF bounds = new RectangleF(20,100,495,100); 
//Draw the rectangle. 
page.Graphics.DrawRectangle(pen, bounds); 
// Draw the text element. 
element.Draw(page, bounds, layoutFormat);  
 
We have prepared a sample to wrap the text inside the RectangleF. Please find the sample in the below link, 
 
With Regards, 
Gayathri R 



CP Corey Powell November 1, 2016 02:53 PM UTC

Thank you for your response, I have implemented the code, but when the PDF is generated the text is not wrapped in the box, not sure where I am going wrong, my code is below

RectangleF answerRec = new RectangleF(20, 65, 520, 40);
            PdfPen border = new PdfPen(new PdfColor(51, 0, 0));
            graphics.DrawRectangle(border, answerRec);
            PdfFont answerF = new PdfStandardFont(PdfFontFamily.TimesRoman, 6);
            PdfTextElement answer = new PdfTextElement(peopleQ1comments, answerF);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Top);
            format.ParagraphIndent = 5f;
            format.WordWrap = PdfWordWrapType.Word;
            answer.StringFormat = format;
            answer.Draw(page2, new PointF(answerRec.Left, answerRec.Top + 4));




GR Gayathri Ramalingam Syncfusion Team November 2, 2016 11:24 AM UTC

Hi Corey, 
 
Thank you for your update. Please find the details of your queries in the below table, 
Query 
Details 
 I have implemented the code, but when the PDF is generated the text is not wrapped in the box, not sure where I am going wrong, my code is below
 
RectangleF answerRec = new RectangleF(20, 65, 520, 40);
            PdfPen border = new PdfPen(new PdfColor(51, 0, 0));
            graphics.DrawRectangle(border, answerRec);
            PdfFont answerF = new PdfStandardFont(PdfFontFamily.TimesRoman, 6);
            PdfTextElement answer = new PdfTextElement(peopleQ1comments, answerF);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify, PdfVerticalAlignment.Top);
            format.ParagraphIndent = 5f;
            format.WordWrap = PdfWordWrapType.Word;
            answer.StringFormat = format; 

            answer.Draw(page2, new PointF(answerRec.Left, answerRec.Top + 4)); 
On analyzing your code snippet, we came to know that you have used PointF to draw the string. As mentioned in the previous update, When we use Draw() method with PointF, the text will be truncated. Because by default the height and width for the text will be assigned as 0. Due to this, the text has been truncated. We need to provide appropriate height and width manually using RectangleF. So, please draw the string using RectangleF instead of using PointF.   
 
Please find the below code snippet, 
             
answer.Draw(page2, answerRec); 

With Regards, 
Gayathri R 



CP Corey Powell November 7, 2016 01:20 PM UTC

Thank you for the support


GR Gayathri Ramalingam Syncfusion Team November 8, 2016 08:46 AM UTC

Hi Corey,  
Thank you for your update. 
 
With Regards, 
Gayathri R 


Loader.
Up arrow icon