Cómo aplicar/configurar alineación de texto al cuadro de texto
Los cuadros de texto pueden mejorar la expresividad de nuestros documentos y diagramas, y aplicar diferentes alineaciones a diferentes partes de un cuadro de texto puede ayudar a resaltar puntos de interés para los lectores. Pero la alineación predeterminada del cuadro de texto no satisface todas nuestras necesidades. Para esto, es posible que necesites ajustar cada cuadro de texto para cumplir con tus requisitos. Si no tienes muchos objetos de cuadro de texto para modificar, estás de suerte. Si hay muchos cuadros de texto para ajustar, creo que tendrás problemas. No te preocupes ahora, Aspose.Cells proporciona una interfaz de API para ayudarte a hacer justamente eso.
El siguiente código de ejemplo aplica la alineación de texto a un cuadro de texto.
// 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"); |
También puedes cambiar la alineación del texto dentro de una forma de cuadro de texto con el texto HTML apropiado. El siguiente código de ejemplo aplica la alineación de texto a una parte del texto dentro del cuadro de texto.
// 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"); |