Come applicare/impostare l allineamento del testo alla casella di testo
Le caselle di testo possono migliorare l’espressività dei nostri documenti e diagrammi, e l’applicazione di diversi allineamenti a diverse parti di una casella di testo può aiutare a evidenziare punti di interesse per i lettori. Tuttavia, l’allineamento predefinito della casella di testo non soddisfa tutte le nostre esigenze. Per questo, potrebbe essere necessario regolare ogni casella di testo per soddisfare i requisiti desiderati. Se non si dispone di molte caselle di testo da regolare, si è fortunati. Se ci sono così tante caselle di testo da regolare, penso che avrete problemi. Non preoccupatevi, ora Aspose.Cells fornisce un’interfaccia API per aiutarvi a fare proprio questo.
Il seguente codice di esempio applica l’allineamento del testo a una casella di testo.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = ""; | |
if (!System.IO.Directory.Exists(dataDir)) | |
{ | |
System.IO.Directory.CreateDirectory(dataDir); | |
} | |
//Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
ShapeCollection shapes = workbook.Worksheets[0].Shapes; | |
//add a TextBox | |
Shape shape = shapes.AddTextBox(2, 0, 2, 0, 50, 120); | |
shape.Text = "This is a test."; | |
//set alignment | |
shape.TextHorizontalAlignment = TextAlignmentType.Center; | |
shape.TextVerticalAlignment = TextAlignmentType.Center; | |
//Save the excel file. | |
workbook.Save(dataDir + "result.xlsx"); |
È anche possibile cambiare l’allineamento del testo di alcuni testi all’interno di una forma casella di testo con l’appropriato testo HTML.Il seguente codice di esempio applica l’allineamento del testo a parte del testo all’interno della casella di testo.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Intialize an object of the Workbook class to load template file | |
Workbook sourceWb = new Workbook("SampleTextboxExcel2016.xlsx"); | |
// Access the target textbox whose text is to be aligned | |
var sourceTextBox = sourceWb.Worksheets[0].Shapes[0]; | |
// Create and object of the target workbook | |
var destWb = new Workbook(); | |
// Access first worksheet from the collection | |
var _sheet = destWb.Worksheets[0]; | |
//Create new textbox | |
TextBox _textBox = (TextBox)_sheet.Shapes.AddShape( MsoDrawingType.TextBox,1, 0, 1, 0, 200, 200); | |
// Alternatively text box can be added using following line as well | |
// TextBox _textBox = _sheet.Shapes.AddTextBox(1, 0, 1, 0, 200, 200); | |
// Use Html string from a template file textbox | |
_textBox.HtmlText = sourceTextBox.HtmlText; | |
// Save the workbook on disc | |
destWb.Save("Output.xlsx"); |