Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Description

Excerpt

Returns the minimum width required to fit the given text string using the given font.

Signature
C#
C#
 public double GetTextWidth(System.String text, Font font, double desiredHeight)
{signature}{signature:
}
Signature
vb.net
vb.net
Public Function GetTextWidth(ByVal text As String, ByVal font As Font, ByVal desiredHeight As Double) As Double
{signature}
{parameters}
{param:text}The text string whose width will be calculated.{param}
{param:font}The font that will be used to format the text.{param}
{param:desiredHeight}The height, in character units, of the cell or area into which the text will be inserted.{param}
{returns}The minimum width, in character units, required to accomodate the given text string.{returns}
{remarks}
The maximum width of any one column is 409 character units, so if the width return is greater than 409, you must merge cells across multiple columns and set the width of each column to accomodate the total minimum width of the text.
{remarks}
{example}{code:csharp|title=C#}
Parameters
Param
text
text

The text string whose width will be calculated.

Param
font
font

The font that will be used to format the text.

Param
desiredHeight
desiredHeight

The height, in character units, of the cell or area into which the text will be inserted.

Returns

The minimum width, in character units, required to accomodate the given text string.

Remarks

The maximum width of any one column is 409 character units, so if the width return is greater than 409, you must merge cells across multiple columns and set the width of each column to accomodate the total minimum width of the text.

Example
Code Block
csharp
csharp
titleC#


          //--- What is the height of row 1?
          RowProperties rowProps = worksheet.GetRowProperties(0);

          //--- How wide will the data be?
          double requiredWidth =
               wb.GetTextWidth(longTextString,
               rowProps.Style.Font,
               rowProps.Height);

          //--- Will we need multiple columns to display all of the data?
          //--- Can not exceed 255 character units in width per column.
          int numCols = (int) Math.round((requiredWidth / 255) + 0.5);

          //--- Evenly distribute the required width between all of the columns.
          worksheet.Cells[0, 3].Value = longTextString;
          a = worksheet.createArea(0, 3, 1, numCols);
          a.MergeCells();
          a.AllColumnWidthsInChars = (requiredWidth / numCols);
        
{code} {code:
Code Block
vb.net
|title=
vb.net
titlevb.net
}


          '--- What is the height of row 1?
          Dim rowProps As RowProperties = worksheet.GetRowProperties(0)

          '--- How wide will the data be?
          Dim requiredWidth As Double = _
               wb.GetTextWidth(longTextString, _
               rowProps.Style.Font, _
               rowProps.Height)

          '--- Will we need multiple columns to display all of the data?
          '--- Can not exceed 409 character units in width per column.
          Dim numCols As Integer = Math.Round((requiredWidth / 409) + 0.5)

          '--- Evenly distribute the required width between all of the columns.
          worksheet.Cells(0, 3).Value = longTextString
          Dim a As Area = worksheet.CreateArea(2, 2, numRows, 1)
          a.MergeCells()
          a.AllColumnWidthsInChars = (requiredHeight / numRows)
        
{code} {example}