Table of Contents

XlsFile.AutofitComment Method

Will return the resized anchor so the size of the comment is enough to fit all the text inside.

Syntax

Namespace: FlexCel.XlsAdapter

public override TClientAnchor AutofitComment(TRichString text, Double aspectRatio, Boolean dontShrink, Double adjustment, Double adjustmentFixed, TClientAnchor anchor)

Parameters

<-> Parameter Type Description
text TRichString Text for the comment.
aspectRatio Double If you pass 0 as aspect ratio, FlexCel will only resize the height of the comment, keeping the same width as it already had. If you pass another number like 4.0/3.0 or 16.0/10.0, FlexCel will try to create a comment box with that approximate aspect ratio. Note that the ratio won't be the exact aspect ratio, just approximate.
dontShrink Boolean If true, the box will grow if the text needs more space, but it won't be made smaller if the text fits in a smaller height than the existing anchor.
adjustment Double The final height will be multiplied by this number. Because of small differences between how FlexCel and Excel render the text, it might happen that the size calculated by FlexCel is smaller than what it needs to be to display correctly in Excel. By setting this number bigger than 1, you can add a margin for those errors.
adjustmentFixed Double This number will be added to the final calculated height. Use it to add some margin around the comment box.
anchor TClientAnchor

Returns

The new anchor so the size of the box fits the text.

Examples

This code will add a comment and resize the box so it fits:

    static void AddCommentAndResize(XlsFile xls, double aspectRatio, int row, int col, TRichString text)
    {
        TCommentProperties comProps = TCommentProperties.CreateStandard(row, col, xls); //Create a standard comment box.
        comProps.Anchor = xls.AutofitComment(text, aspectRatio, true, 1.1, 10, comProps.Anchor); //Resize it so it fits the text.
        xls.SetComment(row, col, text, "", comProps); //Add the comment.
    }

    public static void CreateFileWithComment()
    {
        XlsFile xls = new XlsFile(1, TExcelFileFormat.v2021, true);
        AddCommentAndResize(xls, 1.6, 3, 1, "This is a long comment so it won't fit in a standard comment box. " +
          " But by using AutoFitComment, we will resize the box to hold all the text, and have an 1.6 aspect ratio between width and height");
        xls.Save("test.xlsx");
    }

See also