TExcelFile.AutofitComment Method
Will return the resized anchor so the size of the comment is enough to fit all the text inside.
Syntax
Unit: FlexCel.Core
function TExcelFile.AutofitComment(const text: TRichString; const aspectRatio: Double; const dontShrink: Boolean; const adjustment: Double; const adjustmentFixed: Double; const Anchor: TClientAnchor): TClientAnchor; virtual; abstract;
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
const | text | TRichString | Text for the comment. |
const | 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. |
const | 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. |
const | 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. |
const | adjustmentFixed | Double | This number will be added to the final calculated height. Use it to add some margin around the comment box. |
const | Anchor | TClientAnchor | Here you pass the original dimensions for the comment. |
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:
procedure AddCommentAndResize(const xls: TXlsFile; const aspectRatio: double; const row: Int32; const col: Int32; const text: TRichString);
var
comProps: ICommentProperties;
begin
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.
end;
procedure CreateFileWithComment;
var
xls: TXlsFile;
begin
xls := TXlsFile.Create(1, TExcelFileFormat.v2021, true);
try
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');
finally
xls.Free;
end;
end;