SAP Knowledge Base Article - Public

1441329 - How to change the width of a field dynamically in Crystal Reports?

Symptom

  • How to dynamically change the size of a field?
  • How to change the width of a field dynamically in Crystal Reports, based on the number of characters the field contain?
        
        
  • Note: Images and data in this SAP Knowledge Base Article is from SAP internal systems, sample data, or demo systems. Any resemblance to real data is purely coincidental.

Environment

  • SAP Crystal Reports 2013
  • SAP Crystal Reports 2016
  • SAP Crystal Reports 2020

Resolution

  • Create a formula to dynamically resize the width of a field using the Size and Position option: "Width"
      
    1. In Crystal Reports, right click on the field you want to dynamically resize based on the number of characters it displays, and select "Size and Position..."
        
    2. In the window: "Object Size and Position", set the Width to be small, like: 0.1
        
    3. To set the width dynamically, click on the "X-2" button located on the right side of the option "Width"
        
    4. In the Formula Editor, enter a formula like:

      Local NumberVar Character_size := 110;
      Local NumberVar Number_Of_Characters_Limit := 35;
      Local NumberVar Current_Number_Of_Characters := Length(CurrentFieldValue);

      If Current_Number_Of_Characters > Number_Of_Characters_Limit Then
           Character_Size * Number_Of_Characters_Limit
      Else
           Character_Size * Current_Number_Of_Characters
        

      Important Notes:
         
      • The unit of measurement for the size is not in characters, because in general, a character in MS Windows have different width, and size, depending on the font selected.  For example: The width of the character "A", is larger than the width for the character "I" 
          
      • The unit of measurement used in a formula is twip. And one inch is equal to 1440 twips.
          
      • There are some fonts in MS Windows that have a Fixed Character Length Size, like: Courrier New, but it still have the font size to take into account, therefore is not possible to create a formula for all scenario, and this is why in the above formula sample, it uses a variable for the estimated Character Size in twip, which you will need to change depending on the font used, and font size used on the report.
          
      • Finally, the sample formula set a number of characters limit per line, to ensure the width of the field will not overlay another field, or go outside the paper size. You will also need to modify the value for the number of characters limit to fit your report design requirement.
              
    1. Save and close the formula.
        
    2. Back to "Object Size and Position", click "OK" to accept the change.
        
         
  • Sample Report demonstrating how to set a field width size dynamically is available in the Attachments section below, and it is called: Dynamic Field Width.rpt

Keywords

Width of the Field, Dynamic changes. , KBA , BI-RA-CR , Crystal Reports designer or Business View Manager , How To

Product

SAP Crystal Reports 2013 ; SAP Crystal Reports 2016 ; SAP Crystal Reports 2020

Attachments

Dynamic Field Width.rpt