With Spire.XLS, developers can add text or image to the textbox to Excel worksheet easily. From version 9.3.10, Spire.XLS supports to set the inner margin of contents on Excel text box. With this feature, we can adjust the position of the text contents on the textbox to make it beautiful. This article is going to introduce how to set the inner margins of the textbox in Excel worksheet in C#.
using Spire.Xls; using Spire.Xls.Core.Spreadsheet.Shapes; namespace SetInternalMargin { class Program { static void Main(string[] args) { { //load the sample document Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx", ExcelVersion.Version2010); //get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //add a textbox to the sheet and set its position and size XlsTextBoxShape textbox = sheet.TextBoxes.AddTextBox(4, 2, 100, 300) as XlsTextBoxShape; //set the text on the textbox textbox.Text = "Insert TextBox in Excel and set the margin for the text"; textbox.HAlignment = CommentHAlignType.Center; textbox.VAlignment = CommentVAlignType.Center; //set the inner margins of the contents textbox.InnerLeftMargin = 1; textbox.InnerRightMargin = 3; textbox.InnerTopMargin = 1; textbox.InnerBottomMargin = 1; //save the document to file workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010); } } } }
Effective screenshot after setting the margins of the contents: