Text not being wrapped with MeasureParagraphHeight / DrawText

Posted by: steve_goa on 9 June 2024, 12:29 pm EST

    • Post Options:
    • Link

    Posted 9 June 2024, 12:29 pm EST - Updated 9 June 2024, 1:12 pm EST

    Using: ActiveReports v16.4.1 / Visual Studio 2022 / .NET Framework v4.8.1 / WinForms / C#

    On a code-based SectionReport, I have the following code in the Detail_Format() event to render a paragraph onto the report with appropriate wrapping based on a given width. However, when it renders on the report, it renders on one line and gets cut off at the end of the line (the right page margin). I expect the text to render as a paragraph, wrapping at the set width. The report units are set to Inches in the report settings.

    This is my code:

    Document.Pages[Document.Pages.Count - 1].Font = new Font(pvFontName, pvSizeofFont, FontStyle.Bold);
    
    pvWidth = 7.8f; // 7.8 inches
    
    SizeF sz = Document.Pages[Document.Pages.Count - 1].MeasureParagraphHeight(pvData, pvWidth);
    
    float intParagraphHeight = sz.Height;
    
    Document.Pages[Document.Pages.Count - 1].DrawText(pvData, pvX, pvY, pvWidth, intParagraphHeight);
    

    Could you please guide me as to what could be wrong and how to resolve this?

  • Posted 10 June 2024, 2:18 am EST

    Hi Steve,

    Thanks for reaching out to us!

    AFAIK, the reason the text is drawn in a single line and not wrapped as a paragraph is that the DrawText method of the Page class does not automatically handle text wrapping. It draws the text within the specified width without considering word wrapping or paragraph layout.

    We would suggest that you consider using the TextBox control to render text as a paragraph using it’s WrapMode property.

    Please refer to the attached sample demonstrating the same. Let us know if you face any issues or have any further questions, and we’ll be glad to assist!

    TextWrapping_v16.zip

  • Posted 10 June 2024, 8:59 am EST - Updated 10 June 2024, 4:50 pm EST

    Hi Katyayny,

    I have checked your provided sample. My report is adding text on itself dynamically at runtime. In order to dynamically create and display a TextBox at runtime, we have to create the TextBox in the report’s OnStart() event, and then add it to the relevant section eg. Detail in the OnFomat() event. But what if the number of TextBoxes required is not known at the time when the report starts? Is there any alternative to DrawText or workaround that would work correctly as intended in this case? Eg. First putting the text into a TextBox and then rendering that TextBox’s window onto the report section’s canvas?

  • Posted 11 June 2024, 6:08 am EST

    Hi Steve,

    If your requirement is that you have multiple text strings and want to show them differently, then you can pass those strings as data to the report and bind a single textbox with the required field in the Detail section. This will render the textbox as many times as the number of fields in your data. Please refer to the attached sample demonstrating the same.

    You can refer to the attached documentation link regarding binding Section Reports to Data Source: Bind Reports to a Data Source

    If not, could you please let us know more about your use-case and report structure so that we can help you accordingly?

    DataBindingSample.zip

  • Posted 12 June 2024, 10:48 am EST - Updated 12 June 2024, 11:12 am EST

    Hi Katyayny,

    This was an existing software developed with AR2 and VB6.0 that renders text on the report from a SQL Server database, that we are now upgrading to C# .NET WinForms v4.8.1 / AR16. It is a complex report that while it does have data binding, it does not have text boxes on it. It draws the text on the report using the DrawText() method. It was working in AR2 because the MeasureParagraphHeight and DrawText functions were working correctly to measure the height of the text and then correctly drawing the text with text-wrapping. Its not compatible in AR.Net even though the Height parameter is present in DrawText function. I think that even MeasureParagraph is not returning the correct height ie. it is not wrapping the text for the supplied width. Your suggestions of adding textboxes to the report is not suitable for us because the report is dynamic depending on data present in the database and we don’t know in advance how many text boxes would be needed. Is there any other method available to: 1) Measure the text height for a paragraph given the width, and 2) Render it as wrapped text in the given rectangle?

    Best wishes,

    Steve

  • Posted 13 June 2024, 2:47 am EST - Updated 13 June 2024, 2:52 am EST

    Hi Steve,

    Thanks for explaining your requirements in such details. To draw the entire text as a paragraph, you can use custom code to ensure consistent vertical spacing and proper text wrapping based on the specified pvWidth. This involves measuring the height of a single line using MeasureParagraphHeight, splitting pvData into words, and iterating through each word to construct lines. Each word is added to the current line, and when adding a word exceeds pvWidth, we draw the current line using DrawText method. We then move to the next vertical position, reset the line with the current word, and repeat until all words are processed. Finally, any remaining text in the current line is drawn to complete the paragraph on the page.

    Please refer to the attached sample demonstrating the same. We hope this helps!

    Attachments:

    TextWrapping_v16_Updated.zip

  • Posted 13 June 2024, 6:10 am EST

    Hi Katyayny,

    This solution resolves the issue. Thank you.

  • Posted 13 June 2024, 11:20 pm EST

    Hi Steve,

    We are glad that your issue has been resolved! Feel free to create a new post/ticket in case of any queries or questions, and we’ll be glad to assist!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels